Sending emails from php scripts in windows server

You can send emails from php scripts in Windows Servers using SMTP authentication code.  For this you can use PHPMailer.

Sample code is given below :-

require 'phpmailer\PHPMailer.php';
require 'phpmailer\SMTP.php';
require 'phpmailer\Exception.php';

$mail = new PHPMailer\PHPMailer\PHPMailer();

$mail->IsSMTP(); // Set mailer to use SMTP
//$mail->SMTPDebug = 3; 
$mail->Host = 'server-hostname'; // Specify main server name of your account
$mail->Port = 587; // Set the SMTP port (25/587/465)
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'mail@domainname'; // SMTP username
$mail->Password = '********'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable encryption, 'ssl' also accepted

$mail->From = 'mail@domainname';
$mail->FromName = 'Your From name';
$mail->AddAddress('mail@yourdomainname', 'Name'); // Add a recipient
$mail->IsHTML(true); // Set email format to HTML

$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->Send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
echo 'Message has been sent';

For debuging the errors, you can comment out the line $mail->SMTPDebug = 3; 

Was this answer helpful? 0 Users Found This Useful (0 Votes)