Skip to content

Simplified the mailer docs about a global from address #12351

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 23, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 6 additions & 34 deletions mailer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,12 @@ both strings or address objects::
// ...
;

.. tip::

Instead of calling ``->from()`` *every* time you create a new email, you can
create an :doc:`event subscriber </event_dispatcher>` and listen to the
``MessageEvent::class`` event to set the same ``From`` email to all messages.

Multiple addresses are defined with the ``addXXX()`` methods::

$email = (new Email())
Expand Down Expand Up @@ -252,40 +258,6 @@ images inside the HTML contents::
->html('<img src="cid:logo"> ... <img src="cid:footer-signature"> ...')
;

Global from Address
-------------------

Instead of calling ``->from()`` *every* time you create a new email, you can
create an event subscriber to set it automatically::

// src/EventSubscriber/MailerFromSubscriber.php
namespace App\EventSubscriber;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Mailer\Event\MessageEvent;
use Symfony\Component\Mime\Email;

class MailerFromSubscriber implements EventSubscriberInterface
{
public function onMessageSend(MessageEvent $event)
{
$message = $event->getMessage();

// make sure it's an Email object
if (!$message instanceof Email) {
return;
}

// always set the from address
$message->from('fabien@example.com');
}

public static function getSubscribedEvents()
{
return [MessageEvent::class => 'onMessageSend'];
}
}

.. _mailer-twig:

Twig: HTML & CSS
Expand Down