Tag Apache

How to Send Email with XAMPP on Windows XP

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 = me@example.com

– 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 = something@server.com

– Replace mail.server.com and something@server.com 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: jmehmett@host.com
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 jmehmett@host.com; Sun, 15 Jun 2009 17:18:55 +0200
To: jmehmett@host.com
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.