globegroup/mailer-bundle

v1.0 2020-01-30 15:37 UTC

This package is auto-updated.

Last update: 2024-09-29 05:29:06 UTC


README

包含的功能

  • 使用.env.local配置NG和API主机

安装

  1. symfony/mailer需要4.4.*版本
  2. 执行命令
    composer require globegroup/emaillabs-mailer
    
  3. .env.local中使用以下配置
    API_HOST=api.globegroup.test
    API_SCHEME=http
    NG_HOST=globegroup.test
    NG_SCHEME=http
    FROM_EMAIL=test@globegroup.test
    NO_REPLY_EMAIL=test@globegroup.test
    ### konfiguracja API oraz frontendu ###
  4. 同时,请记得配置MAILER_DSNsymfony/mailer

本地测试

  1. 将仓库克隆到symfony/localVendor文件夹中。
  2. 添加到composer.json
    "repositories": [
        {
            "type": "path",
            "url": "localVendor/globegroup-emaillabs-mailer"
        }
    ],
  3. 检查minimum-stability是否设置为dev
  4. 继续安装

用法

  1. Create class App\Mailer\TestMailer:

    <?php namespace App\Mailer; use GlobeGroup\MailerBundle\Mailer\Mailer; class TestMailer extends Mailer { }
  2. Create method which will be sending email or emails.

    public function sendMessage(User $user): void { $this->setVariables([ 'user' => $user, ]); $email = $this->getTemplatedEmail() ->subject($this->getTranslatedSubject('authorization.subject')) ->htmlTemplate('emails/authorization.html.twig') ->addTo($user->getEmail()) ->context($this->getVariables()) ; $this->mailer->send($email); }
  3. $variables is an array which are passed to Twig email template.

    Also there are two variables which are passed by default by method $this->addBasicVariables($variables):

    {{ ngHostWithScheme }} = link with http/https to frontend {{ apiHostWithScheme }} = link with http/https to backend
  4. If you need to pass extra configuration variables like url you can use dependency injection:

    <?php namespace App\Mailer; use GlobeGroup\MailerBundle\Mailer\Mailer; use GlobeGroup\MailerBundle\Mailer\MailerParameters; use Symfony\Component\Mailer\MailerInterface; use Symfony\Contracts\Translation\TranslatorInterface; class AuthorizationMailer extends Mailer { /** @var string $link */ private $link; public function __construct( MailerInterface $mailer, MailerParameters $mailerParameters, TranslatorInterface $translator, string $link ) { parent::__construct($mailer, $mailerParameters, $translator); $this->link = $link; } }
  5. And in services.yaml you need to pass values:

    parameters: link: '%ngHostWithScheme%/link' services: [...] App\Mailer\AuthorizationMailer: $link: '%link%' 
  6. Variables %ngHostWithScheme% and %apiHostWithScheme% are configured inside bundle.

  7. When setting array of template variables use method $this->setVariables([]);

  8. When passing variables into email template use method $this->getVariables();

License

此包采用MIT许可。请参阅完整的LICENSE