SwiftMailer不发送消息

时间:2014-12-15 15:29:55

标签: php swiftmailer

我正在学习swift,当运行我的代码时,抛出异常:

Fatal error: Call to undefined method Swift_SmtpTransport::send()

<?php

    include_once "swift_required.php";

    $subject = 'Hello from Mandrill, PHP!';
    $from = array('you@yourdomain.com' =>'Your Name');
    $to = array(
     'recipient1@example.com'  => 'Recipient1 Name',
     'recipient2@example2.com' => 'Recipient2 Name'
    );

    $text = "Mandrill speaks plaintext";
    $html = "<em>Mandrill speaks <strong>HTML</strong></em>";

    $transport = Swift_SmtpTransport::newInstance('smtp.mandrillapp.com', 587);
    $transport->setUsername('MANDRILL_USERNAME');
    $transport->setPassword('MANDRILL_PASSWORD');

    $message = new Swift_Message($subject);
    $message->setFrom($from);
    $message->setBody($html, 'text/html');
    $message->setTo($to);
    $message->addPart($text, 'text/plain');

    if ($recipients = $transport->send($message, $failures))
    {
     echo 'Message successfully sent!';
    } else {
     echo "There was an error:\n";
     print_r($failures);
    }

http://pastebin.com/vBNSxgWq

2 个答案:

答案 0 :(得分:3)

方法send在Swift_Mailer类中可用,而不在Transport中。创建邮件程序实例很容易:

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

答案 1 :(得分:2)

你错过了:

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

而不是

transport->send

它应该是:

$swift->send
相关问题