PHP

How to Send Email with PHPMailer

By Shahrin Nizar27 Jan, 2022

PHPMailer is one of the world's popular code for sending email from PHP. It also used by many open source project such as Wordpress. Integrated SMTP make is easier to send email without a local mail server. Here is quick guide to send email from SMTP server from a provider such as Gmail (Google Mail).

Download PHPMailer

First, download PHPMailer release from Github
https://github.com/PHPMailer/PHPMailer/releases

Extract it and copy on your project folder.

Writing PHPMailer Script

Create a PHP file which you will be using to send email. For this example, I rename the file as phpmailer.php

While opening PHP markup, write down the following.

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

Depending on folder name which consist of PHPMailer version, add this line.

require '<Path to PHPMailer folder>/Exception.php';
require '<Path to PHPMailer folder>/PHPMailer.php';
require '<Path to PHPMailer folder>/SMTP.php';

Since I'm using version 6.6.0, my phpmailer.php will look like this.

<?php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'PHPMailer-6.6.0/Exception.php';
require 'PHPMailer-6.6.0/PHPMailer.php';
require 'PHPMailer-6.6.0/SMTP.php';

?>

Add host info and default port configuration.

$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->Host        = 'smtp.example.com';
$mail->Port        = 587;
$mail->SMTPSecure  = 'tls';
$mail->SMTPAuth    = true;
$mail->SMTPAutoTLS = true;

Authenticate user account and password.

$mail->Username = 'user@example.com';
$mail->Password = 'somepassword';

Set email from and recipient email. If you have more than one recipient, add more rows with different email address.

$mail->SetFrom('user@example.com', 'Some Name');
$mail->addAddress('customer.one@domain.com', 'Recipient 1');
$mail->addAddress('customer.two@domain.com', 'Recipient 2');

You may now craft content for your mail body. If you set isHTML, your mail body can be markup in HTML format.

$mail->isHTML(true);
$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

Optional, if you wish your mail to have attachment. 

$mail->addAttachment('/upload/image.jpg');                // Sample 1, without file name.
$mail->addAttachment('/upload/image.jpg', 'photo.jpg');   // Sample 2, with file name which rename attachment name.

Send email for the last line.

$mail->send();

If you need to catch and debug any error, you add the follow line.

$mail->SMTPDebug  = 2;

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

This is how you final code may will look like.

<?php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'PHPMailer-6.6.0/Exception.php';
require 'PHPMailer-6.6.0/PHPMailer.php';
require 'PHPMailer-6.6.0/SMTP.php';

$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->Host        = 'smtp.example.com';
$mail->Port        = 587;
$mail->SMTPSecure  = 'tls';
$mail->SMTPAuth    = true;
$mail->SMTPAutoTLS = true;

$mail->Username = 'user@example.com';
$mail->Password = 'somepassword';

$mail->SetFrom('user@example.com', 'Some Name');
$mail->addAddress('customer.one@domain.com', 'Recipient 1');
$mail->addAddress('customer.two@domain.com', 'Recipient 2');

$mail->isHTML(true);
$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

$mail->addAttachment('/upload/image.jpg', 'photo.jpg'); 

$mail->send();

$mail->SMTPDebug  = 2;

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

?>

Sending Email Using PHPMailer with Google Mail

You need to change smtp host with gmail domain.

$mail->Host = 'smtp.gmail.com';

Next, enable 2-step verification which then allow you to create app password. Sign-in to your Google Account and go to https://myaccount.google.com/security. App password can only be viewed once.

$mail->Username = 'username@gmail.com';
$mail->Password = 'app-password';

Now you should be able to send email to targeted address from your Gmail using PHPMailer.

Advertisement
Copyright © 2018 - 2022, XGEA TECH
This website is developed by Shahrin Nizar and sponsored by OneHosting.sg