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

时间:2011-05-05 13:18:28

标签: php gmail

有一段时间我试图使用php脚本和gmail服务器创建和发送自动电子邮件,我总是收到此错误:

  

警告:mail()[function.mail]:无法连接到“ssl://smtp.gmail.com”端口465的邮件服务器,验证php.ini中的“SMTP”和“smtp_port”设置或使用第141行的C:\ xampp \ php \ PEAR \ Mail.php中的ini_set()

这是我的代码:

   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);

1 个答案:

答案 0 :(得分:0)

如果你可以发送到本地Postfix并让Postfix传送到Gmail,你可以尝试解决方案,我前一段时间blogged about