PHP Script To Send Email Using SMTP

It’s very easy to send email using PHP via SMTP, first of all you must downloads PHP Mailer (https://github.com/PHPMailer/PHPMailer), after we will use small script as an example which will connect to smtp and send email.

<?php
 
require_once "PHPMailer-master/PHPMailerAutoload.php";
 
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "mail.domain.com";
$mail->SMTPAuth = true;
$mail->Username = "username";
$mail->Password = "password";
$mail->From = "[email protected]";
$mail->FromName = "Your Name";
$mail->AddAddress("[email protected]");
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = "This is subject";
$mail->Body = "THis is body <b>html</b>";
$mail->AltBody = "This is the body in plain text.";
if (!$mail->Send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent!";
}

Leave a Reply

Your email address will not be published.