Implement PHP mailer with smtp authentication

Implement PHP mailer with smtp authentication

Implement PHP mailer with smtp authentication

This tutorial is for implementing the PHP mailer with SMTP authentication. You can set SMTP authentication mail to your website using this. So simply follow the setup, and you can get working SMTP authentication mail.

Step 1)

Download the PHP mailer library from here. click here for download

Step 2)

Make a function for sending mail as below.

<?php 
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
require 'PHPMailer/src/Exception.php';
$mail = new PHPMailer(true); // Passing `true` enables exceptions 
try { 
//Server settings 
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = ''; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = "";
$mail->Password = "";
$mail->SMTPSecure = 'tls'; // Enable SSL encryption, TLS also accepted with port 465
$mail->Port = 587; // TCP port to connect to 
$mail->SMTPOptions = array(
    'ssl' => array(
        'verify_peer' => false,
        'verify_peer_name' => false,
        'allow_self_signed' => true
    )
);
//Recipients 
$mail->setFrom('Your SMTP Email Address', 'Your SMTP User Name'); //This is the email your form sends From 
$mail->addAddress('Your User Email Address', 'Your User Name'); // Add a recipient address
//Content 
//$mail->isHTML(true); // Set email format to HTML 
$mail->Subject = 'Your Subject';
$htmldata = "<html>
           <head>
           <title>Title Here</title>
           </head>
           <body>HTML Content</body></html>";
           $mail->IsHTML(true);          
$mail->Body = $htmldata; 
$mail->send(); echo 'Message has been sent'; 
} catch (Exception $e) { echo 'Message could not be sent.'; 
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
?>

Step 3)

Now use the above function to send the SMTP authentication mail via SMTP.

0 Comment

Leave a Reply

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