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!";
}
?>

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.

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: