Transport Config & Mailtrap

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.

We've already learned quite a bit about how to customize a specific email... with a lot more coming. But how do we customize how an email is sent. In Symfony, the way that your messages are delivered is called a transport. Go back to your terminal and run:

git status

The Mailer dsn

When we installed the Mailer component, its recipe did a couple of interesting things. First, it created a new file called config/packages/mailer.yaml. Let's open up that up. Wow... as you can see: the mailer system doesn't really have a lot of config. The only thing here is the dsn: a URL that tells Mailer what server or cloud service to use for delivery. This references an environment variable called MAILER_DSN. Hey! That's the error we just saw:

Environment variable not found: "MAILER_DSN".

The recipe also modified the .env file. If you run

git diff .env

Yep! You'll see that it added a section with an example MAILER_DSN.

Configuring MAILER_DSN

Open up .env. And, at the bottom, uncomment that MAILER_DSN line. By default, this tries to send to a local SMTP server... and I definitely do not have one of those running. But... let's try it anyways. Refresh to resubmit the registration form and... boom!

Connection could not be established with host "tcp://localhost:25"

So how are we going to send emails? Because... there are a lot of different options. You could run your own SMTP server... which is not something I recommend... or register with a cloud email sender - like SendGrid - and use your connection details from them for Mailer. Mailer supports a bunch of the most famous cloud providers... as well as any cloud provider that implements SMTP... which is like... all of them. We're going to show how to use SendGrid a bit later.

Why are we not going to use SendGrid right now? Because... when you're developing and debugging your emails, there's a better option. Instead of sending real emails to a real email server, you can send them to a "fake" mailbox.

One of the most famous tools to do this is called MailCatcher. Basically, you download MailCatcher, start it on your machine, and it creates a temporary SMTP server that you can send to. But instead of delivering the messages, it holds onto them and you can view them all in a fake inbox in your browser. MailCatcher is written in Ruby and a similar tool - MailHog - is written in Go. Those are both great options.

Hello Mailtrap

But... to save me the headache of getting those running, I'm going to use a third option called Mailtrap. Head to mailtrap.io. This is basically a "hosted" version of those tools: it gives us a fake SMTP server and fake inbox, but we don't need to install anything. And it has an excellent free plan.

After you register, you'll end up in a spot like this: with a "Demo inbox". Click into that Demo inbox. On the right, you'll see a bunch of information about how to connect to this. At the time of recording, they do have specific instructions for Symfony 4... but these are for using Mailtrap with SwiftMailer, not Symfony Mailer.

No worries, setup is dead simple. The DSN follows a standard structure: username:password@server:port. Copy the username from Mailtrap, paste, add a colon, copy and paste the password, then @ the server - smtp.mailtrap.io - one more colon, and the port. We could use any of these. Try 2525.

Done! If we haven't messed anything up, our email should be delivered to our Mailtrap inbox. Let's try it! Refresh the form submit and... ah! Validation error. The last time we tried this, the email failed to send but the user was saved to the database. Make the email unique by adding a "2". Then click the terms, enter any password and... register!

Ok, no errors! Go check Mailtrap! There it is! It's got the subject, text content, but no HTML content because we haven't set that yet. There are also a couple of other cool debugging features in Mailtrap - we'll talk about some of these soon.

Now that we've got some success, it's time to attack the obvious shortcoming of this email... it's just text! It's not 1995 anymore people, we need to send HTML emails. And Mailer gives us a great way to do this: native integration with Twig. That's next.

