Symfony 4.3 sends emails by SendGrid

Install composer

xiaoyu chen
2 min readDec 2, 2019

Normally, the Mailer component is not installed by default, so let’s install it. Open your terminal, then type a command like this “composer require symfony/mailer”

Creating

Assuming I want to send a confirmation email to the user when they register. Let’s head to your RegistrationController (the controller name is different, no worries), and find your register function. About the logical part, you want to send an email to the user when you validate all registration information. But all depend on you where you want to put it.

First, introducing MailerInterface and then new a TemplatedEmail instance, after that setting necessary properties like ‘from to subject content’, finally call the method to send it to the user. It looks pretty easy, right.

Another thing I want to talk about is htmlTemplate(). In this email.html.twig, you may not use the framework like bootstraps anymore, but you can write by yourself. Besides, if you want to use CSS style or images, remember using absolute_url.

SendGrid Account

Heading to your account->Setup Guide, click Intergreate using our Web API or SMTP relay-> choose SMTP Relay, type your API key name->Create Key, and then copy your password (a longer string).

Just leave them there, go to Symfony .env file, type below in your .env file.

MAILER_DSN=smtp://PasteYourPasswordHere@sendgrid

After that, heading back to SendGrid, tick ‘I’ve updated my settings.’ and click ‘Next: Verify Integration’.

Run your Symfony project, and register a new account. Then heading to SendGrid, if there is no error showing in your Symfony, just click ‘Verify Integration’.

Wow, cool. Check your mailbox, if you can’t find it, dig out your spam. (If using google mail, type in: spam)

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

xiaoyu chen
xiaoyu chen

No responses yet

Write a response