【问题标题】:Cannot Access Attach Method using SwiftMailer in Symfony 1.4无法在 Symfony 1.4 中使用 SwiftMailer 访问附加方法
【发布时间】:2015-06-04 22:31:13
【问题描述】:

我正在尝试使用 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);

【问题讨论】:

  • 那是确切的消息吗?未找到附加方法?没有关于在非对象等上找不到的任何东西?只需要确定抱歉:p
  • "method attach not found in swift_mime_message" 是我得到的确切消息
  • 我还看到 ->attach() 不存在于 Swift_Mime_Message 接口中。不知道为什么,或者下一步该做什么。
  • 你能不能var_dump($message);newInstance 行之后,attach 行之前提供输出。我觉得奇怪的是,理论上你得到了一个 Swift_Message 的实例,但在 attach,你有一个 Swift_Mime_Message 对象。
  • 感谢这些 cmets。虽然我使用了不同的方法,但它们帮助我解决了这个问题。

标签: php symfony symfony-1.4 swiftmailer


【解决方案1】:

我通过使用不同的邮寄方式解决了这个问题。我把邮件参数放在 factory.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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-14
    • 2023-04-06
    • 1970-01-01
    相关资源
    最近更新 更多