Overview
PHPMailer lets you send email from your site via SMTP. This article describes how to install it on a Shared web server.
If your website is on a Managed VPS or Dedicated Server, view this article instead.
How do I install PHPMailer?
- Log into your server via SSH.
- Make sure you're in your user's home directory.
[server]$ cd ~
- Download the zip file from GitHub.com.
[server]$ wget https://github.com/PHPMailer/PHPMailer/archive/master.zip
- Unzip the file.
This creates a directory named PHPMailer-master.
[server]$ unzip master.zip
- Rename this directory.
[server]$ mv PHPMailer-master PHPMailer
How do I create and run the mail script?
Create a new PHP file and name it something like phpmail.php. You can name this file anything you like and place it in any directory under your user on the server.
What to update in this example
- You only need to update the code in bold.
- username — Make sure to change the username in file paths to your actual shell username. You can also view this by running the command echo $USER.
- 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 — these variables must both be an email on the domain you're sending from.
Add the following code to the PHP file.
<?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; require '/home/username/PHPMailer/src/Exception.php'; require '/home/username/PHPMailer/src/PHPMailer.php'; require '/home/username/PHPMailer/src/SMTP.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 = 'contact@example.com'; // SMTP username $mail->Password = 'secret'; // SMTP password $mail->SMTPSecure = 'tls'; $mail->Port = 587; // TCP port to connect to //Recipients $mail->setFrom('contact@example.com', 'Mailer'); //This is the email your form sends From $mail->addAddress('recipient@dreamhost.com', 'Joe User'); // Add a recipient address //$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; }
?>
How do I run the file?
Run the file using this command:
[server]$ php phpmail.php
This sends an email to the recipient address you added to the script.
How do I use PHPMailer in a contact form?
The code above is a simple example that sends an email when the script is run, however, most often contact form is used to send the email. View the following pages for examples of using PHPMailer in a contact form instead.
How do I allow GMAIL to send from my 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 further details.