@@ -206,23 +206,26 @@ Your own Sender
206206
207207Imagine that you already have an ``ImportantAction `` message going through the
208208message bus and being handled by a handler. Now, you also want to send this
209- message as an email.
209+ message as an email (using the :doc: `Mime </components/mime >` and
210+ :doc: `Mailer </components/mailer >` components).
210211
211212Using the :class: `Symfony\\ Component\\ Messenger\\ Transport\\ Sender\\ SenderInterface `,
212213you can create your own message sender::
213214
214215 namespace App\MessageSender;
215216
216217 use App\Message\ImportantAction;
218+ use Symfony\Component\Mailer\MailerInterface;
217219 use Symfony\Component\Messenger\Envelope;
218220 use Symfony\Component\Messenger\Transport\Sender\SenderInterface;
221+ use Symfony\Component\Mime\Email;
219222
220223 class ImportantActionToEmailSender implements SenderInterface
221224 {
222225 private $mailer;
223226 private $toEmail;
224227
225- public function __construct(\Swift_Mailer $mailer, string $toEmail)
228+ public function __construct(MailerInterface $mailer, string $toEmail)
226229 {
227230 $this->mailer = $mailer;
228231 $this->toEmail = $toEmail;
@@ -237,12 +240,10 @@ you can create your own message sender::
237240 }
238241
239242 $this->mailer->send(
240- (new \Swift_Message('Important action made'))
241- ->setTo($this->toEmail)
242- ->setBody(
243- '<h1>Important action</h1><p>Made by '.$message->getUsername().'</p>',
244- 'text/html'
245- )
243+ (new Email())
244+ ->to($this->toEmail)
245+ ->subject('Important action made')
246+ ->html('<h1>Important action</h1><p>Made by '.$message->getUsername().'</p>')
246247 );
247248
248249 return $envelope;
0 commit comments