
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.
Environment
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
Assumptions
- 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
Note
This will create two files, "composer.json", "composer.lock" and a subdirectory, "vendor/".
Create PHP Mailer
$ nano mailer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?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__);
$dotenv->load();
$dotenv->required(['MAILER_PREFIX','MAILER_PWD','MAILER_SUFFIX']);
$dotenv->required('MAILER_PREFIX')->notEmpty();
$dotenv->required('MAILER_PWD')->notEmpty();
$dotenv->required('MAILER_SUFFIX')->notEmpty();
$dsn = $_ENV['MAILER_PREFIX'] . urlencode($_ENV['MAILER_PWD']) . $_ENV['MAILER_SUFFIX'];
$transport = Transport::fromDsn($dsn);
$mailer = new Mailer($transport);
$email = (new Email())
->from('Tester' . '<' . 'tester@example.com' .'>')
->to('hello@example.com')
->subject('Contact Form Submission')
->text('Hello world, this is a test of the PHP email sender.');
$mailer->send($email);
?>
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 <>tester@example.com>").
20 - Set email message "to" value (e.g. "hello@example.com").
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.
Note
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
MAILER_PWD="enter-password"
MAILER_PREFIX="smtp://username@example.com:"
MAILER_SUFFIX="@mail.example.com:465"
Note
Enclose the variables in double quotes to avoid the value being truncated.
Note
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="@mail.example.com:465?verify_peer=0"), 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.
References
- Composer
- Data source name, Wikipedia
- Eamil, Wikipedia
- PHP dotenv project
- PHP: Hypertext Preprocessor
- PHP, Wikipedia
- Send Email SVG, SVG Repo
- Send emails with Mailer, Symfony Docs
- Simple Mail Transfer Protocol, Wikipedia
- Symfony logo
- Symfony Mailer component
- symfony/mailer, Packagist
- Transport Layer Security, Wikipedia
- vlucas/phpdotenv, Packagist
Changelog
-
- change topic
-
- change 100DaysToOffload message