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 install PHPMailer

The recommend way to install PHPMailer is through composer.

  1. View the following article to set up composer on your DreamHost server:
  2. Once composer is configured, navigate to the directory you wish to install PHPMailer in.
  3. In that directory, install PHPMailer by running the following command:
    [server]$ composer require phpmailer/phpmailer

    This installs PHPMailer into a folder called /vendor in the directory you ran the command in.

Basic code example for email hosted at DreamHost

Insert the following code into a PHP file.

You only need to update the code in bold.

username — Make sure to change the 'username' field to your actual shell username.

Path to the autoload.php file — This is the full path to the autoload.php file. This is different depending on where you installed PHPMailer. Make sure to change this path to the location where you installed PHPMailer.

Host — 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.

Username and setFrom must both be an email on the domain you're sending from.

<?php
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

//Load composer's autoloader
require '/home/username/vendor/autoload.php';

$mail = new PHPMailer(true);                              // Passing `true` enables exceptions
try {
    //Server settings
    $mail->SMTPDebug = 2;                                 // Enable verbose debug output
    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = 'sub4.mail.dreamhost.com';                           // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = 'user@example.com';                 // SMTP username
    $mail->Password = 'secret';                           // SMTP password
    $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587;                                    // TCP port to connect to

    //Recipients
    $mail->setFrom('user@example.com', 'Mailer');
    $mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient
    //$mail->addAddress('ellen@example.com');               // Name is optional
    //$mail->addReplyTo('info@example.com', 'Information');
    //$mail->addCC('cc@example.com');
    //$mail->addBCC('bcc@example.com');

    //Attachments
    //$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
    //$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

    //Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Subject line goes here';
    $mail->Body    = 'Body text goes here';
    //$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
}
?>

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:

Allowing GMAIL to send from your mail application

If you're using your GMAIL address to send via SMTP, you must first allow your application access to your GMAIL address. If you do not do this, your email will not authenticate and not send. View the following article for details:

See also

Internal links

External links

 

 

Did this article answer your questions?

Article last updated .