Open In App

How to send an email using PHPMailer ?

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.

composer require phpmailer/phpmailer

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:

$mail->SMTPDebug = 2;                   // Enable verbose debug output
$mail->isSMTP();                        // Set mailer to use SMTP
$mail->Host       = 'smtp.gfg.com;';    // Specify main SMTP server
$mail->SMTPAuth   = true;               // Enable SMTP authentication
$mail->Username   = 'user@gfg.com';     // 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('from@gfg.com', 'Name');           // Set sender of the mail
$mail->addAddress('receiver1@gfg.net');           // Add a recipient
$mail->addAddress('receiver2@gfg.com', 'Name');   // Name is optional

Add attachments (if any).

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

Add the content.

$mail->isHTML(true);                                  
$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.

$mail->send();

And Your e-mail would be sent. Program: Complete PHP program to send e-mail using PHPMailer. 




<?php
 
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
 
require 'vendor/autoload.php';
 
$mail = new PHPMailer(true);
 
try {
    $mail->SMTPDebug = 2;                                      
    $mail->isSMTP();                                           
    $mail->Host       = 'smtp.gfg.com;';                   
    $mail->SMTPAuth   = true;                            
    $mail->Username   = 'user@gfg.com';                
    $mail->Password   = 'password';                       
    $mail->SMTPSecure = 'tls';                             
    $mail->Port       = 587; 
 
    $mail->setFrom('from@gfg.com', 'Name');          
    $mail->addAddress('receiver1@gfg.com');
    $mail->addAddress('receiver2@gfg.com', 'Name');
      
    $mail->isHTML(true);                                 
    $mail->Subject = 'Subject';
    $mail->Body    = 'HTML message body in <b>bold</b> ';
    $mail->AltBody = 'Body in plain text for non-HTML mail clients';
    $mail->send();
    echo "Mail has been sent successfully!";
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
 
?>


Article Tags :