在Symfony 1.4中无法使用SwiftMailer访问附加方法

时间:2015-06-04 22:31:13

标签: php symfony symfony-1.4 swiftmailer

我正在尝试使用SwiftMailer附加文件,但是当我调用->attach()方法时,我得到:swift_mime_message中找不到方法附件

目前,邮件已发送,但没有附件。

以下控制器代码:

$transport = Swift_MailTransport::newInstance('smtp.gmail.com', 587, 'ssl')
    ->setUsername('project@notworking.com')
    ->setPassword('Work_Pls')
;

// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);

$html = $this->getPartial("email/confirmation", $model);
$subject = "Order Confirmation";

// Create the message
$message = Swift_Message::newInstance();

// Give the message a subject
$message->setSubject($subject);

// Set the From address with an associative array
$message->setFrom(array('admin@derp.com' => 'Portal Noreply'));

// Set the To addresses with an associative array
$message->setTo(array('derp@gmail.com'));

// Give it a body
$message->setBody($html);

// Optionally add any attachments
$message->attach(Swift_Attachment::fromPath('uploads/important.jpg'));

// Send the message
$mailer->send($message);

1 个答案:

答案 0 :(得分:0)

我通过使用不同的邮件方法解决了这个问题。我把邮件参数放在factories.yml中,看起来像这样:

mailer:
  class: sfMailer
  param:
    logging:           %SF_LOGGING_ENABLED%
    charset:           %SF_CHARSET%
    transport:
      class: Swift_SmtpTransport
      param:
        host:       smtp.gmail.com
        port:       587
        encryption: tls
        username:   goober@gmail.com
        password:   derpderp

然后我将控制器改为:

// Send email
$html = $this->getPartial("email/order_confirmation", $model);
$subject = "Order Confirmation";
$to = $derp->getUser()->getEmail();

$message = $this->getMailer()->compose(
    array('admin@derp.com' => 'Portal Noreply'),
    $to, $subject);
$message->setBody($html, 'text/html');
$message->attach(Swift_Attachment::fromPath('/projects/derp.jpg'));
 $sent_count = $this->getMailer()->send($message);