PHPmailer overview

PHPmailer is a fully-featured email creation and transport class for PHP. Visit PHPMailer’s GitHub page for further details on what it has to offer.

How to download PHPMailer

There are two options to download:

  • Download from the GitHub page, or
  • Run commands via SSH.

Downloading PHPMailer and uploading via SFTP

  1. Download the PHPMailer files from Github. You can download a ZIP file from that page.
  2. Unzip the file on your computer.
    A folder named ‘PHPMailer-master’ is unzipped.
  3. In that folder, use an SFTP client to copy the following files to your site’s directory:
    • class.phpmailer.php
    • class.smtp.php
    • class.pop3.php
    • PHPMailerAutoload.php.

Downloading PHPMailer via SSH

  1. Download the PHPMailer files from Github.
  2. Run the following command via SSH in your site’s directory:
    [server]$ wget https://github.com/PHPMailer/PHPMailer/archive/master.zip
    
  3. Unzip the file, and then run the following to unzip:
    [server]$ unzip master.zip
    
    A folder named ‘PHPMailer-master’ is unzipped.

Basic code example

Insert the following code into a PHP file. Make sure to adjust the first line to the location of your Autoload.php file. Also, adjust the settings in bold to match your mail server and email address credentials.

In the past, it was possible to use mail.example.com to connect (where example.com is your actual domain name). Due to how modern mail clients handle security, this is now NOT recommended as it may throw connection errors. Please ensure you are only using your mail cluster servername when connecting.

<?php
/**
* This example shows making an SMTP connection with authentication.
*/
//SMTP needs accurate times, and the PHP timezone MUST be set //This should be done in your php.ini, but this is how to do it if you don't have access to that date_default_timezone_set('Etc/UTC');
require '/location/of/PHPMailerAutoload.php'; //Create a new PHPMailer instance $mail = new PHPMailer;
//Tell PHPMailer to use SMTP $mail->isSMTP(); //Enable SMTP debugging // 0 = off (for production use) // 1 = client messages // 2 = client and server messages $mail->SMTPDebug = 2; //Ask for HTML-friendly debug output $mail->Debugoutput = 'html';
//Set the hostname of the mail server $mail->Host = "sub4.mail.dreamhost.com"; //Set the SMTP port number - likely to be 25, 465 or 587 $mail->Port = 587; //Whether to use SMTP authentication $mail->SMTPAuth = true; //Username to use for SMTP authentication $mail->Username = "yourname@example.com"; //Password to use for SMTP authentication $mail->Password = "yourpassword"; //Set who the message is to be sent from $mail->setFrom('from@example.com', 'First Last'); //Set an alternative reply-to address $mail->addReplyTo('replyto@example.com', 'First Last'); //Set who the message is to be sent to $mail->addAddress('whoto@example.com', 'John Doe');
//Set the subject line $mail->Subject = 'PHPMailer SMTP test'; //if you want to include text in the body. $mail->Body = "This is a test of email";
//send the message, check for errors if (!$mail->send()) { echo "Mailer Error: " . $mail->ErrorInfo; } else { echo "Message sent!"; } ?>

More examples are available at PHPM@iler.

You can use a third-party's SMTP server (like Gmail) – just use their mail hostname, and the username and password you use to access that specific email address' inbox. Visit the following page to view a Gmail example:

Important note about bulk email

Bulk email of any kind sent from a DreamHost account is subject to the DreamHost spam policy which is part of the terms of service. Please read those pages along with SMTP quota to ensure you’re not in violation of any of these policies.

Troubleshooting

Certificate errors using PHP 5.6

PHP 5.6 handles certificates differently than previous versions as explained on the following page:

If you use an incorrect mail hostname, you may see the following error when attempting to send email:

Warning: stream_socket_enable_crypto(): Peer certificate CN=`*.mail.dreamhost.com' did not match expected CN=`mail.example.com'

This is due to the hostname being used. View the following article for a detailed solution:

See also

Internal links

External links

 

 

Did this article answer your questions?

Article last updated .