Tutorial PHP Mailer
Tutorial PHP Mailer
com/phpmailer-tutorial/
This complete guide (updated in 2018) will teach you how to setup and run the most used PHP email
library: PHPMailer.
Learning how to send emails will take your PHP code to the next level. You will be able to write an alert
daemon, receive an email when someone tries to log in and much more.
This tutorial covers all you need to know: the installation steps, the class functions, how to use SMTP
authentication (including Gmail), how to handle errors and more. You will also find many code examples.
(You probably want to add this page to your Bookmarks to come back here for reference.)
PHPMailer offers many functionalities over the PHP core function mail(), including SMTP SSL encryption
and authentication, HTML messages, file attachments and more.
On top of that, PHPMailer is much easier to use than mail() and provides a clean, readable object-oriented
syntax.
Let’s see in more detail why you should use PHPMailer instead of mail() or other third-party libraries.
1 of 22 9/24/2018, 5:38 AM
PHPMailer complete tutorial (2018): send emails with PHP https://alexwebdevelop.com/phpmailer-tutorial/
The first is that mail() relies on the server email subsystem to work. This means that if you want to change
some settings like the SMTP server or the authentication parameters, you need to do it system wide.
That is usually a quite difficult operation, and unless you have a dedicated server or you are using a local PHP
development environment, you are probably not even allowed to do it.
This also makes almost impossible to use different configurations at the same time, for example using multiple
SMTP accounts and switching between them programmatically.
As you will see in a minute, with PHPMailer is very easy to change any parameter dynamically right in your
PHP script.
The second reason is that mail() doesn’t offer any advanced functionality.
mail() is fine for sending simple, plain text emails, but it’s very limiting if you need to do anything more than
that. Adding attachments or sending HTML emails, for example, is very difficult with mail(), while with
PHPMailer it’s just a matter of a single line of code.
Yes, there are other libraries like Zend Mail, SwiftMailer and Zeta Components Mail, but PHPMailer is usually
the first choice because of its popularity.
Of course, if you are already familiar with another mail extension and it works fine for you, you can just stick
with it.
But if you want to start using one of them and you need to choose which one to go with, PHPMailer is
probably the best choice because it’s the most used one.
Other extensions like Zend Mail, SwiftMailer or Zeta Components Mail are probably as good as PHPMailer,
but look at the Google search results for “php mail library”:
2 of 22 9/24/2018, 5:38 AM
PHPMailer complete tutorial (2018): send emails with PHP https://alexwebdevelop.com/phpmailer-tutorial/
3 of 22 9/24/2018, 5:38 AM
PHPMailer complete tutorial (2018): send emails with PHP https://alexwebdevelop.com/phpmailer-tutorial/
As you can clearly see, PHPMailer dominates the first results. That doesn’t necessarily mean PHPMailer is
better than other extensions, but it does mean it’s the most popular.
The main reason you want to go with the most used library is support: the more widely used a piece of
software is, the easier is to find help and examples online.
I should also mention that I have been using PHPMailer for work for a few years now, sending up to 100
emails per day with it. I use it to send alerts using PHP daemons, to monitor my scripts’ memory usage and to
receive warnings if SQL injection attacks are detected.
As far as I remember, I never had any trouble with it, so chances are you won’t either.
Starting from version 6, however, this file is no longer provided. Now, to make the PHPMailer class available
in your script you have two options:
The first option is the recommended one, because Composer takes care of all the download, update and
dependency check steps. This is especially useful if you need to install other libraries as well, like the one
needed for XOAUTH2 Google authentication (I will tell you how to use XOAUTH2 with Google later in this
post).
4 of 22 9/24/2018, 5:38 AM
PHPMailer complete tutorial (2018): send emails with PHP https://alexwebdevelop.com/phpmailer-tutorial/
However, the second option may be useful if you don’t want to install Composer for some reason, for example
if you are using PHPMailer on a testing environment.
Let’s see both installation options, starting with the one with Composer.
Installing Composer and PHPMailer on Windows (if you use XAMPP, WAMP etc.)
Composer is a dependency manager for PHP. It helps you download, install and keep up to date PHP
extensions and libraries.
1. first, make sure you have a web development environment already installed (XAMPP, WAMP, EasyPHP
etc.) as Composer needs a PHP executable to work;
2. download the Composer installation file from here (under “Windows Installer”) and run it;
3. follow the installation instructions, and make sure you can select a PHP executable:
4. once the installation is complete, you will be able to use the Composer command line tools to install
PHPMailer.
Now you need to open a terminal (by executing “cmd.exe” or looking for “Command prompt” in the Start
menu) and navigate to the directory where you want to install the Composer packages including PHPMailer (if
you need help using the terminal just leave me a comment below).
For example, let’s use “C:\xampp\composer” as installation directory. First create the directory, then go back
to the terminal and move into the directory by typing “cd C:\xampp\composer”.
Then, simply execute the command: “composer require phpmailer/phpmailer” as shown in the following
image:
5 of 22 9/24/2018, 5:38 AM
PHPMailer complete tutorial (2018): send emails with PHP https://alexwebdevelop.com/phpmailer-tutorial/
Composer will generate an “autoload.php” file you can use to include the installed libraries, in this case
PHPMailer. This file is located under the “vendor” directory by default, although you can configure Composer
to use a different directory name.
So, assuming your installation directory is “C:\xampp\composer”, you need to include the “C:\xampp
\composer\vendor\autoload.php” file.
Also, because now PHPMailer defines its classes under the PHPMailer\PHPMailer namespace, it’s a good
idea to make use of the use directive at the beginning of your script, aliasing the PHPMailer\PHPMailer
\PHPMailer and PHPMailer\PHPMailer\Exception classes:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'C:\xampp\composer\vendor\autoload.php';
On Debian-like systems (including Ubuntu), for example, you can simply use the “apt-get” command to
install Composer (installing Composer from the source code is outside the scope of this guide, but if you need
help with it feel free to ask me).
Once Composer is installed, you can use the Composer command line executable to install PHPMailer inside
a directory of your choice, just like we already saw for the Windows installation (you can use the very same
6 of 22 9/24/2018, 5:38 AM
PHPMailer complete tutorial (2018): send emails with PHP https://alexwebdevelop.com/phpmailer-tutorial/
command):
You can include the “autoload.php” file and set the namespace aliases just like for the Windows installation
(changing the file path, of course).
If you prefer not to use Composer, you can just download the PHPMailer source files and include the required
files manually.
You can download the ZIP file with the source code from the PHPMailer homepage, clicking on the “Clone or
download” green button (on the right) and then selecting “Download ZIP”. Unzip the package inside the
directory where you want to save the source files.
Then you just need to include the needed classes files in your PHP script.
As a minimum, you want to include the main PHPMailer class, the Exception class (for error handling) and
probably the SMTP class too, if you are going to connect to an SMTP server for mail delivery.
You should also set the namespace aliases just like you saw before for the Composer installation.
Assuming your source code directory is “C:\PHPMailer”, this is how your PHP script will look like:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'C:\PHPMailer\src\Exception.php';
require 'C:\PHPMailer\src\PHPMailer.php';
7 of 22 9/24/2018, 5:38 AM
PHPMailer complete tutorial (2018): send emails with PHP https://alexwebdevelop.com/phpmailer-tutorial/
require 'C:\PHPMailer\src\SMTP.php';
If you choose the Composer installation method, you can keep your PHPMailer installation up to date by
executing the “update” Composer command.
The syntax is the same on both Windows and Linux. Just go to the installation directory (where you issued the
installation command) and execute the update:
If you choose not to use Composer, then you will need to manually download the new ZIP file from the
PHPMailer homepage and overwrite the previous installation (following the same steps).
In either case, before updating your installation is a good idea to look at the changelog file and the update
notes, just to be sure that your PHP applications are compatible with the new PHPMailer version.
Let’s see a basic example so you can understand how this class works (don’t worry about the details for now,
we will cover them in the next chapter).
In all the examples I will use the Windows installation path, because I am working on my local XAMPP
development environment. If you are using Linux just change the include paths accordingly.
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'C:\xampp\composer\vendor\autoload.php';
try {
$mail->addAddress('[email protected]', 'Emperor');
$mail->Subject = 'Force';
8 of 22 9/24/2018, 5:38 AM
PHPMailer complete tutorial (2018): send emails with PHP https://alexwebdevelop.com/phpmailer-tutorial/
$mail->send();
}
catch (Exception $e)
{
echo $e->errorMessage();
}
catch (\Exception $e)
{
echo $e->getMessage();
}
The class’ constructor argument is set to TRUE to make PHPMailer use exceptions for error reporting.
If you prefer not to use exceptions, just omit the argument (or set it to FALSE). In this case you need to check
the send() method return value and look for the error message in the $ErrorInfo attribute:
<?php
use PHPMailer\PHPMailer\PHPMailer;
require 'C:\xampp\composer\vendor\autoload.php';
$mail->addAddress('[email protected]', 'Emperor');
$mail->Subject = 'Force';
if (!$mail->send())
{
echo $mail->ErrorInfo;
}
It’s interesting to see how this script would look like if you were using the mail() function:
<?php
9 of 22 9/24/2018, 5:38 AM
PHPMailer complete tutorial (2018): send emails with PHP https://alexwebdevelop.com/phpmailer-tutorial/
In the previous basic example, we did not tell PHPMailer to use a specific SMTP server. In this case
PHPMailer uses the server email subsystem, just like mail() does.
Of course, you want to use your own SMTP connection parameters instead. Don’t worry: we’ll cover this in a
minute.
Now let’s see how to use all the PHPMailer class functionalities.
Would you like to talk with me and other developers about PHPMailer, PHP and web development? Join my
Facebook Group now: Alex PHP café
This is probably the first thing you want to do. This method takes the sender address as first parameter,
while the second parameter is optional and sets the sender name as will be seen by the recipients.
$mail->addAddress('[email protected]', 'Emperor');
$mail->Subject = 'Force';
10 of 22 9/24/2018, 5:38 AM
PHPMailer complete tutorial (2018): send emails with PHP https://alexwebdevelop.com/phpmailer-tutorial/
The message body can include plain text and HTML code. If you want your email to be parsed as
HTML, be sure to read the next point.
$mail->isHTML(TRUE);
$mail->Body = '<html>There is a great disturbance in the <strong>Force</strong>.</html>';
$mail->AltBody = 'There is a great disturbance in the Force.';
Add an attachment
You can attach files to your email using the addAttachment() function.
This function takes two parameters: the first is the file path, and the second (optional) is the file name
that the recipient will see. If not set, the same file name of the local file will be used.
Note that the file must be readable by the user running the PHP script (this is especially important under
Linux systems).
$mail->addAttachment('/home/darth/star_wars.mp3', 'Star_Wars_music.mp3');
You can specify which address the recipient will reply to when replying to your email. The default is the
sender (“From:”) address, but you can change it.
All CC (carbon-copy) and BCC (blind carbon-copy) recipients will receive the email, but BCC addresses
will be invisible to other recipients.
In the example below, we attach two files: the first contains the data from a “blob” (binary) SQL field
11 of 22 9/24/2018, 5:38 AM
PHPMailer complete tutorial (2018): send emails with PHP https://alexwebdevelop.com/phpmailer-tutorial/
and is named “db_data.db”, the second is created from a network stream (a PDF file) and is named
“file.pdf”.
$mysql_data = $mysql_row['blob_data'];
$mail->addStringAttachment($mysql_data, 'db_data.db');
$pdf_url = 'http://remote-server.com/file.pdf';
$mail->addStringAttachment(file_get_contents($pdf_url), 'file.pdf');
$mail->msgHTML(file_get_contents('contents.html'), __DIR__);
Learn how to use PHPMailer with this complete 2018 tutorial Click to Tweet
This is incredibly useful compared to mail(), which relies on the underlying system configuration instead.
The following example shows how you can use a specific SMTP server using SSL and authentication:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'C:\xampp\composer\vendor\autoload.php';
try {
$mail->isSMTP();
$mail->Host = 'smtp.empire.com';
$mail->SMTPAuth = TRUE;
12 of 22 9/24/2018, 5:38 AM
PHPMailer complete tutorial (2018): send emails with PHP https://alexwebdevelop.com/phpmailer-tutorial/
$mail->SMTPSecure = 'tls';
$mail->Username = '[email protected]';
$mail->Password = 'iamyourfather';
$mail->Port = 587;
$mail->send();
}
catch (Exception $e)
{
echo $e->errorMessage();
}
catch (\Exception $e)
{
echo $e->getMessage();
}
Of course, you need to use the correct connection parameters, including the server encryption capabilities and
the TCP port it listens to.
A more advanced SMTP related attribute is the SMTPOptions array. If you are using SMTP encryption,
PHPMailer passes this array to the underlying PHP stream_context_create() function.
You can use the SMTPOptions attribute to set advanced SSL context options. While you won’t need to use it
most of the time, in some cases it can be really useful.
For example, if you have your own SMTP server which uses a self-signed certificate, the SMTP connection
will probably fail for the lack of a valid certification authority signature. This is a quite common scenario in
work environments.
In these cases, you can use the SMTPOptions attribute to tell PHPMailer (and the PHP SSL subsystem) to
ignore this issue.
In the following example we set the verify_peer and verify_peer_name parameters to FALSE and
the allow_self_signed parameter to TRUE in order to disable some security checks:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'C:\xampp\composer\vendor\autoload.php';
try {
$mail->isSMTP();
$mail->Host = 'smtp.empire.com';
13 of 22 9/24/2018, 5:38 AM
PHPMailer complete tutorial (2018): send emails with PHP https://alexwebdevelop.com/phpmailer-tutorial/
$mail->SMTPAuth = TRUE;
$mail->SMTPSecure = 'tls';
$mail->Username = '[email protected]';
$mail->Password = 'iamyourfather';
$mail->Port = 587;
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
$mail->send();
}
catch (Exception $e)
{
echo $e->errorMessage();
}
catch (\Exception $e)
{
echo $e->getMessage();
}
Why don’t you look at a practical video example? This nice video from Mr. Digital shows how to create a
PHPMailer script:
The best way to handle errors is to use a try/catch block to catch the exception, as shown in the previous
examples. The PHPMailer/Exception class provides the errorMessage() method to retrieve the error message
directly from the exception object.
SMTP DEBUG
You can set the SMTPDebug attribute to enable on screen SMTP connection debugging.
This kind of debugging output can be extremely useful in the development phase, because it lets you see if
something goes wrong with the SMTP connection.
Note that the SMTPDebug output should be used for debugging purposes only, because it can expose sensitive
data to remote clients.
The SMTPDebug argument can be set to an integer value between 0 (the default, no output is generated) and 4
(low-level info output is displayed). If your emails are not being sent, I suggest you set this value to 4 and see
what happens:
<?php
14 of 22 9/24/2018, 5:38 AM
PHPMailer complete tutorial (2018): send emails with PHP https://alexwebdevelop.com/phpmailer-tutorial/
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'C:\xampp\composer\vendor\autoload.php';
try {
$mail->SMTPDebug = 4;
$mail->send();
}
catch (Exception $e)
{
echo $e->errorMessage();
}
catch (\Exception $e)
{
echo $e->getMessage();
}
PHPMailer supports error messages localization. You can use one of the many (48 at the moment of writing)
already available translations (you can find the list here) or even create a customized language set by yourself.
$mail->setLanguage('it');
$mail->setLanguage('klingon', 'C:\PHPMailer\myLanguages\');
If you want to use your own translation set, then you need to pass the translation file path to the function as
second parameter (with a trailing directory separator). If you are using one of the default ones, then leave the
second parameter empty.
I hope you are enjoying this guide! Why don’t you share it with your friends?
It’s just 1 second of your time and you’ll make me happy!
15 of 22 9/24/2018, 5:38 AM
PHPMailer complete tutorial (2018): send emails with PHP https://alexwebdevelop.com/phpmailer-tutorial/
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
SMTP servers usually require clients to authenticate themselves with username and password before they are
allowed to send messages. Google’s SMTP does too, but you may need to take additional steps to make it
work.
Google’s SMTP authentication steps with PHPMailer depend on whether you have Google two-step
verification enabled or not.
If you don’t have Google two-step verification enabled, you can try using your own username (in this case,
your email address) and password to login:
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Username = '[email protected]';
$mail->Password = 'mypassword';
That *may* work, however Google is very suspicious about unusual log in attempts and may refuse to
authorize your PHPMailer script authentication process. This is also explained in this PHPMailer’s
Troubleshooting document.
Google’s SMTP may ask the “client” (in this case, the PHPMailer script) to log in with the web browser first,
or to follow this page‘s instructions.
If you don’t want to enable 2-step verification, you probably must tell Google to “Allow less secure apps”
login attempts for the PHPMailer’s authentication to be accepted.
To do that you can follow these instructions, or just go here while logged in with the Google account you want
to use.
After you have authorized “less secure apps” connections, you can try again and see if it works (you may need
to wait for a couple of hours for the change to take effect).
16 of 22 9/24/2018, 5:38 AM
PHPMailer complete tutorial (2018): send emails with PHP https://alexwebdevelop.com/phpmailer-tutorial/
If it still doesn’t work, you are left with two options: enable the 2-step verification and follow the instructions
below or use the XOAUTH2 authentication process as described later in this tutorial.
Your choice
If you enabled two-step verification then you need to create an “App password” and use it to log in with
PHP, because you won’t be able to use the second authentication factor with PHPMailer (which involves using
an authentication app or authorizing the access with your smartphone).
An “app password” is a string you can use to login without the second authentication factor. To create one, just
follow Google’s instructions at this page or just open this link.
From there, choose a name for your app (for example, “PHPMailer”) and press the “Generate” button as seen
in this image:
After pressing the button, you will obtain a 16-chars string you can use to log in. Be sure to copy it right away,
because you won’t be able to retrieve it later (if you lose it, you will need to create a new app password from
scratch):
17 of 22 9/24/2018, 5:38 AM
PHPMailer complete tutorial (2018): send emails with PHP https://alexwebdevelop.com/phpmailer-tutorial/
Now all you need to do is use this new string as password in your PHPMailer configuration (without spaces,
those are there just for readability):
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Username = '[email protected]';
$mail->Password = 'eqwvrwlbbizcsdcz';
XOAUTH2 is Google’s recommended authentication method. While it’s a quite complex protocol, thanks to
PHPMailer is possible to use it without much difficulty.
PHPMailer’s XOAUTH2 implementation relies on the league/oauth2-client library, that must therefore be
installed on your system. Specifically, you need to install the league/oauth2-google package.
The easiest way to install it is to use Composer. If you chose to install PHPMailer with Composer, then you can
do the same with the league/oauth2-google library: just go to the Composer installation directory and type
“composer require league/oauth2-google”:
18 of 22 9/24/2018, 5:38 AM
PHPMailer complete tutorial (2018): send emails with PHP https://alexwebdevelop.com/phpmailer-tutorial/
You can also install this library inside another directory, but in this case you will need to include two
autoloader.php files. If you didn’t use Composer to install PHPMailer, just install the OAUTH2 library in a new
directory.
Unfortunately, installing league/oauth2-google without Composer is a bit difficult, because it requires a few
libraries to be installed as well. If you really hate Composer and don’t want to use it, you will need to download
them all manually and include them in your scripts.
Once you have league/oauth2-google installed, you will need to perform some steps with the Google account
you are going to use.
All these steps are explained in detail in this PHPMailer XOAUTH2 tutorial. Follow all the steps but stop
before the last “Configure your email script” paragraph, because it’s outdated and the example code there
no longer works.
Keep in mind that you will need to have a working development environment, because you will need to edit a
PHPMailer file (get_oauth_token.php) and run it with a web browser.
After you have completed all the required steps, you will have three authentication strings:
1. the Client ID
2. the Client Secret
3. and the Refresh Token
Next you need to configure the PHPMailer script, include the OAUTH2 library and set the authentication
parameters. The required steps are:
1. set namespace aliases for the PHPMailer OAuth and league Google classes;
2. include the league library (if you used the same Composer installation directory then the autoloader.php
file will take care of that);
3. set the script time zone to UTC; this is needed for proper time synchronization with the Google server;
4. set the PHPMailer class AuthType attribute to “XOAUTH2”;
19 of 22 9/24/2018, 5:38 AM
PHPMailer complete tutorial (2018): send emails with PHP https://alexwebdevelop.com/phpmailer-tutorial/
5. create a league/Google object and a PHPMailer/OAuth object with the authentication strings you got
in the previous steps and use them for the authentication process.
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\OAuth;
use League\OAuth2\Client\Provider\Google;
require 'C:\xampp\composer\vendor\autoload.php';
date_default_timezone_set('Etc/UTC');
$google_email = '[email protected]';
$oauth2_clientId = 'CliendIdString';
$oauth2_clientSecret = 'ClientSecret';
$oauth2_refreshToken = 'RefreshToken';
try {
$mail->Host = 'smtp.gmail.com';
$mail->AuthType = 'XOAUTH2';
$mail->setOAuth(
new OAuth(
[
'provider' => $provider,
'clientId' => $oauth2_clientId,
'clientSecret' => $oauth2_clientSecret,
'refreshToken' => $oauth2_refreshToken,
'userName' => $google_email,
20 of 22 9/24/2018, 5:38 AM
PHPMailer complete tutorial (2018): send emails with PHP https://alexwebdevelop.com/phpmailer-tutorial/
]
)
);
$mail->send();
}
catch (Exception $e)
{
echo $e->errorMessage();
}
catch (\Exception $e)
{
echo $e->getMessage();
}
With this tutorial, you have learned all you need to know to use PHPMailer at its full potential: how to install it,
how to use its functionalities, how to configure SMTPS connections (including Gmail’s), error handling and
more.
Feel free to bookmark this page and return here for reference as needed.
If you have any issue, check the PHPMailer Troubleshooting guide or just leave me a comment below. You are
also invited to join my Facebook Group (Alex PHP café) where me and my community will be very glad to
help you!
Now, I’m curious: how do YOU use email in your web applications? I’m looking forward to hearing from
you!
P.s. If this guide has been helpful to you, please spend a second of your time to share it… thanks!
21 of 22 9/24/2018, 5:38 AM
PHPMailer complete tutorial (2018): send emails with PHP https://alexwebdevelop.com/phpmailer-tutorial/
Get the FREE PDF now along with blog news delivered directly to your inbox!
22 of 22 9/24/2018, 5:38 AM