【问题标题】:What is wrong with this PHP script to send mail using Pear Mail?这个使用 Pear Mail 发送邮件的 PHP 脚本有什么问题?
【发布时间】:2011-10-30 03:55:40
【问题描述】:

我有这个脚本:

require_once "Mail.php";

 $from = "Stephen <username@nvrforget.com>";//Google apps domain
 $to = "username@gmail.com";
 $subject = "Hi!";
 $body = "Hi,\n\nHow are you?";

 $host = "mail.nvrforget.com";
 $username = "username@nvrforget.com";
 $password = "password";

 $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>");
  }

我想出了这个错误:

Non-static method Mail::factory() should not be called statically 

知道如何解决这个问题吗? Pear Mail 已安装在服务器上。

【问题讨论】:

  • 看起来正确。你确定你的 Pear Mail 库被正确包含了吗?

标签: php email smtp pear


【解决方案1】:

不应静态调用非静态方法 Mail::factory()

这是来自 PHP 的非致命通知,因为 PEAR Mail 是史前并且尚未更新为使用五年前引入的 static 关键字PHP5.

reviewing the documentation 之后,您对Mail::factory 的调用看起来完全正确且正常。

您没有告诉我们对send 的调用是成功还是失败。如果成功了,但邮件始终没有送达,请检查 SMTP 服务器日志。如果失败,实际的错误信息是什么? Mail::send 文档包含一个完整的错误列表。

您可能需要考虑使用更现代的邮件发送库,例如Swiftmailer

【讨论】:

  • 啊……很有趣。发送似乎没有成功。我会尝试 swiftmailer,因为我肯定更喜欢有一个更现代的库。谢谢。
【解决方案2】:

也许这与缺少 & 号有关?

我注意到在文档示例中,工厂的用法如下所示:

// Create the mail object using the Mail::factory method
$mail_object =& Mail::factory('sendmail', $params);

注意使用 =& 的赋值

【讨论】:

  • -1,在 PHP5 中不再需要通过引用传递对象。该语法仅适用于 PHP4。
【解决方案3】:

在所有 pear / mail 调用前添加一个 @。有时您可能会遇到 Mail::factory() 不应被称为静态错误

【讨论】:

    猜你喜欢
    • 2010-12-02
    • 2010-12-05
    • 1970-01-01
    • 2017-10-26
    • 2018-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-01
    相关资源
    最近更新 更多