<?php
require 'includes/PHPMailer.php';
require 'includes/SMTP.php';
require 'includes/Exception.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
$mail = new PHPMailer();
// Message Importing from another file **.
//$message = file_get_contents('simple-mail.php');
$mail->isSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls";
$mail->Port = "587";
$mail->CharSet = 'utf-8';
$mail->Username = "your_gmail_username";
$mail->Password = "your_gmail_password";
$mail->Subject = "New Mail";
$mail->From = 'No-Reply@example.com';
$mail->FromName = 'Anantaraj Khanal';
$mail->isHTML(true);
//if you want to attached a message through another file **.
//$mail->MsgHTML($message);
$mail->Body = 'your_message';
$mail->addAddress('sender_mail_address');
// Sent mail
$mail->send();
// Condition to sent mail
if ( $mail->send() ) {
echo "Email Sent..!";
}else{
echo "Message could not be sent. Mailer Error: "{$mail->ErrorInfo};
}
// To close SMTP
$mail->smtpClose();
?>This code uses the PHPMailer library to send an email using SMTP. Here is an overview of what the code does:
- The
requirestatements include the necessary PHPMailer files. - The
usestatements import PHPMailer classes to the current namespace. - A new instance of
PHPMaileris created. - The
isSMTP()method sets the mailer to use SMTP. - The
Host,SMTPAuth,SMTPSecure,Port,CharSet,Username, andPasswordproperties are set to appropriate values for the SMTP server you want to use. - The
Subject,From,FromName, andisHTMLproperties are set. - The email message body is set using either the
MsgHTMLmethod or theBodyproperty. - The recipient email address is added using the
addAddressmethod. - The
sendmethod is called to send the email. - A conditional statement checks if the email was sent successfully and displays a message accordingly.
- The
smtpClosemethod is called to close the SMTP connection.
To use this code, you need to replace the placeholders your_gmail_username, your_gmail_password, No-Reply@example.com, Anantaraj Khanal, your_message, and sender_mail_address with appropriate values for your use case. Additionally, you need to make sure that the necessary PHPMailer files are present in the includes directory.





