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; 

  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

500 internal server error - How to Get detailed errors

By default, our Windows hosting accounts display a custom/generic error when applications...

Disabling Browser Cache on Windows Server static files

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

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

How to install JAVA application in DocumentRoot directory?

There is no such functionality in Plesk at the moment to install java application in root...

How to set Session Timeout in web.config

There are two way to set session timeout property in ASP.Net.First Method : Edit the following in...