How to configure PHP to send e-mail?
I need to send mail to the users of my website using php script. I have tried using mail function in php.
My code is as follows:
$to = "myweb@gmail.com"; $subject = "Test mail"; $message = "My message"; $from = "webp@gmail.com"; $headers = "From:" . $from; mail($to,$subject,$message,$headers);
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().
Please tell me what address to include in the $from variable. Do I need a smtp server for this? How do I send mails using a localhost? Please tell me what exactly to edit in the php.ini file I am new to all this.. Please help me..
I take it you’re on a Windows box? You’ll need to, as specified in the error message, define your SMTP server’s address and port #. You’re trying to connect to a local SMTP server, which you do not have.
Just thought i would mention XAMPP can use mailtodisk that works well on localhost for development
8 Answers 8
require('./PHPMailer/class.phpmailer.php'); $mail=new PHPMailer(); $mail->CharSet = 'UTF-8'; $body = 'This is the message'; $mail->IsSMTP(); $mail->Host = 'smtp.gmail.com'; $mail->SMTPSecure = 'tls'; $mail->Port = 587; $mail->SMTPDebug = 1; $mail->SMTPAuth = true; $mail->Username = 'me.sender@gmail.com'; $mail->Password = '123!@#'; $mail->SetFrom('me.sender@gmail.com', $name); $mail->AddReplyTo('no-reply@mycomp.com','no-reply'); $mail->Subject = 'subject'; $mail->MsgHTML($body); $mail->AddAddress('abc1@gmail.com', 'title1'); $mail->AddAddress('abc2@gmail.com', 'title2'); /* . */ $mail->AddAttachment($fileName); $mail->send();
You need to have a smtp service setup in your local machine in order to send emails. There are many available freely just search on google.
If you own a server or VPS upload the script and it will work fine.
You won’t be able to send a message through other people mail servers. Check with your host provider how to send emails. Try to send an email from your server without PHP, you can use any email client like Outook. Just after it works, try to configure PHP.ini with your email client SMTP (sending e-mail) configuration.
Here’s the link that gives me the answer and we use gmail:
Install the «fake sendmail for windows». If you are not using XAMPP you can download it here: http://glob.com.au/sendmail/sendmail.zip
Modify the php.ini file to use it (commented out the other lines):
mail function
SMTP = smtp.gmail.com smtp_port = 25 For Win32 only. sendmail_from = @gmail.com
You may supply arguments as well (default: sendmail -t -i ).
sendmail_path = "C:\xampp\sendmail\sendmail.exe -t"
(ignore the «Unix only» bit, since we actually are using sendmail)
You then have to configure the «sendmail.ini» file in the directory where sendmail was installed:
sendmail
smtp_server=smtp.gmail.com smtp_port=25 error_logfile=error.log debug_logfile=debug.log auth_username= auth_password= force_sender=@gmail.com
configure your php.ini like this
SMTP = smtp.gmail.com [mail function] ; XAMPP: Comment out this if you want to work with an SMTP Server like Mercury ; SMTP = smtp.gmail.com ; smtp_port = 465 ; For Win32 only. ; http://php.net/sendmail-from ;sendmail_from = postmaster@localhost
To fix this, you must review your PHP.INI, and the mail services setup you have in your server.
But my best advice for you is to forget about the mail() function. It depends on PHP.INI settings, it’s configuration is different depending on the platform (Linux or Windows), and it can’t handle SMTP authentication, which is a big trouble in current days. Too much headache.
Use «PHP Mailer» instead (https://github.com/PHPMailer/PHPMailer), it’s a PHP class available for free, and it can handle almost any SMTP server, internal or external, with or without authentication, it works exactly the same way on Linux and Windows, and it won’t depend on PHP.INI settings. It comes with many examples, it’s very powerful and easy to use.
On anything other than MSWindows it doesn’t use SMTP — so why should it implement SMTP authentication?
@symcbean Because in MANY situations you need to. PHP Mailer is just much better: it allows you to use security if you need to, but it can work without it. However, the mail() function won’t allow it in any way. So, the choice is clear. Go see, for example, what WordPress and other CMS use to send their emails: it’s PHP Mailer, and NOT mail().
I think you’re missing the point PHP is not a webserver. PHP is not an MTA. There are lots of tools to extend PHP’s functionality to support different protocols hence it DOES NOT NEED to do those things. If you want a single tool which implements everything badly then PHP is not a good choice. I have no problem with you recommending phpmailer, nor would I criticise you for suggesting a smart relay which support SSMTP / SMTP/TLS / SMTP auth / pop before SMTP. but it’s absurd to criticize PHP for not implementing functionality that is better implemented elsewhere and trivial to integrate.
Am I criticizing PHP? I’m just saying the mail() function lacks some important features, just like you did in your own answer, and I recommended a free class, written in PHP, that does everything he may ever need, just like you did, after me.
This will not work on a local host, but uploaded on a server, this code should do the trick. Just make sure to enter your own email address for the $to line.
Title
Name:
'.$name.'
Email:
'.$email.'