Single Blog

How to Send Emails Using PHP Mail and PHPMailer Library?

March 15, 2022, Written by 0 comment

PHPMailer is a child of PHP (Hypertext Preprocessor language) and is widely used by PHP developers and only for mail purposes. It was initially released 20 years ago (2001 by Brent R. Matzelle) and is available in 47 languages.

Honestly, PHP gained quite immense popularity since its deployment in the market especially for web development purposes and now the technology entraps in each corner of the industry. 

What is PHP Mail/PHPMailer/PHPMailer Library? 

PHPMailer, the name itself definite its meaning is a popular code library to send emails safely (via PHP code/mail() function in PHP) from a web server i.e. MUA to the MSA server.

It is saying that PHPMailer courtly simplifies the process of sending emails and it is very easy to use. It also eliminates the precautions, issues, and vulnerabilities about email injection for spamming and leads email senders to achieve a good email reputation and scores. 

Basic and Advanced Features of PHPMailer

  • Such technology is exceptionally on its own and the world’s most popular code for sending email from PHP.
  • It is renowned and recognized by top open-source projects such as WordPress, Drupal, 1CRM, SugarCRM, Yii, Joomla.
  • Integrated SMTP support – send without a local mail server.
  • Send emails with multiple To, CC, BCC, and Reply-to addresses
  • The most organic feature of PHPMailer is that you can add attachments, including inline.
  • Validates email addresses automatically. 
  • Protects against header injection attacks.
  • Error messages in over 50 languages.
  • Namespaced to prevent name clashes and much more!

Read more about its features, popularity, and explore complete documentation on 

A Basic Guide To Send Emails in PHP Using PHP Library

It is very simple to draft emails and configure them to send via PHPMailer. Proceed with the mentioned steps to learn how to send emails using PHP Mail and the PHPMailer library.

  • Install Composer
  • Open PHPMailer
  • Configure the Server Setting
  • Complete the PHP Program

Step 1 – Install Composer

The best way to install PHPMailer is by using composer. Download the composer in case you haven’t. After this follow the below pins. 

  • Open the Command prompt and go to the directory of the project in which you want to use PHPMailer.
  • Try to run this command: composer require phpmailer/phpmailer
  • Wait for seconds and let the installation complete successfully. 

Step 2 – Using PHPMailer

  • Import the PHPMailer classes into the global namespace.
  • Load the composer’s autoloader.
  • Create a PHPMailer class object.

Step 3 – Configure the Server Setting

Now you need to configure your server to perform some crucial tasks. Those might include – 

  • SMTPDebug: Avail this to display messages regarding problems in connectivity and sending emails. It has the following values:

0: It is the default value. Disable debugging.

1: Display output messages sent by the client.

2: As 1, plus display responses received from the server

3: As 2, plus more information about the initial connection – this level can help diagnose STARTTLS failures.

4: As 3, plus display even lower-level information.

  • isSMTP(): Set mailer to use SMTP.
  • isMail(): Set mailer to use PHP’s mail function.
  • Host: Specifies the servers.
  • SMTPAuth: Enable/Disable SMTP Authentication.
  • Username: Give the username.
  • Password: Write the password.
  • SMTPSecure: Specify encryption technique. Accepted values ‘tls’ or ‘SSL’.
  • Port: Specify the TCP port which is to be connected.

Add the recipients of the mail.

Add attachments (if any).

Add the Content

  • isHTML(): If passed true, sets the email format to HTML.
  • Subject: Provide the subject of the Mail.
  • Body: Provide the contents of the Mail.
  • AltBody: Alternate body in case the e-mail client doesn’t support HTML.

At last, send the mail

Step 4 – Complete the PHP Program

Hey there! Write the following PHP program to send email using PHPMailer.


use PHPMailer\PHPMailer\PHPMailer;

use PHPMailer\PHPMailer\Exception;

require ‘vendor/autoload.php’;

$mail = new PHPMailer(true);

try {

$mail->SMTPDebug = 2;


$mail->Host = ‘;’;

$mail->SMTPAuth = true;

$mail->Username = ‘’;

$mail->Password = ‘password’;

$mail->SMTPSecure = ‘tls’;

$mail->Port = 587;

$mail->setFrom(‘’, ‘Name’);


$mail->addAddress(‘’, ‘Name’);


$mail->Subject = ‘Subject’;

$mail->Body = ‘HTML message body in <b>bold</b> ‘;

$mail->AltBody = ‘Body in plain text for non-HTML mail clients’;


echo “Mail has been sent successfully!”;

} catch (Exception $e) {

echo “Message could not be sent. Mailer Error: {$mail->ErrorInfo}”;



It’s Done! Enjoy

See. Isn’t it so easy to use the mailer function in PHP…!

Now it’s your turn to prove that you actually acknowledge this blog. I am excited to hear your comments guys! Share your thoughts ASAP. 

Need any help with mailing services? Time4Servers Experts can help you in everything right from setting up a mailing server to configuring for optimum performance and more.

Leave a reply

Your email address will not be published. Required fields are marked *