问题:使用 Gmail 服务器发送电子邮件

发布于 2024-11-05 03:33:53 字数 1232 浏览 0 评论 0原文

一段时间以来,我尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酷到爆炸 2024-11-12 03:33:53

如果您可以投递到本地 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文