Symfony logo with text beside it that says, Symfony. On the next line a send email icon with text that reads, Mailer.

Send Email Messages Using Symfony Mailer

  • Adam Douglas

It is quite often a common practice now to see many websites with a contact form that will send the message to the recipient via email. What is not always clear is how to achieve this. My intent here is to demonstrate how to send email messages using the Symfony mailer component without using the Symfony framework. To keep things simple the PHP code will be run via the command line and use SMTP (simple mail transfer protocol) for the mail transport. I will not be covering how to create the HTML form, how to capture errors/exceptions, but rather focus on the just the act of sending an email message using PHP.


Tested using the following…

  • Arch Linux x86_64
  • Composer v2.4.4
  • GNU bash v5.1.16
  • PHP v8.0.0
  • PHP Dotenv v5.5
  • Symfony mailer v5.4


  • General understanding of how email operates
  • General understanding of using a Linux terminal (command-line interface)
  • General understanding of PHP and Composer
  • Steps prefixed with a “$” (dollar sign) represents the CLI prompt
  • The text after the “$” is to be entered at the CLI
  • PHP is already installed and configured
  • Composer is already installed and configured
  • Have a test email account login credentials

Create Project Directory

This directory will be used to develop our simple email message sender.

$ mkdir symfony-mailer-example

Change to the project directory.

$ cd symfony-mailer-example

Install Dependencies

$ composer require symfony/mailer
$ composer require vlucas/phpdotenv


This will create two files, "composer.json", "composer.lock" and a subdirectory, "vendor/".

Create PHP Mailer

$ nano mailer.php
require_once 'vendor/autoload.php';
use Symfony\Component\Mailer\Mailer;
use Symfony\Component\Mailer\Transport;
use Symfony\Component\Mime\Email;
use Dotenv\Dotenv;

$dotenv = Dotenv::createImmutable(__DIR__);

$dsn = $_ENV['MAILER_PREFIX'] . urlencode($_ENV['MAILER_PWD']) . $_ENV['MAILER_SUFFIX'];
$transport = Transport::fromDsn($dsn);
$mailer = new Mailer($transport);
$email = (new Email())
    ->from('Tester' . '<' . '' .'>')
    ->subject('Contact Form Submission')
    ->text('Hello world, this is a test of the PHP email sender.');

Line by line code explanation.

01 - PHP opening tag, signals the PHP parser to look at code between "<?php" and "?>".
02 - Automatically load require libraries.
03 - Use Symfony mailer component.
04 - Use Symfony mailer transport component.
05 - Use Symfony mailer email mime component.
06 - Use Dotenv component.
08 - Initiate Dotenv.
09 - Load dotenv files.
10 - Set required variables.
11 - Require variable to be not empty.
12 - Require variable to be not empty.
13 - Require variable to be not empty.
15 - Set the data source name schema (e.g. protocol://username:password@mailhostname:port). Password is URL encoded.
16 - Set mail transport.
17 - Create mailer object.
18 - Create email object.
19 - Set email message "from" value (e.g. "Tester <>>").
20 - Set email message "to" value (e.g. "").
21 - Set email message "subject" (e.g. "Contact form Submission").
22 - Set email message "text body" (e.g. "Hello world, this is a test of the PHP email sender.").
23 - Send email message.
24 - Close PHP tag.


Before the password is used, it will be URL encoded to ensure compatibility. See RFC 3986 for reserved characters.

Create Configuration File

This configuration file is used by Symfony mailer to authenticate to the email server in order to send an email message. It is best practice to store this information separately to help avoid people stealing it.

$ nano .env


Enclose the variables in double quotes to avoid the value being truncated.


TLS (SSL) peer verification is done by default. This can be disabled by adding "?verify_peer=0" to the end of `MAILER_SUFFIX` (e.g. MAILER_SUFFIX=""), though this is not advisable.

Project Directory

The contents of the project directory should now have the following files.

├── .env
├── composer.json
├── composer.lock
├── mailer.php
└── vendor/

Run The Code

At this point if everything was entered correctly the code should be able to run successfully. As successful run will return the prompt and not display a message in the terminal. Check your email inbox for the sent email message.

$ php mailer.php

Git Repository

For a fast initial setup, you may use the git repository I created, symfony-mailer-example.

This is post 42 of 100, and is round 2 of the 100 Days To Offload challenge.

    • change topic
    • change 100DaysToOffload message