Creating, Configuring & Sending the Email Object

Video not working?

It looks like your browser may not support the H264 codec. If you're using Linux, try a different browser or try installing the gstreamer0.10-ffmpeg gstreamer0.10-plugins-good packages.

Thanks! This saves us from needing to use Flash or encode videos in multiple formats. And that let's us get back to making more videos :). But as always, please feel free to message us.

Time to send... an email! After a user registers for a new account, we should probably send them a welcome email. The controller for this page lives at src/Controller/SecurityController.php... find the register() method.

This is a very traditional controller: it creates a Symfony form, processes it, saves a new User object to the database and ultimately redirects when it finishes.

Let's send an email right here: right after the user is saved, but before the redirect. How? It's gorgeous. Start with $email = (new Email()) - the one from the Mime namespace.

... lines 1 - 10
use Symfony\Component\Mime\Email;
... lines 12 - 16
class SecurityController extends AbstractController
{
... lines 19 - 46
public function register(Request $request, UserPasswordEncoderInterface $passwordEncoder, GuardAuthenticatorHandler $guardHandler, LoginFormAuthenticator $formAuthenticator)
{
... lines 49 - 51
if ($form->isSubmitted() && $form->isValid()) {
... lines 53 - 70
$em->flush();
$email = (new Email())
... lines 74 - 84
}
... lines 86 - 89
}
}

Mime & Mailer Components

Actually, this is a good moment to mention that when we talk about the Mailer component in Symfony, we're actually talking about two components: Mailer and Mime. The Mime component is all about creating & configuring the email itself and Mailer is all about sending that email. But mostly... that's not too important: just don't be surprised when you're using objects from this Mime namespace.

Configuring the Email

I've put the new Email object in parentheses on purpose: it allows us to immediately chain off of this to configure the message. Pretty much all the methods on the Email class are... delightfully boring & familiar. Let's set the ->from() address to, how about, alienmailer@example.com, the ->to() to the address of the user that just registered - so $user->getEmail() - and this email needs a snazzy subject!

Welcome to the Space Bar!

... lines 1 - 10
use Symfony\Component\Mime\Email;
... lines 12 - 16
class SecurityController extends AbstractController
{
... lines 19 - 46
public function register(Request $request, UserPasswordEncoderInterface $passwordEncoder, GuardAuthenticatorHandler $guardHandler, LoginFormAuthenticator $formAuthenticator)
{
... lines 49 - 72
$email = (new Email())
->from('alienmailcarrier@example.com')
->to($user->getEmail())
->subject('Welcome to the Space Bar!')
... lines 77 - 89
}
}

Pure poetry. Finally, our email needs content! If you've sent emails before, then you might know that an email can have text content, HTML content or both. We'll talk about HTML content soon. But for now, let's set the ->text() content of the email to:

Nice to meet you

And then open curly close curly, $user->getFirstName(), and, of course, a ❤️ emoji.

... lines 1 - 10
use Symfony\Component\Mime\Email;
... lines 12 - 16
class SecurityController extends AbstractController
{
... lines 19 - 46
public function register(Request $request, UserPasswordEncoderInterface $passwordEncoder, GuardAuthenticatorHandler $guardHandler, LoginFormAuthenticator $formAuthenticator)
{
... lines 49 - 72
$email = (new Email())
->from('alienmailcarrier@example.com')
->to($user->getEmail())
->subject('Welcome to the Space Bar!')
->text("Nice to meet you {$user->getFirstName()}! ❤️");
... lines 78 - 89
}
}

There are a bunch more methods on this class, like cc(), addCc(), bcc() and more... but most of these are dead-easy to understand. And because it's such a simple class, you can look inside to see what else is possible, like replyTo(). We'll talk about many of these - like attaching files - later.

So... that's it! That's what it looks like to create an email. I hope this "wow'ed" you... and disappointed you in its simplicity... all at the same time.

Sending the Email

Ok... so now... how do we send this email? As soon as we installed the Mailer component, Symfony configured a new mailer service for us that we can autowire by using - surprise! - the MailerInterface type-hint.

