【问题标题】:SwiftMailer, Fatal error: Call to a member function Send() on a non-objectSwiftMailer,致命错误:调用非对象上的成员函数 Send()
【发布时间】:2014-02-10 18:22:05
【问题描述】:

我正在尝试使用 Swiftmailer 发送电子邮件,但收到以下错误:

致命错误:在第 20 行对 x 中的非对象调用成员函数 send()

这是我的代码:

<?php

require_once('swift/lib/swift_required.php');

$to = $_POST['email'];
if(empty($to)) {
    header("Location: index.php?error=empty");
}

echo $to;

$body="This is my message!";

$message = Swift_Message::newInstance('Some subject')
    ->setFrom(array('example@example.com' => 'John Doe'))
    ->setTo($to)
    ->setBody($body);
$message->attach(Swift_Attachment::fromPath("myfile.pdf"));

$result = $mailer->send($message); // line 20

?>

谁能帮帮我?

【问题讨论】:

  • $mailer 在你的代码中应该来自哪里……?

标签: php email object swiftmailer


【解决方案1】:

错误表明 $mailer 对象从未被创建,因此您不能在其上调用任何对象。我相信您在第 20 行之前缺少这样的内容:

$transport = Swift_SmtpTransport::newInstance('smtp.example.org', 25)
  ->setUsername('your username')
  ->setPassword('your password');

$mailer = Swift_Mailer::newInstance($transport);

【讨论】:

    猜你喜欢
    • 2016-09-20
    • 1970-01-01
    • 1970-01-01
    • 2013-11-25
    • 2012-05-30
    • 2016-08-30
    • 2015-02-06
    • 2012-05-14
    • 2013-11-19
    相关资源
    最近更新 更多