0
votes

I use the PHP mail() function in a program to send email. This is the code:

<?php

ini_set ('SMTP', 'my.domain');
mail ("[email protected]", "PHP mail function", "Body of email", "From:[email protected]");

?>

I get this error:

Warning: 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 C:\mail.php on line 41

I have made sure the SMTP and smtp_port setting in php.ini are set:

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

Why am I receiving this error?

2
Have you tried configuring the smtp server in your php.ini file. - Nagama Inamdar
Take a look at phpinfo() to ensure your SMTP settings are actually what you think they are. Sometimes you edit PHP.ini and due to a syntax error or unprintable character, your settings don't load. Or, you edited the wrong file. - Brad
Sounds like your server doesn't have an SMTP service running, which is not at all surprising for a Windows machine (evidenced by the C:\mail.php). You need to setup access to an SMTP service, probably a 3rd party one (like login and send with Gmail). It is very highly recommended not to use mail() and instead use a class like PHPMailer. phpmailer.worxware.com - Michael Berkowski
Dozens of things can go wrong when trying to send mail, the first of which is not having an SMTP service available to send through. Using PHPMailer will help solve at least a large number of those dozens of things. The rest are wrapped in DNS settings, spam filters, careful header construction, etc. It's a minefield. - Michael Berkowski
Also make sure you restart your services after editing config files. - Rimble

2 Answers

0
votes

You need to make sure SMTP is running on the machine. Either that or point your application to another SMTP host.

0
votes
If you're running windows, install fakesendmail 
http://glob.com.au/sendmail/

If you're on *NIX install sendmail
 on Linux
  issue > `yum install sendmail`          //Fedora, Centos
  issue > `apt-get install sendmail`      //Debian, Ubuntu

Once you've done that, sending mail and using the mail function in php shouldn't be an issue. 

In addition, you can check your maillog, which is extremely useful 

 issue> `tailf /var/log/maillog`

you may need `sudo` permission if you don't have root privileges on linux or unix