When we are working on a new web application we usually test our projects locally before making them public. But, localhost doesn’t have all the good stuff available on the real host. Some functions need to be configured properly and some additional programs need to be implemented.
Problem
I was working on PHP/MySQL based small project which is all about students’ registration system, where the students can register themselves then they’ll be able to receive school transcripts and such stuff by email.
When I finished the project I tested the application to see if it was working properly but I got a problem with PHP‘s mail()
function, which was not sending any email messages out.
I was feeling frustrated for a while before coming up with the solution below.
Solution
To make our locally hosted web applications talk to any SMTP server including those on the Internet, we will configure that by using the PHP‘s configuration file called php.ini
which can be found in the following locations (assuming you are using XAMPP installed in drive C:/
):
C:\<xampp-installation-path>\php\php.ini
C:\<xampp-installation-path>\php\php5.ini
C:\<xampp-installation-path>\apache\bin\php.ini
Okay, that was locating the configuration files; let’s move to the next steps.
Method 1:
– Open php.ini
file and uncomment the php_smtp.dll
extension. This is required when sending emails to a remote server.
– Scroll down and find the following lines:
[mail function]
; For Win32 only.
;SMTP = localhost
;smtp_port = 25
; For Win32 only.
;sendmail_from = [email protected]
– From the lines above, uncomment SMTP
, smtp_port
and sendmail_from
directives, then add SMTP server, SMTP port number and your preferred email address to SMTP
, smtp_port
and sendmail_from
directives respectively, your final code should be similar to the one below:
[mail function]
; For Win32 only.
SMTP = mail.server.com
smtp_port = 25
; For Win32 only.
sendmail_from = [email protected]
– Replace mail.server.com
and [email protected]
with correct values. The defualt SMTP port number is “25”, therefore, you have 99% chance of not changing this.
– Restart your server.
Everything should work properly now. If not, double check your changes again. If you think you made everything correct, but there is nothing working, try method 2 instead.
NB: You should be aware that once you assign an email address to sendmail_from
PHP will force all the senders’ emails to that address.
Method 2:
This method is more easier than the steps described in method 1. We’ll use fake Sendmail Program for Windows (sendmail.exe
) which is a simple windows console application that emulates sendmail's "-t"
option to deliver emails piped via stdin
. sendmail.exe
is bundled with XAMPP so you don’t need to install it unless you are using hand made server.
– In method 1 we have enabled SMTP
, smtp_port
and sendmail_from
directives, please make sure that these directives are commented out since we don’t need them anymore. Then scroll down and find the following two lines in your php.ini
file:
; For Unix only. You may supply arguments as well (default: "sendmail -t -i").
sendmail_path = "C:\<xampp-installation-path>\sendmail\sendmail.exe -t"
– Make sure that the sendmail_path
directive is not commented out, and the path is correct, then save php.ini
and close it.
Edit Sendmail Configuration File (sendmail.ini
):
The fake Sendmail program is found in the following location:
C:\<xampp-installation-path>\sendmail\sendmail.exe
…and its configuration file is found here:
C:\<xampp-installation-path>\sendmail\sendmail.ini
Okey. That was that. Let’s configure it, so it will work the way we wanted.
– Open sendmail.ini file and use the following settings:
[sendmail]
; you must change mail.mydomain.com to your smtp server,
smtp_server=mail.mydomain.com
; smtp port (normally 25)
smtp_port=25
– Replace mail.mydomain.com
with a valid SMTP server and assign port number (usually 25) to smtp_port
.
Your SMTP server may require an authentication. If this is the case, scroll down the file and find the following lines:
; if your smtp server requires authentication, modify the following two lines
auth_username=username
auth_password=drowssap
– Modify the above two directives. Add your SMTP server’s username and password.
Some SMTP servers use POP3. If yours is one of those servers, then you need further modifications.
; if your smtp server uses pop3 before smtp authentication, modify the
; following three lines
pop3_server=mail.mydomain.com
pop3_username=username
pop3_password=drowssap
– Change the values of the above three directives to fit your needs and save your file. Then restart your server and try to send a test message to your email address.
If everything is correct, you can send emails to any server now. The following snippet is a header of message sent from my localhost server:
Return-path:
Envelope-to: [email protected]
Delivery-date: Sun, 15 Jun 2009 17:18:55 +0200
Received: from [192.168.3.134] (helo=mehmett)
by host.server.com with esmtpa (Exim 4.62)
(envelope-from )
id 1MFrTy-000CQx-OY
for [email protected]; Sun, 15 Jun 2009 17:18:55 +0200
To: [email protected]
Subject: Taste email from localhost
Date: Sun, 15 Jun 2009 19:18:39 +0400
From: J Mehmett
Message-ID:
X-Priority: 3
X-Mailer: PHPMailer (phpmailer.sourceforge.net) [version 2.0.4]
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Content-Type: text/plain; charset="UTF-8"
That was easy, huh?
Final Thoughts
This is a simple mail configuration. I tested it using the SMTP settings of my host and it worked properly.
Gmail users may check Brett Shaffer’s solution, alternatively, AOL users may see KahWee’s solution.
If you have enjoyed reading this post don’t forget to share your thoughts in the comments section.
I followed your post from google and I am really very happy to admit that it works.
Method #2 worked for me like a marathon. My machine sends out messages like a real host.
Thank you any way,
– Chris
Chris: Method #2 is my favorite, too. It works well with AOL and Gmail so it’s not limited with your own SMTP server.
I am using a project management program on xampp and we couldn’t find a solution to send or receive emails through xampp. You saved our day.
Thanks
Hi. This is the greatest XAMPP/PHP/ sending emails tutorial. I was searching this for several days and I would be frustrated if I didn’t get this post. Thanks
Thanks for sharing. Method #2 worked for me.
I’m trying to use method #2 with Gmail but seem to be stuck. Did anyone who is running SMTP through Gmail have to add the pop3 settings in sendmail.ini, or just the smtp.gmail.com and login?
Thanks
RC, Gmail has restrictive security features. As I stated, this topic addresses using your own SMTP.
Anyways, this post seems to be a solution for your question.
Hi thnks for the post.. But can you let me know how should I send email from local host using my gmail account..
Ronak Sah, many people asked me this question. I will write about it in the near future.
Anyway, check this post.
I want to send mail slowly , i mean 4 0r 5 per minute , is there any rule or function or feature in any program , please let me know , is this works for sending mail to inbox
Sreekanth Reddy, there is no specific function for this job. You need to write down a PHP class that can handle such task.
Brilliant!
*JUST* what I was needing to find.
Thank you!
Cheers
Brad
Brad, Thank you very much.
boredphuck, Really? Did you double check your changes first?
doesn’t work at all!!
it seem work, but the email that i post doesnot sent to my email…
could you tell me how do i get my smtp server? maybe i placed a wrong name….
susie, Contact to the people managing your host and they will provide you your SMTP server details.
Hi J
I’m using the cformsII contact form plugin on a local WordPress installation on XAMPP (using windows vista).
Trying to get the form to send out emails.
I’ve tried both of your methods without any luck.
Any suggestions?
Thank you for your post, however when i used method 2 to send email to my gmail address, the email sent but in my gmail inbox, i found no new email. Could anyone help me with this? thanks.
Tuankaka, Please check your sendmail log file.
Method 2 🙂
Thanks a lot….it working
I use xampp on windows, what I need is not to send real mail during testing, but if there is a way that I can see the mails any where locally how they will look after sending.
is it possible?
it isn’t work for me, and make me headache for 5 days, pls help me,
Have I miss something here? pls help me
2nd method works a charm!
Thanks for the tutorial!
I use xampp on windows, with an exchange server. So your post#1 works fine for local testing.
[mail function]
; For Win32 only.
SMTP = server.company.local
smtp_port = 25
; For Win32 only.
sendmail_from = [email protected].
For all getting problems with this configuration look for this post with the mercury server. Works fine even.
http://i-tech-life.blogspot.com/2010/01/how-to-send-mail-from-localhost-with.html
If email want to be sent to a hotmail account([email protected]), is that mean SMTP have to be mx1.hotmail.com or mx2.hotmail.com? username will be [email protected] and password will be password for [email protected]?
Sorry, I am noob, not really understand what value have to put in.