Another way to learn New Technology

Best way to use PHPMailer for sending emails

0 1,051

How to send Email using PHPMailer

PHPMailer is one of the most well known open source PHP libraries to send emails. It was initially introduced back in 2001 and since then it has turned into the most loved method of PHP developer’s for sending emails programmatically, besides a couple of other fan top choices like Swiftmailer.


In this article, we’ll discuss why you ought to utilize PHPMailer rather than PHP’s mail() function and we’ll demonstrate some code tests on proper methodologies to utilize this library.

PHP’s mail() function is an alternative?

Mostly, PHPMailer is a substitute of PHP’s mail() function, however, there are numerous different situations where the mail() function is basically not sufficiently adaptable to accomplish what you require.

Most importantly, PHPMailer gives object-oriented interface, while mail() is not object oriented. PHP developer, for the most part, prefer not to make $headers strings while sending emails using the mail() function since they require a log of escaping – PHPMailer makes this a breeze. Developers additionally need to compose messy code (escaping characters, encoding and formatting) to send attachments and HTML-based emails when using the mail() function while PHPMailer makes this effortless

Likewise, the mail() function requires a local mail server to send out emails. PHPMailer can utilize a non-local mail server (SMTP) if you have authentication.

Best advantages of using PHPMailer:

  1. It can print different sort of errors messages in more than 40 languages when it neglects to send an email.
  2. Integrated SMTP protocol support and verification over SSL and TLS.
  3. Can send elective plaintext adaptation of email for non-HTML email customers.
  4. Extremely dynamic designer group which stays up with the latest.

PHPMailer is also utilized by popular PHP content management systems like WordPress, Drupal, Joomla etc.

How to install PHPMailer


Click Here to Download PHPMailer


Or Use Composer to Download Via below Command


composer require phpmailer/phpmailer


Sending Email from Local Web Server using PHPMailer

Here is the easiest guide of sending an email from a local web server using PHPMailer

The code and comments should be enough clear to clarify everything that’s going on.

Sending an E-Mail with Attachments

Let’s see an example on how to send an email with attachments using PHPMailer.

<?php require_once "vendor/autoload.php"; $mail = new PHPMailer; $mail->From = "";
$mail->FromName = "Full Name";

$mail->addAddress("", "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->Subject = "Subject Text";
$mail->Body = "Mail body in HTML";
$mail->AltBody = "This is the plain text version of the email content";

    echo "Mailer Error: " . $mail->ErrorInfo;
    echo "Message has been sent successfully";

Here I am attaching two files i.e., 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 giving the file path as argument. For attaching multiple files we need to call it multiple times.

Using SMTP

You can utilize the mail server of an another host to send email, however, for this you initially need confirmation. For instance: to send an email from Gmail’s mail server you need a Gmail account.

SMTP is a protocol utilized via mail clients to send an email to send a demand to a mail server. Once the mail server confirms the email it sends it to the goal mail server.

Here is a case of sending an email from Gmail’s mail server from your space. You needn’t bother with a neighborhood mail server to run the code. We will be utilizing the SMTP protocol:

<?php require_once "vendor/autoload.php"; $mail = new PHPMailer; //Enable SMTP debugging. $mail->SMTPDebug = 3;                               
//Set PHPMailer to use SMTP.
//Set SMTP host name                          
$mail->Host = "";
//Set this to true if SMTP host requires authentication to send email
$mail->SMTPAuth = true;                          
//Provide username and password     
$mail->Username = "";                 
$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 = "";
$mail->FromName = "Full Name";

$mail->addAddress("", "Recepient Name");


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

    echo "Mailer Error: " . $mail->ErrorInfo;
    echo "Message has been sent successfully";

Gmail needs TLS encryption over SMTP so we set it respectively. Before you send via SMTP, you need to find out the hostname, port number, encryption type if needed and if authentication is required you also need the username and password. Note that having a 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’s attack filters will mark it as spam. For example, if you send an email from a server with actual hostname with the from address to, then Yahoo’s servers will mark it as spam or display a message to the user not to trust the email because the mail’s origin is and yet it shows itself as if coming from While you own, there is no way for Yahoo to find that out.

Retrieving E-Mails using POP3

PHPMailer also allows POP-before-SMTP verification to send emails. In other words, you can authenticate using POP and send email using SMTP. Sadly, PHPMailer doesn’t support retrieving emails from mail servers using the POP3 protocol. It’s limited to only sending emails.

Displaying Local Error Messages

$mail->ErrorInfo can return error messages in 43 different languages.

To display error messages in a different language, copy the language directory from PHPMailer’s source code to the project directory.

To return error messages in, for example, Russian, set the PHPMailer object to the Russian language using the below method call:


You can also add your own language files to the language directory.

If you are a New Boston so you can learn about API’s using Official Ducumentation