I keep getting this error when I try to send an e-mail in PHP:
Warning: mail() [function.mail]: Failed to connect to mailserver at \"localhost\" port 25, ver
For sending mails using php mail function is used. But mail function requires SMTP server for sending emails. we need to mention SMTP host and SMTP port in php.ini file. Upon successful configuration of SMTP server mails will be sent successfully sent through php scripts.
Change SMTP=localhost to SMTP=smtp.gmail.com
First of all, you aren't forced to use an SMTP on your localhost, if you change that localhost entry into the DNS name of the MTA from your ISP provider (who will let you relay mail) it will work right away, so no messing about with your own email service. Just try to use your providers SMTP servers, it will work right away.
php doesn't allow or think it as very difficult to send email from localhost but this issue will be solved if you upload the project to the server. and if you want to check if email sent or not on the local server then you have to download smtp4dev and install and run it. This will give you a message if your email sent successfully or there is an issue with your code.
PHP mail function can send email in 2 scenarios:
a. Try to send email via unix sendmail program At linux it will exec program "sendmail", put all params to sendmail and that all.
OR
b. Connect to mail server (using smtp protocol and host/port/username/pass from php.ini) and try to send email.
If php unable to connect to email server it will give warning (and you see such workning in your logs) To solve it, install smtp server on your local machine or use any available server. How to setup / configure smtp you can find on php.net
If you are running your application just on localhost and it is not yet live, I believe it is very difficult to send mail using this.
Once you put your application online, I believe that this problem should be automatically solved. But i think ini_set()
helps you to change the values in php.ini
during run time.