Sending PHP mail from localhost
Here I provide instructions on sending PHP mail from localhost, that is, from a local development machine. I only tested this on Windows, but it should work with any localhost setting.
In trying to test PHP code on Windows development machine using a WAMP stack like AMPPS or XAMPP, you might want to send mail from PHP. This should be pretty easy to do from code hosted on a hosting provider (for example, my host 1&1 provides simple instructions), but it wasn’t so easy for me from a local WAMP stack.
Fortunately, I came across a blog post that tackled this problem. Unfortunately, the solution provided didn’t quite work for me. However, based on the core information provided along with some clarifications and tips from the comments, here’s what finally worked for me using AMPPS (it should probably work with any other XAMP stack. Instead of making any changes to the php.ini file like the blog post I referenced said, you can add the following lines to any PHP file from which you send mail:
ini_set( 'SMTP', 'smtp.yourisp.net' ); // must be set to your own local ISP ini_set( 'smtp_port', '25' ); // assumes no authentication (passwords) required ini_set( 'sendmail_from', 'email@example.com' ); // can be any e-mail address, but must be set $to = 'firstname.lastname@example.org'; // the address that will receive the e-mail $subject = 'This is the subject of the e-mail'; $body = "This is the message body of the e-mail"; $headers = 'MIME-Version: 1.0' . PHP_EOL; // PHP_EOL automatically inserts \r or \n or \r\n as appropriate $headers .= 'Content-type: text/plain; charset=iso-8859-1' . PHP_EOL; // for HTML e-mail, use text/html $headers .= 'From: email@example.com'; // This instruction overrides sendmail_from above. IMPORTANT: do not include PHP_EOL here mail( $to, $subject, $body, $headers ); // sends the e-mail
Note the following:
- This works only within your ISP’s network connection (in my case, at home connected to my own wifi network), since that’s the only context that doesn’t require SMTP authentication. If you change network connection, you’ll need to change the SMTP settings accordingly.
- “firstname.lastname@example.org” really means any e-mail address at all. Since you override it in the headers section with the real From address, it doesn’t matter what you put there. However, it must be set to something, otherwise the e-mail sending will not work.
- IMPORTANT: if you move your e-mail code to a production server on a web host, you must comment out or delete the three ini_set instructions, as your web host’s defaults should work automatically.
- If you get the following warning:
Warning: mail() [function.mail]: It is not safe to rely on the system's timezone settings. ...
then enter the following line anywhere before the
date_default_timezone_set ( 'America/Montreal' ); // Timezone from http://www.php.net/manual/en/timezones.php
You can find your appropriate time zone code from the official PHP manual.
Nice post.. This is very helpful to learn about mail() function. Thanks once again.
Nice post.This is very helpful to learn about mail()function . And thanks once again.
This worked for me. Thanks so much.