Send Email Using PHPMailer

Send Email Using PHPMailer

It provides all the steps required to send emails using PHPMailer.

December 25, 2019

In certain conditions, we might be required to send emails using PHP without installing the email server. We can do so by installing and configuring PHPMailer. This tutorial provides all the steps required to install and configure PHPMailer using Composer to send emails using PHP.

Install PHPMailer

In case your project is already using the composer, you just need to add this line within the require section of your composer.json file. If you are new to Composer, you can follow How To Install Composer On Windows to install Composer and start using it as a project dependency manager.

"require" : {
....
....
"phpmailer/phpmailer": "6.1.*",
....
....
}

After updating the file, simply run the composer update command to install PHPMailer.

You can also install PHPMailer using the command within the project root directory as shown below.

# Install PHPMailer

composer require phpmailer/phpmailer

# OR

composer require phpmailer/phpmailer:6.1.*

Send Mail Using PHPMailer

In this step, we will use PHPMailer to trigger emails. The below-mentioned PHP script can be used to trigger emails using PHPMailer using PHP's mail function.

<?php
require __DIR__ . '/vendor/autoload.php';

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

$mailer = new PHPMailer( true );

try {

// Use PHP mail function
$mailer->isMail();

// Sender
$mailer->setFrom( 'admin@mydomain.com', 'Admin' );

// Recipients
$mailer->addAddress( 'info@mydomain.com' );

// Send HTML Email
$mailer->isHTML( true );

// Configure Subject and Body
$mailer->Subject = 'Welcome PHPMailer';
$mailer->Body = 'Mail sent using <b>PHPMailer</b>.';

// Trigger Email
$mailer->send();

echo "Email sent.";
}
catch( Exception $e ) {

echo "Error sending email: {$mailer->ErrorInfo}";
}

Make sure that the appropriate Email server is installed on the system or php.ini is configured to use an SMTP server. You can also follow How To Use Sendmail On Windows To Send Email Using PHP to configure Sendmail on Windows.

We can also configure PHPMailer to use an SMTP server as shown below.

<?php
require __DIR__ . '/vendor/autoload.php';

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

$mailer = new PHPMailer( true );

try {

// Use SMTP
$mailer->isSMTP();

// Debug SMTP
$mailer->SMTPDebug = 2;

// Configure SMTP
$mailer->Host = 'tls://smtp.gmail.com';
$mailer->SMTPAuth = true;
$mailer->Username = 'yourname@gmail.com';
$mailer->Password = 'password';
$mailer->SMTPSecure = 'tls';
$mailer->Port = 587;

// Sender
$mailer->setFrom( 'yourname@gmail.com', 'Your Name' );

// Recipients
$mailer->addAddress( 'receiver1@gmail.com' );
$mailer->addAddress( 'receiver2@gmail.com', 'Receiver2 Name' );
// Send HTML Email
$mailer->isHTML( true );

// Configure Subject and Body
$mailer->Subject = 'Welcome PHPMailer';
$mailer->Body = 'Mail sent using <b>PHPMailer</b>.';

// Trigger Email
$mailer->send();

echo "Email sent.";
}
catch( Exception $e ) {

echo "Error sending email: {$mailer->ErrorInfo}";
}

Summary

In this tutorial, we have installed PHPMailer and used it to send emails by configuring it.

You can also post your comments to join the discussion. The other relevant tutorials for PHP includes How To Send Email Using PHP, Send Email Using Swift Mailer, How To Use Sendmail On Windows To Send Email Using PHP, How To Configure PHP OpenSSL Module On Windows, How To Install XAMPP On Windows, How To Install WampServer on Windows, How To Install PHP 7 On Ubuntu 18.04 LTS, and How To Install PHP 7 On Windows.

Write a Comment
Click the captcha image to get new code.
Discussion Forum by DISQUS