Open In App

How to send an email using PHPMailer ?

Last Updated : 07 Mar, 2023
Like Article

PHPMailer is a code library and used to send emails safely and easily via PHP code from a web server. Sending emails directly via PHP code requires a high-level familiarity to SMTP standard protocol and related issues and vulnerabilities about Email injection for spamming. PHPMailer simplifies the process of sending emails and it is very easy to use. Installation: The best way to install PHPMailer is by using composer. Before proceeding make sure to install composer.

  • Open the Command prompt and go to the directory of the project in which you want to use PHPMailer.
  • Run the following command:
composer require phpmailer/phpmailer
  • Wait for the installation to complete. It will download all the necessary classes to your project folder.

Using PHPMailer: Import the PHPMailer classes into the global namespace. Note: Make sure that these lines are at the top of the script not inside any function.

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

Load the composer’s autoloader.

require 'vendor/autoload.php';

Create a PHPMailer class object.

$mail = PHPMailer()

Configure the server settings:

  • SMTPDebug: Used to display messages regarding problems in connectivity and sending emails. It has following values:
    • 0: It is 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: Specify the username.
  • Password: Specify the password.
  • SMTPSecure: Specify encryption technique. Accepted values ‘tls’ or ‘ssl’.
  • Port: Specify the TCP port which is to be connected.
$mail->SMTPDebug = 2;                   // Enable verbose debug output
$mail->isSMTP();                        // Set mailer to use SMTP
$mail->Host       = ';';    // Specify main SMTP server
$mail->SMTPAuth   = true;               // Enable SMTP authentication
$mail->Username   = '';     // SMTP username
$mail->Password   = 'password';         // SMTP password
$mail->SMTPSecure = 'tls';              // Enable TLS encryption, 'ssl' also accepted
$mail->Port       = 587;                // TCP port to connect to

Add the recipients of the mail.

$mail->setFrom('', 'Name');           // Set sender of the mail
$mail->addAddress('');           // Add a recipient
$mail->addAddress('', 'Name');   // Name is optional

Add attachments (if any).

$mail->addAttachment('url', 'filename');    // Name is optional

Add the content.

  • isHTML(): If passed true, sets the email format to HTML.
  • Subject: Set the subject of the Mail.
  • Body: Set the contents of the Mail.
  • AltBody: Alternate body in case the e-mail client doesn’t support HTML.
$mail->Subject = 'Subject';
$mail->Body    = 'HTML message body in <b>bold</b>!';
$mail->AltBody = 'Body in plain text for non-HTML mail clients';

Finally, send the email.


And Your e-mail would be sent. Program: Complete PHP program to send e-mail 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}";

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads