
The PHP mail function is a wonderful feature of PHP but like all good things in life this function also has many drawbacks. On a Linuxย platform it requires sendmail to be installed, but coming to windows a SMTP server is needed to send email. So if you take a peak inside the php.ini file you’ll find the following line
[mail function] ; For Win32 only. SMTP = localhost smtp_port = 25
But the limitation here is you can only specify the SMTP server name and port, if the server requires authentication there is no provision for specifying the username and password. So unless you have a mail server configured to send local email without authentication there is no way to use the php mail() to send emails.
This is where sendmail for windows steps in. Created by Byron Jones it is also called fake sendmail. Using this tool you can configure PHP to use an external SMTP server with authentication to send emails using the php mail function. I’ll guide you step-by-step configuring sendmail for windows.
Step 1:- Download and Configure sendmail for windows
Download sendmail for windows extract sendmail.zip and place the contents in an easily accessible place. For this example I’ll place it inside the directory C:\sendmail. Next configure the sendmail.ini file. Open the sendmail.ini file using a text editor such as notepad. Edit the following values
[sendmail] smtp_server=localhost smtp_port=25 ;auth_username= ;auth_password=
For this tutorial I’ll be using gmail’s SMTP servers so the values will be
[sendmail] smtp_server=ssl://smtp.gmail.com smtp_port=465 auth_username=username@gmail.com auth_password=gmailpassword
Save the file and move on to the next step.
Step 2:- Edit the php.ini file
In this step the the sendmail_path value has to be set in the php.ini file, before that comment out the unnecessary lines find the following line and put a semi colon (;) before them
[mail function] ; For Win32 only. ;SMTP = localhost ;smtp_port = 25 ; For Win32 only. ;sendmail_from =
Now modify the following and set the sendmail path to where the sendmail.exe file resides in
; For Unix only.ย You may supply arguments as well (default: "sendmail -t -i"). sendmail_path = C:\sendmail\sendmail.exe
save the php.ini file and restart apache web server or what ever we server you are using.
Step 3:- Test the settings using the mail() function
Create a new file named mail.php inside your document root (htdocs or public_html or www folder) and enter the following code into it
<?php mail("youremail@domain.com","Subject","Email message","From: username@gmail.com"); ?>
Save the file and access it via url (http://example.com/mail.php) you’ll see a blank page. Go and check youremail@domain.com’s inbox for any new mails.
Note: For emails to be sent through an external SMTP server your computer must be connected to the internet. Even though I may sound foolish, this might be the case if you’re testing on your home computer !!
Thx Jesin ๐
this tuto helped me a lot ๐
Does not work for me,
I get this message in the sendmail error.log file
13/11/23 02:29:42 : Socket Error # 10061Connection refused.
Firewall is disabled.
Hi Jon,
What smtp_server are you using? It could be that your ISP is blocking outbound port 25 access.
Open a command prompt and try this
If you should get a connection a 220 message like this
Instead if you get a “Could not open connection to the host” message it means either your ISP is blocking that port or a SMTP server is not setup in that address.
Sorry, my mistake,
I somehow reverted the smtp_server value back to “localhost” from “smtp.gmail.com”.
๐
I make all steps but not work with me and it not display any error but not send
how to configure the sendmail in windows if anybody know the steps pls share with above mail id thanks for your helping
If I would like to CC to other people, could it be down, if yes, how? Thank you!
Hi Chin,
You would add them to the CC list and pass it as the 4th parameter of the PHP mail() function.
Look at Example 3 on this page.
Thank you
This helped me a lot ..Can you tell me how to add attachments to this mail
Dear Sir;
Where shall I put “sendmail” folder?
Warning: mail() [function.mail]: Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in …\wamp\www\mail.php on line 2
can you help me please
The same thing happened to me. I have tried using the telnet application to see if port 25 was working properly, but that’s not an application anymore.
Hi Alexander,
Do you have a mail server setup and running on Windows?
Sendmail for Windows is not a complete mail server it only relays emails via other SMTP servers.
So either you should have a mail server like Hmailserver installed locally or configure sendmail to relay via 3rd party SMTP servers.
Telnet command-line tool is not installed by default. To install it go to Start, search for Turn windows features on or off and choose Telnet client.
this is the error that i got could you please help?
Are you using a correct pair of credentials for the directives
auth_username
andauth_password
?yes i’m using my own email and password. should i chage it?
Are you using your complete email address and not just the username portion? What are you using for
smtp_server
andsmtp_port
?this is what ve wrote in the sendmail file
Enable debug logging by removing the semicolon before the following line in sendmail.ini
Try running the mail.php script, open C:\sendmail\debug.log, put those lines in pastebin.com and share the link here.
I don’t find any file with this name debug.log
I think PHP isn’t using sendmail at all. Open the php.ini file and look under
[mail function]
it should be like thisyes i don’t have such an error anymore thank you so much, but it woks with me just one time . I received the email the first time ,I ve found it in spam ,but when i try it again i don’t receive any email. do you have an idea about it?
Try sending to different recipients. Enable debug logging and check debug.log for messages.
Use Mail Tester to check why the emails are being classified as spam.
could you please tell me how can i enable debug logging? because i tried many and adresses it doesn’t work
Look at my earlier comment.
thank you so much you helped me
It’s not working.. In wordpress I am getting an error: The e-mail could not be sent.
Possible reason: you may have disabled the mail() function.
When I checked the sendmail error log, I got this:
14-07-19 22:53:41 : Socket Error # 11001Host not found.
Please help me… How can I resolve this??
It works generally well except that php crashed from time to time with this exception:
madExecpt version: 3.01
exception number: 1
exception class: EIdSMTPReplyError
exception message: Too many concurrent SMTP connections; please try again later.
Do we need to close the connection after mail() function? How to do it?
This error message is caused by the SMTP server you are using.
What SMTP service are you using and how many emails do you send in a day?
I recon its SMTP server too. It went back to normal after one or two days.
Thanks!
i use ssl 465 then why it connected to ssl 25 and mail not send in debug.log this line found..
Connecting to ssl:25
Socket Error # 11001Host not found.
Post the configuration lines inside the sendmail.ini file.
Does it work on windows 8 ? I tried lot its not working for me
then i get:
Socket Error # 10060Connection timed out.
i tried to change compatability mode for all users on sendmail.exe to
run as admin
run as XP SP3
then i get:
Error connecting with SSL.Error connecting with SSL.error:1408F10B:SSL routines:SSL3_GET_RECORD:wrong version number
i tried to disable avast
then i get:
Connect timed out.
i seem to have got it to work
but im not sure why.
i tried a few more changes in
sendmail.ini
changed:
smtp_server=ssl://smtp.gmail.com
smtp_server=smtp.gmail.com
and
hostname=
to
hostname=localhost
then i got that gmail had blocked a login attempt
i allowed that
and now it seems to works
Thank you to both Jesin A. for the awesome tutorial, and to Clodue for discovering these two changes! These two items were all it took to get mine to work as well.
Thanks Jesin,
In my first attempt, I configured it on windows7 and it’s working.
Keep posting such needful information.
this are my errors
I think there is an error in the
smtp_server
value. Check ifssl://
has a colon and NOT a semicolon.smtp_server=ssl://smtp.gmail.com
here is my setting on smtp_server
There might be multiple
smtp_server
values in the file. Paste the entire sendmail.ini file to pastbin.com and post its link here.Right click on sendmail.exe
Properties
Compatibility
Change the configuration for all users
Execute as Windows XP SP 3
Execute as adminitrator
create new password “Your application-specific passwords” – See https://support.google.com/mail/answer/1173270?hl=en
In sendmail.ini
smtp_server=smtp.gmail.com
smtp_port=587
auth_username=user@gmail.com
auth_password=application password generated as above
Enjoy!!
Thank you. Very helpful procedure to get the mail() function working for me.
i got error when i done this::
Failed to connect to mailserver at “localhost” port 25, verify your “SMTP” and “smtp_port” setting in php.ini or use ini_set() in C:\wamp\www\mail send\mailsend.php on line 9
plz help mee
Did you put a semicolon before
SMTP
andsmtp_port
?yah i put that…but the same answer is coming
Hi i have a problem, so i set up my hmail server and also this sendmail for windows and had follow the steps accordingly, but i am still getting an error like this {[Warning: mail(): SMTP server response: 550 The address is not valid. in C:\wamp\www\controlpanel\mail.php on line 9]
, please advise on what i should fo now.
Good Example, thanks
What is the “license” for these sendmail binaries; I would like to use this at work
Hi Steve,
The official page mentions “BSD license” – http://www.glob.com.au/sendmail/
lol version no updated
latest update 2011
The software’s author has said that himself:
i have got no errors but i didn’t receive any email..?
Check the error.log file in the sendmail directory for any information.
Socket Error # 10060Connection timed out.
What do you have in the smtp_server directive? Try telneting to it to ensure your ISP isn’t blocking outgoing SMTP.
i just follow your steps where i get to know about stmp_server
dude i think my gmail 2 step verification making issues…
In the sendmail.ini file what value did you enter for smtp_server?
smtp.gmail.com
now error log is empty i have removed step two verification from gmail as well but still didn’t receive any mail…. i got message Email sent
It doesn’t work for me I got this in error.log fil Connection Closed Gracefully.
there is no error but it doesn’t work for me!!! would you please tell me what are the other programs I should install to solve this problem? I really need it!!
Thanks for the tutorial – helped us when porting a system from Linux to windows.
thanks
Art
i am getting this error
sendmail: Error during delivery: Invalid address
I go ths error
sendmail: Error during delivery: Invalid address
I have the following error:
Warning: mail() [function.mail]: Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\WinServer\wamp\www\mail.php on line 2
Warning: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\wamp\www\sendEmail\index.php on line
Thenk you for article! It helped my very much!
After TroubleShoot Sendmail.exe i got message as: program not compatible. i am using window 7. help please.
Dear Jesin,
Your tutorial helps me a lots!
My Case: Window 7
sendmail.ini
smtp_server=smtp.gmail.com will work.
Bingo! Thank you!
Regards,
Eugene
Did not work. Here’s what I got:
17/10/04 11:16:44 ** — MESSAGE END —
17/10/04 11:16:45 ** Connecting to ssl:25
17/10/04 11:16:47 ** Disconnected.
17/10/04 11:16:47 ** Disconnected.
17/10/04 11:16:47 ** Disconnected.
I have this in my sendmail.ini:
smtp_server=ssl://smtp.gmail.com
smtp_port=465
smtp_ssl=ssl
Been working on this problem for a while now. Just found your website.
My configuration is:
Windows10
Apache/2.4.25 (Win32)
PHP Version 7.0.15
I have set up my files exactly as your example.
How do I test it?
I do not own a domain. I just connect to the internet though my “isp”
What I am tying to do is just send email using Xampp & Php from my computer to either my gmail account or some other account
Thank You
Hi Jerome,
Your best bet would be to use PHPMailer with you application. They have an example script for relaying emails via Gmail.
Hi! Thanks for your awesome tuto! I followed all indecated steps but it didn’t work
here’s what is in my debug.log
17/12/19 09:24:09 ** Connecting to ssl:25
17/12/19 09:24:12 ** Disconnected.
17/12/19 09:24:12 ** Disconnected.
17/12/19 09:24:12 ** Disconnected.
17/12/19 09:24:12 ** Socket Error # 11001Host not found.
There is no error while sending the mail but i didn’t receive the mail.
Any idea please?!
Hi Raoilison,
What is the value of your
smtp_server
setting?Thank you so much for your quick feedback!
Here’s the value of the smtp_server:
smtp_server=ssl://smtp.gmail.com
and smtp_port=465
I’m connected to the internet when sending the mail,
This is really helpful, love using windows for anything which has to be do with Linux coming across your tutorial has helped so much.
Keep it up mate
Virustotal detected the sendmail for windows as ransomware.
thank u very much
it is so easy to do
i did it for my windows 2016 server and it works perfectly
hi i downloaded what you said from the link above
but it’s strange i have no php.ini file????
Hi Jay, Have you installed PHP for Windows? https://windows.php.net/download
lol no ok what link to i press, download source code or zip??
Can you provide more info on what you’re trying to do?
basically i’v made a songs request form as i own and run an online voluntry radio station
now everytime i test the form, the submission goes into spam. thats
why i need something that pings the submission into inbox i’v been told to use
php mailer or sendmail
So you have a website and would like to send emails from its contact form, correct? And I assume the website is hosted at a web hosting service provider. The article here is for sending emails from a Windows PC and wouldn’t apply in your case. I’ll email you a solution.
i have emailed you back but gmail has spat the email i wrote to you back to me apparently
Message not delivered
Your message couldn’t be delivered to because the remote server is misconfigured. See technical details below for more information.
The response from the remote server was:
554 5.7.1 : Relay access denied
anyone going to comment