globegroup / mailer-bundle
v1.0
2020-01-30 15:37 UTC
Requires
- php: ^7.3
- symfony/mailer: ^4.4|^5.0
- symfony/translation: ^4.4|^5.0
This package is auto-updated.
Last update: 2024-09-29 05:29:06 UTC
README
包含的功能
- 使用.env.local配置NG和API主机
安装
- symfony/mailer需要4.4.*版本
- 执行命令
composer require globegroup/emaillabs-mailer
- 在
.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 ###
- 同时,请记得配置
MAILER_DSN
从symfony/mailer。
本地测试
- 将仓库克隆到
symfony/localVendor
文件夹中。 - 添加到composer.json
"repositories": [ { "type": "path", "url": "localVendor/globegroup-emaillabs-mailer" } ],
- 检查
minimum-stability
是否设置为dev
。 - 继续安装。
用法
-
Create class
App\Mailer\TestMailer
:<?php namespace App\Mailer; use GlobeGroup\MailerBundle\Mailer\Mailer; class TestMailer extends Mailer { }
-
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); }
-
$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
-
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; } }
-
And in
services.yaml
you need to pass values:parameters: link: '%ngHostWithScheme%/link' services: [...] App\Mailer\AuthorizationMailer: $link: '%link%'
-
Variables
%ngHostWithScheme%
and%apiHostWithScheme%
are configured inside bundle. -
When setting array of template variables use method
$this->setVariables([])
; -
When passing variables into email template use method
$this->getVariables()
;
License
此包采用MIT许可。请参阅完整的LICENSE