Sending email with SMTP authentication with PHPMailer on Windows Server

Modified on Mon, 16 Dec, 2024 at 6:55 PM

To send email with SMTP authentication on Windows Server, you just need to copy the libraries to the root directory of the website.


Below we share the script for sending email and the PHPMailer libraries.


<?php
 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;
   } 
  else {
   echo 'Message has been sent';
 }
?>

To debug errors, you can uncomment the line $mail->SMTPDebug = 3;


* Note this setting only applies to customers using Cloud Servers or Bare Metal with Windows Server.

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article