PHP使用Gmail发送邮件

时间:2010-07-30 16:34:02

标签: php email gmail

嘿伙计们,我正在尝试使用我的Gmail帐户发送电子邮件,但由于某种原因我收到了错误消息。有谁知道为什么它会给我这个错误? 脚本:

<?php

    set_include_path("/usr/share/php"); 

    include("Mail.php");

     $from = "email <email address>";
     $to = "<Emailaddress>";
     $subject = "Hi!";
     $body = "Hi,\n\nHow are you?";

     $host = "smtp.gmail.com";
     $username = "myGmailusername@gmail.com";
     $password = "myPassword";

     $headers = array ('From' => $from,
       'To' => $to,
       'Subject' => $subject);
     $smtp = Mail::factory('smtp',
       array ('host' => $host,
         'auth' => true,
         'username' => $username,
         'password' => $password));

     $mail = $smtp->send($to, $headers, $body);

     if (PEAR::isError($mail)) {
       echo("<p>" . $mail->getMessage() . "</p>");
      } else {
       echo("<p>Message successfully sent!</p>");
      }
     ?>

当我转到它时,页面会解析,但会出现以下错误:

Failed to connect to smtp.gmail.com:25 [SMTP: Failed to connect socket: Connection refused (code: -1, response: )]

2 个答案:

答案 0 :(得分:1)

答案 1 :(得分:1)

是的,它似乎连接到错误的端口。当它连接到465时,它连接到端口25.阅读文档,了解如何使用您的类设置端口。它还需要使用ssl进行连接,这是一个如何使其工作的示例:

http://neo22s.com/send-email-using-gmail-and-php/