How To Send Mail Using Smtp in php

What is Smtp( Simple Mail Transfer Protocol):-

SMTP stands for simple mail transfer protocol. You might have previously heard of POP and IMAP protocols used to receive email. Basically, every time you receive an email, whether you’re using POP or IMAP, these protocols enable you to receive an email. 

SMTP is a protocol for sending emails; it enables you to send emails. SMTP is a set of commands your email client uses that authenticates and directs the transfer of emails you’re sending







<?php

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


require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';


$mail = new PHPMailer(true);

try {
 
                  
    $mail->isSMTP();                                           
    $mail->Host       = 'smtp.gmail.com';                     
    $mail->SMTPAuth   = true;                                   
    $mail->Username   = 'ajaypatti@gmail.com';                    
    $mail->Password   = 'hihahcvalceijmxz';                             
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;            
    $mail->Port       = 465;                                    

  
    $mail->setFrom('ajaypatti@gmail.com', 'Send mail');

    $mail->addAddress('ajaypatti@gmail.com');             


    $mail->isHTML(true);                                 
    $mail->Subject = 'Here is the subject'.time();
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}

                                             Download Smtp File