Let's add that as one of the arguments to our controller method: MailerInterface $mailer.

... lines 1 - 10
use Symfony\Component\Mailer\MailerInterface;
... lines 12 - 17
class SecurityController extends AbstractController
{
... lines 20 - 47
public function register(MailerInterface $mailer, Request $request, UserPasswordEncoderInterface $passwordEncoder, GuardAuthenticatorHandler $guardHandler, LoginFormAuthenticator $formAuthenticator)
{
... lines 50 - 92
}
}

And... what methods does this object have on it? Oh, just one: $mailer->send() and pass this $email.

... lines 1 - 10
use Symfony\Component\Mailer\MailerInterface;
... lines 12 - 17
class SecurityController extends AbstractController
{
... lines 20 - 47
public function register(MailerInterface $mailer, Request $request, UserPasswordEncoderInterface $passwordEncoder, GuardAuthenticatorHandler $guardHandler, LoginFormAuthenticator $formAuthenticator)
{
... lines 50 - 52
if ($form->isSubmitted() && $form->isValid()) {
... lines 54 - 73
$email = (new Email())
->from('alienmailcarrier@example.com')
->to($user->getEmail())
->subject('Welcome to the Space Bar!')
->text("Nice to meet you {$user->getFirstName()}! ❤️");
$mailer->send($email);
... lines 81 - 87
}
... lines 89 - 92
}
}

I love how this looks. But... will it work? We haven't actually configured how emails should be sent but... ah, let's just see what happens. Move over and register: first name Fox (last name, Mulder, in case you're wondering), email: thetruthisoutthere@example.com, any password, agree to the terms that we definitely read and, register!

Ah! Error!

Environment variable not found: MAILER_DSN

Ok, fine! To actually deliver emails, we need to add some configuration via this environment variable. Let's talk about that next... including some awesome options for debugging emails while you're developing.

Leave a comment!

  • 2020-02-07 weaverryan

    Hey Niki!

    Hmm. Let's see if we can figure this out!

    > And I cannot make the mailer dsn because its shows that it is invalid

    What do you mean by "it shows that it is invalid". Do you get an error when you try to send an email? Something else? I thought maybe the extra @ symbol in the username might cause problems with parsing the DSN (since there is another @ symbol later). So, I tried it! I added this to my .env:

    MAILER_DSN=smtp://myemailaddress@mywebsite.com:myemailpassword@mail.mywebsite.com

    Obviously, that fake address won't *really* work. But I *was* able to verify that it parsed all the pieces correctly: https://imgur.com/a/XfWpvle

    So, what exactly are you seeing with your site?

    Cheers!

  • 2020-02-07 Niki

    I used (.) for formatting to avoid link transformation here its only a dot.
    The smtp credetials which cpanel sents when i created the email inbox are:
    Smtp: mail.mywebsitename.com
    Username: myemailaddress@mywebsite.com
    Password: the inbox’s password

    And I cannot make the mailer dsn because its shows that it is invalid

    MAILER_DSN=smtp://myemailaddress@myweb...:myemailpassword@mail.mywebsite.com

    So i just wonder for the mailer dsn syntax for using cpanel email

  • 2020-02-07 Victor Bocharsky

    Hey Niki,

    Hm, let me clarify, you have 2 *valid* email addresses and you want to send emails from both? Like some emails send with smtp://myemailaddress@mywebsite(.)com and some emails send with myemailpassword@mail.mywebsite(.)com? Also, cPanel gives you the credentials with "(.)" in the domain? I suppose it's not a valid config and it should be "myemailaddress@mywebsite.com". Though, if you're talking about some cloud hosting that host this and they have support -I'd recommend you to write to them and ask to clarify what credentials you should use for the info that is given to you by cPanel.

    Cheers!

  • 2020-02-06 Niki

    Hello,
    How can I configure cPanel-generated email to mailer dsn because I tried the following as dsn and it's not working:
    MAILER_DSN=smtp://myemailaddress@mywebsite(.)com:myemailpassword@mail.mywebsite(.)com