PHP & MySQL, Tutorials, Tools

Sending emails with PHP’s mail function is not recommended it’s not reliable and hard to debug when things go wrong. Instead use PHPMailer. This library is brilliant, dead easy to use and extendable.

To install you can either download the files from https://github.com/PHPMailer/PHPMailer and include the needed files or use composer (recommended). To install with composer create a composer.json file and put:

{
    "require": {
        "phpmailer/phpmailer": "~5.2"
    }
}

Then navigate to the project folder in your shell/terminal and enter “composer install” this will download PHPMailer into a vendor folder. Upload the folder to the server then in the file you want to use PHPMailer include composer’s auto loader:

<?php
require('vendor/autoload.php');

Now you can send an email, the following are taken directly from the example code on Github:

$mail = new PHPMailer;

$mail->From = 'noreply@domain.com';
$mail->FromName = 'Application Name';
$mail->addAddress('someone@domain.com');
$mail->isHTML(true);
$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}

That’s all that is needed to send an email using PHPMailer the if statement is not really needed you can simple have:

$mail->send();

Other options are available such as adding CC’s and BCC’s, attachments (demonstrated on the Github page).

Whilst PHPMailer is great for sending emails it won’t stop emails going into a junk folder, many things can cause this, I’ve found sending emails through Mandrill has great results in delivering emails, An added benefit is with Mandrill you can get reports on the number of emails sent and also if they have been delivered with options to resend directly from the Mandrill control panel.

To use Mandrill emails needs to be sent via SMTP, thankfully PHPMailer provides options for SMTP. 

Create an account / Sign in at http://mandrill.com/

Next go to Settings, your SMTP settings will be displayed:

Mandrill SMTP 

To get the API key click on “New API Key” enter a description (optional) then click Create. With these settings the PHPMailer script can be updated:

$mail->isSMTP();                       // Set mailer to use SMTP
$mail->Host = 'smtp.mandrillapp.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                // Enable SMTP authentication
$mail->Username = 'somone@domain.com'; // SMTP username
$mail->Password = 'your-api-key';      // SMTP password
$mail->SMTPSecure = 'tls';             // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                     // TCP port to connect to

Putting it all together:

<?php
require('vendor/autoload.php');

$mail = new PHPMailer;

$mail->isSMTP();                       // Set mailer to use SMTP
$mail->Host = 'smtp.mandrillapp.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                // Enable SMTP authentication
$mail->Username = 'somone@domain.com'; // SMTP username
$mail->Password = 'your-api-key';      // SMTP password
$mail->SMTPSecure = 'tls';             // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                     // TCP port to connect to

$mail->From = 'noreply@domain.com';
$mail->FromName = 'Application Name';
$mail->addAddress('someone@domain.com');
$mail->isHTML(true);
$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}