PHPMailer — Installing on a VPS or Dedicated server

 

Overview

This article explains how to install PHPMailer on a VPS or Dedicated Server using Composer, allowing you to send email from your site via SMTP. Visit PHPMailer's GitHub page for further information on its features.

See this article instead if you would like to install PHPMailer on a website hosted on a shared server.

Installing PHPMailer

The recommended way to install PHPMailer is through composer on a VPS or Dedicated Server.

  1. See this article to set up composer on your DreamHost server.
  2. Navigate to the directory you wish to install PHPMailer.
  3. Install PHPMailer using the composer command:
    [server]$ composer require phpmailer/phpmailer

    This installs PHPMailer into a folder called /vendor in the directory where the command was run.

Creating an email script

This section provides a simple script to use PHPMailer with a DreamHost email address.

 

Create a PHP file

Create a new PHP file and name it something like phpmail.php. It can be named anything and be placed in any directory.

Add code to the file

Insert the following code into the PHP file. Please note the following when editing your code example:

  • You only need to update the highlighted code.
  • username — Make sure to change the 'username' field to your actual shell username. Then add the full path to the vendor directory you created in step #2 above.
  • 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 — If you're sending from a DreamHost address, you should only use smtp.dreamhost.com. If you are using a Gmail address, use smtp.gmail.com.
  • 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/path-to-vendor-dir/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 = 'smtp.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';                            
    $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('contact@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;
}
?>

Test the file

Run the file. This sends an email to the recipient address you added to the addAddress field.

[server]$ php phpmail.php

Using PHPMailer in a contact form

The code above is a simple example that sends an email when the script is run. Most often, a contact form is used to send the email. View the following link for an example of using PHPMailer in a contact form instead.

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 be sent. See this article for details.

See also

DreamHost links

Third-party links

Did this article answer your questions?

Article last updated PST.

Still not finding what you're looking for?