@@ -200,23 +200,26 @@ Your own Sender
200
200
201
201
Imagine that you already have an ``ImportantAction `` message going through the
202
202
message bus and being handled by a handler. Now, you also want to send this
203
- message as an email.
203
+ message as an email (using the :doc: `Mime </components/mime >` and
204
+ :doc: `Mailer </components/mailer >` components).
204
205
205
206
Using the :class: `Symfony\\ Component\\ Messenger\\ Transport\\ Sender\\ SenderInterface `,
206
207
you can create your own message sender::
207
208
208
209
namespace App\MessageSender;
209
210
210
211
use App\Message\ImportantAction;
212
+ use Symfony\Component\Mailer\MailerInterface;
211
213
use Symfony\Component\Messenger\Envelope;
212
214
use Symfony\Component\Messenger\Transport\Sender\SenderInterface;
215
+ use Symfony\Component\Mime\Email;
213
216
214
217
class ImportantActionToEmailSender implements SenderInterface
215
218
{
216
219
private $mailer;
217
220
private $toEmail;
218
221
219
- public function __construct(\Swift_Mailer $mailer, string $toEmail)
222
+ public function __construct(MailerInterface $mailer, string $toEmail)
220
223
{
221
224
$this->mailer = $mailer;
222
225
$this->toEmail = $toEmail;
@@ -231,12 +234,10 @@ you can create your own message sender::
231
234
}
232
235
233
236
$this->mailer->send(
234
- (new \Swift_Message('Important action made'))
235
- ->setTo($this->toEmail)
236
- ->setBody(
237
- '<h1>Important action</h1><p>Made by '.$message->getUsername().'</p>',
238
- 'text/html'
239
- )
237
+ (new Email())
238
+ ->to($this->toEmail)
239
+ ->subject('Important action made')
240
+ ->html('<h1>Important action</h1><p>Made by '.$message->getUsername().'</p>')
240
241
);
241
242
242
243
return $envelope;
0 commit comments