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 :-

<?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';
}
?>


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


  • 1 Users Found This Useful
Was this answer helpful?

Related Articles

URL Rewrite/mod_rewrite for Windows on IIS 7.x 8.x

For customers who are looking for a URL rewrite function (like Apache's mod_rewrite) on our...

Windows IIS Server - Reditect http:// to https://

Using the following code in your web.config file automatically redirects visitors to the HTTPS...

Enabling Cache on Your Windows Hosting Account using clientCache

With our Windows hosting accounts, you can create caching rules to enhance your website's speed...

Disabling Browser Cache on Windows Server static files

The following configuration sample adds an HTTP "Cache-Control: no-cache" header to the response,...

Scheduled Tasks (cron job)

Scheduling Tasks (Windows) If you need to run scripts on your hosting account at specific time,...