@@ -206,23 +206,26 @@ Your own Sender
206
206
207
207
Imagine that you already have an ``ImportantAction `` message going through the
208
208
message 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).
210
211
211
212
Using the :class: `Symfony\\ Component\\ Messenger\\ Transport\\ Sender\\ SenderInterface `,
212
213
you can create your own message sender::
213
214
214
215
namespace App\MessageSender;
215
216
216
217
use App\Message\ImportantAction;
218
+ use Symfony\Component\Mailer\MailerInterface;
217
219
use Symfony\Component\Messenger\Envelope;
218
220
use Symfony\Component\Messenger\Transport\Sender\SenderInterface;
221
+ use Symfony\Component\Mime\Email;
219
222
220
223
class ImportantActionToEmailSender implements SenderInterface
221
224
{
222
225
private $mailer;
223
226
private $toEmail;
224
227
225
- public function __construct(\Swift_Mailer $mailer, string $toEmail)
228
+ public function __construct(MailerInterface $mailer, string $toEmail)
226
229
{
227
230
$this->mailer = $mailer;
228
231
$this->toEmail = $toEmail;
@@ -237,12 +240,10 @@ you can create your own message sender::
237
240
}
238
241
239
242
$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>')
246
247
);
247
248
248
249
return $envelope;
0 commit comments