How to send E-mail using PHP


Use Below code to send a Mailer using PHP:

<?php
 // use PHP Mailer
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor/Autoload.php';

$body="Enter Mail Body here";

// send email with create html body data
$mail = new PHPMailer(true);
$mail->IsSMTP(); // telling the class to use SMTP
//$mail->Host       = "ssl://smtp.gmail.com"; // SMTP server
$mail->SMTPDebug  = 1; // enables SMTP debug information (for testing) 1 = errors and messages 2 = messages only
$mail->SMTPAuth   = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
$mail->Host       = "ENTER HOSTNAME HERE";      // sets GMAIL as the SMTP server
$mail->Port       = 465;       // set the SMTP port for the GMAIL server
$mail->Username   = "ENTER USERNAME HERE";  // GMAIL username
$mail->Password   = "ENTER PASSWORD HERE";         // GMAIL password
$mail->SetFrom('ENTER FROM HERE', 'ENTER SENDER'S NAME HERE');
$mail->Subject    = "ENTER SUBJECT HERE";
$mail->MsgHTML($body);
// add email addresses
$mail->AddAddress('ENTER TO HERE', 'NAME');
$mail->AddCC('ENTER CC HERE', 'NAME');
$mail->addAttachment('ATTACHMENT NAME.csv');
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
if (!$mail->Send()) {
   echo "Mailer Error: " . $mail->ErrorInfo;
} else {
   echo "Message sent!";
}
?>
How to send E-mail using PHP How to send E-mail using PHP Reviewed by Ishan Dev Shukl on May 15, 2019 Rating: 5

No comments:

Thanks a lot for your valuable Comment!

Powered by Blogger.