send-emails-in-php-using-phpmailer

Send EMails In PHP Using PHPMailer

PHPMailer is perhaps the most popular open-source PHP library to send emails with. It was first released way back in 2001.

In this Blog Post , We will discuss why you should use phpmailer instead of PHP’s mail() Function, We also have added Some Sample Codes for You to get Started.

What are Advantages Of PHPMailer over PHP’s mail() Function?

First Main Advantage is that PHPMailer Provide Object Oriented interface,whereas mail() is Not a Object Oriented.

while using mail() PHP Developer Have to Create a $header String while Sending mail because they require a Lot of escaping. PHPMailer Make this a Lot Easier.

Developers also need to write a lot of code (escaping characters, encoding and formatting) to send attachments and HTML based emails when using the mail() function, whereas PHPMailer makes this Easy.

The mail() function requires a local mail server to send out emails, which is not always Available to set up. PHPMailer can use a non-local mail server (SMTP) if you have authentication.

Other Advantages Of PHPMailer:

  • can print various kinds of error messages in more than 40 languages when fails to send an email.
  • has integrated SMTP protocol support and authentication over SSL and TLS.
  • can send an alternative plain-text version of email for non-HTML email clients.
  • has a very active developer community that keeps it secure and up to date.

PHPMailer is also used by popular PHP content management systems like WordPress, Drupal, and Joomla.

How to Install PHPMailer?

You can Easily Install PHPMailer Using Composer:

composer require phpmailer/phpmailer

Or You Can Download it From Github: github.com

Or Download From Here:

PHPMailer-6.1.7.Zip

FREE DOWNLOAD

Send download link to:

How To Send Mail From Local Web Server Using PHPMailer?

Below Is a Example Of Sending Mail From Local web server using PHPMailer, We have added Comments along with Code so it will be sufficiently Clear to understand.

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require_once "vendor/autoload.php";

//PHPMailer Object
$mail = new PHPMailer(true); //Argument true in constructor enables exceptions

//From email address and name
$mail->From = "from@yourdomain.com";
$mail->FromName = "Full Name";

//To address and name
$mail->addAddress("recepient1@example.com", "Recepient Name");
$mail->addAddress("recepient1@example.com"); //Recipient name is optional

//Address to which recipient will reply
$mail->addReplyTo("reply@yourdomain.com", "Reply");

//CC and BCC
$mail->addCC("cc@example.com");
$mail->addBCC("bcc@example.com");

//Send HTML or Plain Text email
$mail->isHTML(true);

$mail->Subject = "Subject Text";
$mail->Body = "<i>Mail body in HTML</i>";
$mail->AltBody = "This is the plain text version of the email content";

try {
    $mail->send();
    echo "Message has been sent successfully";
} catch (Exception $e) {
    echo "Mailer Error: " . $mail->ErrorInfo;
}
?>

How to Send Email With Attachment?

Below Is an Example of Sending Email With Attachment using PHPMailer.

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require_once "vendor/autoload.php";

$mail = new PHPMailer;

$mail->From = "from@yourdomain.com";
$mail->FromName = "Full Name";

$mail->addAddress("recipient1@example.com", "Recipient Name");

//Provide file path and name of the attachments
$mail->addAttachment("file.txt", "File.txt");        
$mail->addAttachment("images/profile.png"); //Filename is optional

$mail->isHTML(true);

$mail->Subject = "Subject Text";
$mail->Body = "<i>Mail body in HTML</i>";
$mail->AltBody = "This is the plain text version of the email content";

try {
    $mail->send();
    echo "Message has been sent successfully";
} catch (Exception $e) {
    echo "Mailer Error: " . $mail->ErrorInfo;
}
?>

Here, we’re attaching two files — file.txt, which resides in the same directory as the script, and images/profile.png, which resides in images directory of the script directory.

To add attachments to the email, we just need to call the function addAttachment of the PHPMailer object by passing the file path as argument. For attaching multiple files, we need to call it multiple times.

How To Send Mail Via SMTP?

You can use the mail server of an another host to send email, but for this you first need to have authentication. For example, to send an email from Gmail’s mail server, you need to have a Gmail account.

SMTP is a protocol used by mail clients to send an email send request to a mail server. Once the mail server verifies the email, it sends it to the destination mail server.

Here’s an example of sending an email from Gmail’s mail server from your domain. You don’t need a local mail server to run the code. We’ll be using the SMTP protocol:

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

require_once "vendor/autoload.php";

$mail = new PHPMailer(true);

//Enable SMTP debugging.
$mail->SMTPDebug = 3;                               
//Set PHPMailer to use SMTP.
$mail->isSMTP();            
//Set SMTP host name                          
$mail->Host = "smtp.gmail.com";
//Set this to true if SMTP host requires authentication to send email
$mail->SMTPAuth = true;                          
//Provide username and password     
$mail->Username = "name@gmail.com";                 
$mail->Password = "super_secret_password";                           
//If SMTP requires TLS encryption then set it
$mail->SMTPSecure = "tls";                           
//Set TCP port to connect to
$mail->Port = 587;                                   

$mail->From = "name@gmail.com";
$mail->FromName = "Full Name";

$mail->addAddress("name@example.com", "Recepient Name");

$mail->isHTML(true);

$mail->Subject = "Subject Text";
$mail->Body = "<i>Mail body in HTML</i>";
$mail->AltBody = "This is the plain text version of the email content";

try {
    $mail->send();
    echo "Message has been sent successfully";
} catch (Exception $e) {
    echo "Mailer Error: " . $mail->ErrorInfo;
}
?>

Gmail requires TLS encryption over SMTP, so we have to set it accordingly. Before you send via SMTP, you need to find out the host name, port number, encryption type if required and if authentication is required you also need the username and password.

Note that having two-factor authentication enabled on Gmail won’t let you use their SMTP with username/password. Instead, additional configuration will be required.

One big advantage in using remote SMTP over local mail is that if you use PHP’s mail() function to send email with the from address domain set to anything other than the local domain name (name of the server), then the recipient’s email server attack filters will mark it as spam.

PHPMailer-6.1.7.Zip

FREE DOWNLOAD

Send download link to:

You can learn about this library’s APIs in the repository wiki, or in the official documentation.

Also Check Out: How to Make Friendly URL in PHP? How to Slugify a String in PHP?

If You Have Any Problem Or Doubts,
Please Do Ask Or Suggest In Comment.


Do Follow Us on:

Facebook.com

Twitter.com

About: Aakash Kothari

I am passionate about my work. Because I love what I do, I have a steady source of motivation that drives me to do my best. In my last job, this passion led me to challenge myself daily and learn new skills that helped me to do better work


    2 thoughts on “Send EMails In PHP Using PHPMailer”

    Leave a Reply

    %d bloggers like this: