php mail() function on localhost

You need to setup a mail server on your machine for the mail function to work. If you are on Windows (which I am guessing you are from your use of WAMP) you can setup a Pegasus mail server.

Other options include using a wrapper class such as SwiftMailer or PHPMailer and using them to connect to another SMTP server such as your GMail account. Even if you go the Pegasus mail server on your own localhost route then I would still recommend using one of the two classes I have mentioned above. They give you far more flexibility and are safer.

Connecting to either your ISPs SMTP server or GMail or whatever is the easiest route out of this one.


After spending 2 days on this php mail function issue I have figured it out and would help you do same. This has wasted enough time, Let's begin...

PHP has an in-built php mail function that can be used to send mail from script but this function is somehow limited because it cannot connect you to a simple mail transfer protocol (SMTP) server. I am assuming you are using WAMP/XAMPP, haven't really tried XAMPP but you could figure that out following this:

First we would have to find a way to connect our web server(wamp) to an external SMTP. We would be using Gmail in this case but before that, we need to download/install a tool that would grab our email from the php script and push it to the Gmail SMTP.

  1. First go to http://glob.com.au/sendmail/ and click on the download sendmail.zip to download.

  2. After downloading, extract to C:\wamp\ extract it as a folder meaning it's contents should be in a folder called sendmail and can be located at C:\wamp\sendmail.

  3. Now in the send mail folder, right click on sendmail.ini and open as an administrator since we are about to modify its contents.

  4. change the following lines

     [sendmail]
     smtp_server=smtp.gmail.com
     smtp_port= 465
     smtp_ssl=ssl
     default_domain=localhost
     error_logfile=error.log
     debug_logfile=debug.log
     auth_username= enter your gmail account here        
     auth_password= enter the password for that account here
     ;pop3_server=
     ;pop3_username=
     ;pop3_password=
     ;force_sender=
     ;force_recipient=
     hostname= localhost 
    

Please be careful and do as you see above. I have deleted most of the unwanted contents from my sendmail.ini file to make things a little easier to read.

  1. Save the file
  2. Navigate to your wamp folder and move to bin\apache\apache[version here]\bin\php.ini and modify the php file as an administrator as follows:

If you are using notepad, you can use the find under the edit tab or ctrl+F and type in "mail function" to quickly navigate to the mail function part of the php.ini file.

    [mail function]
    ;For Win32 only.
    ;http://php.net/smtp
    ;SMTP = localhost
    ;http://php.net/smtp-port
    ;smtp_port = 25

    ;For Win32 only.
    ;http://php.net/sendmail-from
    ;sendmail_from = 

    ;For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
    ;http://php.net/sendmail-path
    sendmail_path = "C:\wamp\sendmail\sendmail.exe -t -i" ----> this is the changed part. 

Note: It is only the sendmail_path that you have to change to the above. Leave other settings as you see them. Your path maybe different depending on where you extracted the sendmail folder to.

  1. Save that php.ini file

  2. Move back to C:\wamp\bin and chose php\php[version here]\php.ini and edit the sendmail path as you did above and save the file as an administrator/or before you open, open as an administrator by right clicking

  3. Run wampserver as an administrator and when it turns green turn on the following:

Left click on the green icon and move to php -> php extensions -> php_sockets (click to have the black check on it)

wait for the icon to turn green again and follow the above to turn on php_openssl.

If the icon turns green it is time to turn on apaches ssl_module by moving to the apache icom when you click on the green wampserver icon. You can find this at Apache modules.

When you are done with three steps above exit and run wampserver again as an admin to effect changes.

  1. Move back to the sendmail extracted folder and right click on sendmail.exe
    Go to its properties -> compatibility -> change settings for all users -> compatibility mode [change to windows xp (service pack 3) ] -> Run this program as an administrator [check this option].

Save and Apply.
We had to do this so that each time we call the sendmail App, it would run as an admin.

  1. Now we need to configure our gmail account to work with this sendmail App. Log into gmail account and at the top right corner click on the gear to choose settings. At settings move to Forwarding and POP/IMAP tab and enable IMAP. Save changes

  2. Finally get back to your inbox, an the topmost right corner beside your email address is a little arrow pointing down, click on it to choose My Account. If you don't have that, you can try clicking on your profile picture to choose My Account from there.

Click on the Sign-in & security tab and scroll down to the bottom of the page. You would find Allow less secure apps: and turn it on. Mostly this would be off.

  1. That's all you need to do so you can now send email from your php scripts on localhost. You can now try your mail() function with the 4 minimum parameters and see. Hope this helps. Vote if it helps so i can take time to post more interesting solutions.

This is a well-known issue with using mail() on Windows, where you typically don't have a local SMTP service. As your error message says, you need to define SMTP settings in your php.ini to talk to a mail server through which you are allowed to send outbound messages, with or without authentication. See mail() docs.


There is a simple tool I use Test mail server. It saves the .eml output file in a folder and automatically opens it when a mail() function is used.