Leave a comment!

  • 2020-05-20 Victor Bocharsky

    Hey Steve,

    Ah, yes... I remember something like this, and if I'm not wrong, we even talked about it in the video? Yes, that's limitation of environment variables itself. You can easily replace them with other chars, like "*" or brackets I suppose.

    Anyway, I'm glad you figured it out yourself!

    Cheers!

  • 2020-05-19 Steve

    In fact it does work but the password has issues with certain characters... $ and or ^ are not good.

    Thank you for the help. Have a great day

    Steve

  • 2020-05-19 Steve

    Hi Victor, thank you for help and sorry for delay, I hadn't seen the notification.

    This doesn't work unfortunately, it is something I had tried.

    Steve

  • 2020-05-15 Victor Bocharsky

    Hey Steve,

    Oh, now I see :) Yeah, unfortunately, Symfony Mailer has native integration only with the next several transports: https://symfony.com/doc/cur... . But it does not mean you can't use it for other services like Office 365. As long as they work via SMTP - it just should work. You just need to set the correct value for MAILER_DSN env var, see https://symfony.com/doc/cur.... So, you need to change the format a bit, e.g. username/password should be before the host. Try this one:


    MAILER_DSN=smtp://user:password1234@smtp.office365.com:587?encryption=tls

    Does it work for you?

    Cheers!

  • 2020-05-14 Steve

    Thanks Victor however I may not have been 100% clear, it's about using O365 as a transport option rather than attachments. In SM I can use this:

    MAILER_URL=smtp://smtp.office365.com:587?encryption=tls&username=user@domain.tld&password=password1234

    Does that make sense?

    Steve

  • 2020-05-14 Victor Bocharsky

    Hey Steve,

    Unfortunately I know very little about Office 365. Does Swiftmailer has an integration with it? Do you attach some docs directly from Office 365? Symfony Mailer allows you to attach files as attachments, so the whole doc will be sent to your addressee. And you can set that attachement via PHP resource, i.e. you don't need to get the content of the file with file_get_content() and add it to the email. Here's where we create a PDF file and attach it to the email: https://symfonycasts.com/sc... - I suppose you can do the same with your docs reading them from Office 365.

    I hope this helps!

    Cheers!

  • 2020-05-13 Steve

    HI. I've moved over to Mailer from SwiftMailer but can't find any option to use Office 365, is it possible to configure to use O365 or are hte options the ones listed in the main Symfony docs?

    Cheers

  • 2020-02-10 Diego Aguiar

    Hey Dirk J. Faber

    I'm glad to hear that you could fix your problem. Thanks for sharing your solution with others :)

    Cheers!

  • 2020-02-10 Dirk J. Faber

    I figured out what I could do differently, setting 'mail.example.com' in my hosts file under 127.0.1.1 seems to work!

  • 2020-02-10 Dirk J. Faber

    I do have a local STMP-server that I wish to send emails from. When I was using swiftmailer, I could use:
    MAILER_URL=smtp://localhost
    Now when I try MAILER_DSN=smtp://localhost, I get an error:


    Warning: stream_socket_enable_crypto(): Peer certificate CN=`mail.example.com' did not match expected CN=`localhost'


    I tried just about everything, and when using this smtp-server from another machine (and providing all required credentials) I can use Symfony Mailer. But from the same machine... no luck. Any ideas what I could do differently?

  • 2020-01-08 Victor Bocharsky

    Hey AbelardoLG,

    Ah, good job! Yeah, sounds like so... and we're happy you got it working finally! :)

    Cheers!

  • 2020-01-07 AbelardoLG

    Hi there!
    I have reinstalled my Ubuntu copy and all it worked!

    What I told you: it was a misconfiguration from my [past] Ubuntu copy. :)

  • 2020-01-01 Abelardo León González

    Hi there!

    I'm afraid it's not related to Mailtrap since I have tested it with a virtual machine and it worked (althought it was with my own email server data).
    I think is due to a misconfiguration of my Ubuntu.

    Anyway, thanks for your response. :)

    Best regards and happy New Year!

  • 2019-12-31 weaverryan

    Yo AbelardoLG!

    Hmm. Well, you were thinking about the same thing I was already - is the openssl php extension running. And yes, it appears it is ;). Because you're using Mailtrap... my guess is that your Mailtrap does not have a valid SSL certificate... which totally makes sense, as this is just a local server :). In fact, I think (?) that Mailtrap might not have a secure connection setup - https://github.com/sj26/mai.... Basically, I think (?) that Mailer should be communicating to Mailtrap in a non-secure manner. If I'm reading Mailer's code correctly, as long as you use port "25" to connect to Mailtrap, it *should* connect *without* "tls".

    If you need to do some extra debugging, I would do some "dumping and die'ing" inside this class - https://github.com/symfony/... - to see if tls is ultimately set to false or not.

    Let me know if that helps!

    Cheers!

  • 2019-12-30 Victor Bocharsky

    Hey AbelardoLG,

    Thank you for sharing this post!

    Cheers!

  • 2019-12-28 AbelardoLG

    Obviously, this is a no-related Symfony issue nor Mailer but local setting (Ubuntu)

  • 2019-12-28 AbelardoLG

    Here is another post about this service: https://blog.mailtrap.io/se...

  • 2019-12-28 AbelardoLG

    Hi there:
    When I set up my .env with my Mailtrap credentials, I got this error:
    Warning: stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages:
    error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed

    Extra set up required? :)

    I got this from Ubuntu:
    SSL Version => OpenSSL/1.1.1c
    openssl
    OpenSSL support => enabled
    OpenSSL Library Version => OpenSSL 1.1.1c 28 May 2019
    OpenSSL Header Version => OpenSSL 1.1.1d 10 Sep 2019
    Openssl default config => /usr/local/ssl/openssl.cnf
    openssl.cafile => no value => no value
    openssl.capath => no value => no value
    Native OpenSSL support => enabled

    Does anybody know if this setting is wrong? Any help would be welcome. :)

    Best regards.