draw / mailer
Requires
- php: >=8.2
- draw/core: ^0.10.47
- symfony/console: ^6.4.0
- symfony/css-selector: ^6.4.0
- symfony/dom-crawler: ^6.4.0
- symfony/mailer: ^6.4.0
- symfony/translation: ^6.4.0
- symfony/twig-bridge: ^6.4.0
- twig/twig: ^3.3
Requires (Dev)
- draw/dependency-injection: ^0.10.47
- draw/tester: ^0.10.47
- pelago/emogrifier: ^7.0
- phpunit/phpunit: ^11.3
- dev-master / 0.11.x-dev
- 0.10.47
- 0.10.46
- 0.10.45
- 0.10.44
- 0.10.43
- 0.10.42
- 0.10.41
- 0.10.40
- 0.10.39
- 0.10.38
- 0.10.37
- 0.10.36
- 0.10.35
- 0.10.34
- 0.10.33
- 0.10.32
- 0.10.31
- 0.10.30
- 0.10.29
- 0.10.28
- 0.10.27
- 0.10.26
- 0.10.25
- 0.10.24
- 0.10.23
- 0.10.22
- 0.10.21
- 0.10.20
- 0.10.18
- 0.10.17
- 0.10.16
- 0.10.15
- 0.10.14
- 0.10.13
- 0.10.11
- 0.10.10
- 0.10.9
- 0.10.8
- 0.10.7
- 0.10.6
- 0.10.5
- 0.10.4
- 0.10.3
- 0.10.2
- 0.10.1
- 0.10.0
- 0.9.92
- 0.9.91
- 0.9.90
- 0.9.89
- 0.9.88
- 0.9.87
- 0.9.86
- 0.9.85
- 0.9.84
- 0.9.83
- 0.9.82
- 0.9.81
- 0.9.80
- 0.9.79
- 0.9.78
- 0.9.77
- 0.9.76
- 0.9.75
- 0.9.74
- 0.9.73
- 0.9.72
- 0.9.71
- 0.9.70
- 0.9.69
- 0.9.68
- 0.9.67
- 0.9.66
- 0.9.65
- 0.9.64
- 0.9.63
- 0.9.62
- 0.9.61
- 0.9.60
- 0.9.59
- 0.9.58
- 0.9.57
- 0.9.56
- 0.9.55
- 0.9.54
- 0.9.53
- 0.9.52
- 0.9.51
- 0.9.50
- 0.9.49
- 0.9.48
- 0.9.47
- 0.9.46
- 0.9.45
- 0.9.44
- 0.9.43
- 0.9.42
- 0.9.41
- 0.9.40
- 0.9.39
- 0.9.38
- 0.9.37
- 0.9.36
- 0.9.35
- 0.9.34
- 0.9.33
- 0.9.32
- 0.9.31
- 0.9.30
- 0.9.29
- 0.9.28
- 0.9.27
- 0.9.26
- 0.9.25
- 0.9.24
- 0.9.23
- 0.9.22
- 0.9.21
- 0.9.20
- 0.9.19
- 0.9.18
- 0.9.17
- 0.9.16
- 0.9.15
- 0.9.14
- 0.9.13
- 0.9.12
- 0.9.11
- 0.9.10
- 0.9.9
- 0.9.8
- 0.9.7
- 0.9.6
- 0.9.5
- 0.9.4
- 0.9.3
- 0.9.2
- 0.9.1
- 0.9.0
- 0.8.9
- 0.8.8
- 0.8.7
- 0.8.6
- 0.8.5
- 0.8.4
- 0.8.3
- 0.8.2
- 0.8.1
- 0.8.0
This package is auto-updated.
Last update: 2024-09-25 20:27:09 UTC
README
由于 Symfony/Mailer 尚未完成,某些行为可能在后续版本中受到影响,请将此包视为实验性的
此包允许将电子邮件的创建委托给一个特定的类。
它还允许配置默认的 from。
配置
draw_post_office:
default_from: 'support@example.com'
您不是在控制器中直接构建电子邮件,而是创建一个继承自 Symfony\Component\Mime\Email 的类,并为它创建一个 writer。
任何实现 Draw\Component\Mailer\EmailWriter\EmailWriterInterface 的服务都将注册为 writer。必须返回一个方法与优先级(作为值)的映射,以将方法注册为 writer(如果您返回方法作为值,则认为优先级为 0)。系统将检测电子邮件是否与方法的第一个参数的类匹配,并在需要时调用它。
邮局声明了一个监听器,用于 Symfony\Component\Mailer\Event\MessageEvent,以便将其连接到 symfony 邮件系统。
按照惯例,建议创建一个 Email 文件夹,在其中创建所有电子邮件类,并为实现 Draw\Component\Mailer\EmailWriter\EmailWriterInterface 的类创建一个 EmailWriter。
示例
让我们创建一个忘记密码的电子邮件,此类将包含用于编写电子邮件的最 最小 信息,在这种情况下,触发忘记密码电子邮件流程的用户电子邮件。
<?php namespace App\Email; use Symfony\Bridge\Twig\Mime\TemplatedEmail; class ForgotPasswordEmail extends TemplatedEmail { private $emailAddress; public function __construct(string $emailAddress) { $this->emailAddress = $emailAddress; parent::__construct(); } /** * The email address of the person who forgot is email */ public function getEmailAddress(): string { return $this->emailAddress; } }
我们必须为电子邮件创建一个 writer
<?php namespace App\Email; use App\Email\ForgotPasswordEmail; use App\LostPasswordTokenProvider; use Draw\Component\Mailer\Email\EmailWriterInterface; class ForgotPasswordEmailWriter implements EmailWriterInterface { private $lostPasswordTokenProvider; public function __construct(LostPasswordTokenProvider $lostPasswordTokenProvider) { $this->lostPasswordTokenProvider = $lostPasswordTokenProvider; } public static function getForEmails(): array { return ['compose']; // Or ['compose' => 0]; } public function compose(ForgotPasswordEmail $forgotPasswordEmail) { $emailAddress = $forgotPasswordEmail->getEmailAddress(); $forgotPasswordEmail ->to($emailAddress) ->subject('You have forgotten your password !') ->htmlTemplate('emails/forgot_password.html.twig') ->context([ 'token' => $this->lostPasswordTokenProvider->generateToken($emailAddress) ]); } }
基本控制器示例
<?php namespace App\Controller; use App\Email\ForgotPasswordEmail; use Symfony\Component\Mailer\MailerInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\RedirectResponse; class ForgotPasswordController { public function forgotPasswordAction( Request $request, MailerInterface $mailer ): Response { if ($request->getMethod() == Request::METHOD_GET) { return $this->render('users/forgot_password.html.twig'); } // ... You should have a logic to validate there is a user and send a different email ... / $mailer->send(new ForgotPasswordEmail($request->request->get('email'))); return new RedirectResponse($this->generateUrl('check_email')); } }
这样,您就可以保持控制器整洁,并按照编写电子邮件和覆盖的方式对其进行结构化。
系统还将 Envelope 参数作为第二个参数传递,以防您需要它。
如果您查看 Draw\Component\Mailer\EmailWriter\DefaultFromEmailWriter,您将看到如何创建一个为所有发送的电子邮件调用的 writer。