Tuesday, March 14, 2017

Symfony Framework: Sending Email with Swift Mailer

In any web application sending an email is common task at least when a contact us form is submitted by your website visitor and filled the contact us form.

This short tutorial explains sending an email message to website owners with the content filled in the website contact us form. We use a twig HTML template to send email. Email sent using Google GMail mail server.  The code is built with Symfony 3.2, PHP 7.


First thing first, we assume that the contact us form is already available and have a mechanism to submit the form to your backend. If not, please go ahead and built that first and come back to this tutorial.

1. Setup email server configuration

When we need to send email through application, we need to setup the following parameters. If you don't have these parameters, get them first. If you are using gmail, make sure you enable "access to less secure apps" from your google account settings. When working with symfony, you may configure these parameters in app/config/parameters.yml file.

mailer_transport: gmail
mailer_host:
mailer_user:
mailer_password:

2. Implement a service called MailerService as shown in the below gist

This MailerService is created as utility service to be used by rest of the application. This service takes template engine and mailer as parameters. These parameters are used to render a HTML template to send email and mailer to actually send an email. The MailerService will be injected into other services where email sending is required. The service is configured in services.yml for other services to reuse and inject into them.



From the above code snippet, we can see that  $templating and $mailer are injected through constructor. We have added the corresponding use statements above the class definition.

In the sendEmail method, we first create an instance of the message object and then build it with subject, from, to and message body. As we can see, the message body is constructed by rendering the twig template(shown below) and passing the required parameters to embed into the template.

Finally, invoke the mailer's send method by passing the message object to send the message.


3. Implement HTML twig template used  to format and send a meaningful email



As one can see in the above template we embed the contact us form parameters into the email, which are passed as an array of parameters.

4. Provide configuration for the MailerService in service.yml file as shown below


    app.mailer.service:
        class: AppBundle\Util\Services\MailerService
        arguments: ['@templating', '@mailer']

5.  Inject MailerService into other service or controller that processes contact form submission

Assuming that we have ContactUsService, which processes the contact form submission and sending emails, below is the services.yml configuration to inject mailer service into ContactUsService.

app.cotactus.service:
        class: AppBundle\Contactus\Services\ContactUsService
        arguments: ['@app.mailer.service']

6. Invoking the MailserService to send emails

Below is the code snippet to use MailerService and send emails