How To Use Sendmail On Windows To Send Email Using PHP

By bhagwatchouhan
How To Use Sendmail On Windows To Send Email Using PHP

In certain conditions, we might be required to send emails using PHP from the Windows system without installing the email server. We can do so by installing and configuring Sendmail on Windows. This tutorial provides all the steps required to install and configure Sendmail on Windows to send emails using PHP.

 

Install and Configure Sendmail

 

Download the Sendmail from Official Website.

 

Now extract the downloaded zip to the appropriate location and open the sendmail.ini file to configure the email server as shown below.

 

;Configure Sendmail

[sendmail]
...
smtp_server=<server>
...
smtp_port=<port>
...
auth_username=<authentication username>
...
auth_password=<authentication password>

 

An example to configure the Gmail is as shown below.

 

[sendmail]
...
smtp_server=ssl://smtp.gmail.com
...
smtp_port=465
...
auth_username=username@gmail.com
...
auth_password=password

;OR

[sendmail]
...
smtp_server=tls://smtp.gmail.com
...
smtp_port=587
...
auth_username=username@gmail.com
...
auth_password=password

 

Now save the file and close it to apply the changes. The settings might be different based on the email server or service provider.

 

Configure PHP

 

In case you are using the secure ports 587 or 465, you might get an error in case the OpenSSL module is not enabled for PHP. It can be done on Windows by following How To Configure PHP OpenSSL Module On Windows.

 

In order to configure PHP, open the php.ini file and update it as shown below.

 

;SMTP = localhost;
;smtp_port = 25;

...
...

;auth_username = ;
;auth_password = ;

...
...

;sendmail_from = me@example.com

...
...

sendmail_path = C:\sendmail\sendmail.exe

 

Send Email

 

In this step, we will write the program to send an email using the configurations done by us in previous steps. Write a PHP program to send email as shown below.

 

<?php
$from = "myemail@gmail.com";
$to = "myfriend@gmail.com";
$subject = "Hello Sendmail";
$message = "This is an test email to test Sendmail. Please do not block my account.";
$headers = [ "From: $from" ];

mail( $to, $subject, $message, implode( '\r\n', $headers ) );
// OR - PHP 7.2.0 or greater
//mail( $to, $subject, $message, $headers );

 

Also, check the error.log file in the Sendmail directory to check for errors. It might also generate a crash.txt file in case of any error.

 

Summary

 

In this tutorial, we have installed Sendmail on Windows and configured it to send emails using the PHP program.

share on :

Profile picture for user bhagwatchouhan
bhagwatchouhan