问题:使用 Gmail 服务器发送电子邮件
一段时间以来,我尝试使用 php 脚本和 gmail 服务器创建和发送自动电子邮件,但总是收到此错误:
警告:mail() [function.mail]:无法连接到“ssl://smtp.gmail.com”端口 465 的邮件服务器,请验证 php.ini 中的“SMTP”和“smtp_port”设置或使用ini_set() 位于 C:\xampp\php\PEAR\Mail.php 第 141 行
这是我的代码:
require_once ("mail.php");
ini_set("SMTP","ssl://smtp.gmail.com");
ini_set("SMTP_PORT", 465);
$to = $sqlmail['email'];
$from = $fromemail['email'];
$body = "Hello, \n\n";
$body .= "This is a request to borrow the following book\n";
$body .= "Title: $title\n";
$body .= "Author: $author\n";
$body .= "Year: $year\n";
$body .= "From the user $_SESSION[username]\n";
$subject = $title . " " . $author . " " . $year;
$host = "ssl://smtp.gmail.com";
$port = "465";
$username = "slesher.gmail.com";
$password = "xxxxxxxx";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = new Mail();
$smtp ->factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
For some time I was trying to create and send automatic email using php script and gmail server and I always get this error:
Warning: mail() [function.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:\xampp\php\PEAR\Mail.php on line 141
Here is my code:
require_once ("mail.php");
ini_set("SMTP","ssl://smtp.gmail.com");
ini_set("SMTP_PORT", 465);
$to = $sqlmail['email'];
$from = $fromemail['email'];
$body = "Hello, \n\n";
$body .= "This is a request to borrow the following book\n";
$body .= "Title: $title\n";
$body .= "Author: $author\n";
$body .= "Year: $year\n";
$body .= "From the user $_SESSION[username]\n";
$subject = $title . " " . $author . " " . $year;
$host = "ssl://smtp.gmail.com";
$port = "465";
$username = "slesher.gmail.com";
$password = "xxxxxxxx";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = new Mail();
$smtp ->factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您可以投递到本地 Postfix 并让 Postfix 投递到 Gmail,您可以尝试该解决方案,我已经 前段时间写过博客。
If you can deliver to a local Postfix and let Postfix deliver to Gmail, you can try the solution, I've blogged about some time ago.