How to configure symfony mailer and programatically send custom email using symfony module in Drupal

Symfony mailer is the new alternative for swift mailer which longer maintained and so this module is obsolete.

Symfony mailer

Install module using below commands

composer require ‘drupal/symfony_mailer:^1.0@alpha’ 

If you have already installed mailsystem module , uninstall it.

Install symfony mailer from Extend page.

Under Configuration->System click on Mailer.

So in mailer page you can see all configurations. We will be discussing all of this one by one.

Configuring SMTP as Transport

First thing we are going to send a test mail using symphony mailer. For that we are going to set up SMTP under transport tab. Under transport tab you can see sendmail default transport type.

In order to send mail from localhost using SMTP,  select transport type as SMTP and click on Add transport button.

You can see below form, fill the fields.

Here password is not your gmail account password. From 30-May-2022, you have to set up app password for accessing gmail features from outside, see below article.

https://support.google.com/accounts/answer/185833?hl=en

After saving you can see SMTP in transport type list, 

Next make SMTP as default transport, click on edit button of SMTP, you can see set as default option, click on this. See below screenshot.

So you can see SMTP set as default transport type.

Next click on Test tab. Provide recipient list. Click on send button.

You will relieve email as below with your site name as subject.

At present this module having issues regarding add policy. see below link for more details.

https://www.drupal.org/project/symfony_mailer/issues/3271231

In Mail policy you can add multiple policy and specific fields and data.

Here we are test mail policy in below policy list.

Programmatically send custom mail using symfony mailer

Here we are creating a custom form in our custom module dn_email

A test mail will be send up on submission of the form.

So here we have the below route in our custom module.

Add below in module path  /dn_mail.routing.yml

dn_mail.contact:
 path: '/contact-form'
 defaults:
   _title: 'Send Contact'
   _form: '\Drupal\dn_mail\Form\SendContact'
 requirements:
   _access: 'TRUE'

Create form in path  /dn_mail/src/Form/SendContact.php

Form build function will look like below.

public function buildForm(array $form, FormStateInterface $form_state) {
  
  
   $form['#tree'] = TRUE;
 
   $form['recipient'] = [
     '#title' => $this->t('Recipient'),
     '#type' => 'textfield',
     '#default_value' => '',
     '#description' => $this->t('Recipient email address. Leave blank to send to yourself.'),
   ];
 
  
 
   $form['actions']['#type'] = 'actions';
   $form['actions']['submit'] = [
     '#type' => 'submit',
     '#value' => $this->t('Send'),
     '#button_type' => 'primary',
   ];
 
   return $form;
 }

Here we are accepting To email address. Upon submission sending email to this email address.

So submission part will be as be as below.

public function submitForm(array &$form, FormStateInterface $form_state) {
 
   $emailFactory = \Drupal::service('email_factory');
  
   $to = $form_state->getValue('recipient') ?: $this->currentUser();
   $emailFactory->newModuleEmail('symfony_mailer', 'test')
     ->setTo($to)
     ->send();
   $message = is_object($to) ?
     $this->t('An attempt has been made to send an email to you.') :
     $this->t('An attempt has been made to send an email to @to.', ['@to' => $to]);
   $this->messenger()->addMessage($message);
}

So this will send email with body content and subject took from Mail policy test email.

Note: In new Drupal 9 and Drupal 10 versions use newTypedEmail function instead of newModuleEmail function as below.

$emailFactory->newTypedEmail(‘symfony_mailer’, ‘test’)

->setTo($to)

->send();

Download above sample code with custom module here.

Get Free E-book
Get a free Ebook on Drupal 8 -theme tutorial
I agree to have my personal information transfered to MailChimp ( more information )
if you like this article Buy me a Coffee this will be an ispiration for me to write articles like this.

You may also like...