简体   繁体   中英

Native php mail function don't seem to work with Gmail

I am trying to use the native PHP function to send a simple test email through gmail SMTP, but it's not working. Going through various forums including Stack Overflow and the only solutions I see are the ones that recommend third-party email libraries/frameworks/api's. Does this mean that PHP's mail() does not work with Gmail, and if so, why?

The following is the code:

<?php
$to ="mail2@yahoo.com";
$sbj ="test mail";
$msg ="testing! testing!! testing!!!";

ini_set("SMTP", "ssl://smtp.gmail.com");
ini_set("smtp_port", 465);
ini_set("sendmail_from", "mail1@gmail.com");

$header ="From: " .ini_get("sendmail_from");

mail($to, $sbj, $msg, $header);

?>

And the error message I got:

Warning: mail(): Failed to connect to mailserver at "ssl://smtp.gmail.com" port 465, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\\wamp\\www\\test\\web1\\test1.php on line 12

The best class for you is phpmailer . There are good examples for Gmail (simple and advanced).

Does this mean that PHP's mail() does not work with Gmail, and if so, why?

Gmail servers require user to authenticate before sending any message, mail() does not provide any authentication method

I use Pear along with Gmail. Works great.

http://pear.php.net/package/Mail/redirected

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM