php mail() function on localhost
Asked Answered
H

6

15

I have problem with php mail() function on localhost server. I can't test my email application on localhost when i'm trying to send emails with php function mail().

I'm getting this 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 F:\wamp\www\product\ajax.php on line 64

I'm using WAMP server and if anyone knows solution please help me.

Hautemarne answered 17/3, 2011 at 17:27 Comment(2)
Do you even have a mail server installed?Highpowered
#4596230Fecundity
D
14

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.

Derivative answered 17/3, 2011 at 17:30 Comment(1)
+1 for swift or phpmailer because of the more flexibility they add to your project instead of relying in your servers mail() configOpenhearted
I
10

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.
Isoniazid answered 25/10, 2015 at 19:3 Comment(2)
Very extensive answer, it worked. Thank you very much @Isoniazid !!Isreal
Worked perfectly for me, in Windows 10 with XAMPP. Since XAMPP comes with sendmail, I skipped steps 1, 2, 9, and 10.Campinas
R
1

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.

Rimarimas answered 17/3, 2011 at 17:31 Comment(0)
P
1

The easiest way I have found to create a mail sever in windows is to install Python and then use the inbuilt SMTP debug server with the following one-liner:

python -m smtpd -c DebuggingServer -n localhost:25

On Windows, I have found that it sometimes has strange behaviour with localhost and 127.0.0.1 so, if all else fails, you could trying finding your IP address using the 'ipconfig' command (at the DOS command prompt) and you might see something like:

c:\> ipconfig

-- stuff cut here ---

Wireless LAN adapter WiFi:

Connection-specific DNS Suffix  . :
Link-local IPv6 Address . . . . . : fe80::856d:fd33:3e01:e656%9
IPv4 Address. . . . . . . . . . . : 192.168.8.111
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 192.168.8.1

and, let's say you find you ip address is 192.168.8.11 (as I did in the example above), then changing the command to look like:

python -m smtpd -c DebuggingServer -n 192.168.8.11:25

And setting the following lines in your php.ini file:

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

And, this has worked for me on occasions where nothing else does!

I also note you can check if you local mail server is working by typing the command:

C:/> telnet localhost 25

or for my second configuration:

C:/> telnet 192.168.8.111 25

Telnet comes for free on Linux but to get it on windows you have to go to "Programs and Features" and then click "Turn Windows feaures on and off" (from the little panel on the left) and then check the box for "Telnet Client" in the list of Windows Features.

When you run the telnet command you will see something like this if the server is not running:

C:\Projects>telnet localhost 25
Connecting To localhost...Could not open connection to the host, on port 25: Connect failed

But, if it is all working you should get the screen cleared and a "server identifier" string back from the mail server. On my machine this looked like:

220 ZOOT Python SMTP proxy version 0.3     

If you are really keen you can type some SMTP commands and send a mail here or you can type 'quit' to disconnect from the server.

Premonish answered 17/9, 2019 at 4:53 Comment(0)
R
1

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.

Riegel answered 19/7, 2020 at 20:26 Comment(1)
If any one is coming here after going through whole trouble of trying to mail() from non-smtp supported system this tool saved headache.Outlay
F
0

Try to setup the mail server for localhost. Otherwise it'll not work in localhost.

Foggia answered 20/10, 2014 at 10:56 Comment(1)
this should be the accepted answer. it's totally workingWatchcase

© 2022 - 2024 — McMap. All rights reserved.