【问题标题】:how to add an attachment to an email in Symfony?如何在 Symfony 中向电子邮件添加附件?
【发布时间】:2012-06-06 10:54:39
【问题描述】:

我想在电子邮件中添加附件。我正在使用 sfmailer 类。

这里我在下面给出了我的代码:

$mail_body = '<p>custom html mail content</p>';
$message = Swift_Message::newInstance('Message title')
  ->setFrom(array('sender'))
  ->setTo(array('receiver'))
  ->setBody($mail_body, 'text/html', 'utf-8');

try {
  $this->getMailer()->send($message);
}
catch(Exception $e) {

}

【问题讨论】:

标签: php email symfony1 symfony-1.4 swiftmailer


【解决方案1】:

您还可以按资源附加文件。

$message = Swift_Message::newInstance()
  ->setFrom('from@example.com')
  ->setTo('to@example.com')
  ->setSubject('Subject')
  ->setBody('Body')
  ->attach(Swift_Attachment::newInstance($content, 'invoice.pdf','application/pdf'));

【讨论】:

    【解决方案2】:

    您有多种选择可以使用 swift mailer 将文档附加到电子邮件中。

    来自the symfony doc

    $message = Swift_Message::newInstance()
      ->setFrom('from@example.com')
      ->setTo('to@example.com')
      ->setSubject('Subject')
      ->setBody('Body')
      ->attach(Swift_Attachment::fromPath('/path/to/a/file.zip'))
    ;
    
    $this->getMailer()->send($message);
    

    还有来自the swift mailer doc的许多其他可能性。

    【讨论】:

      猜你喜欢
      • 2011-02-03
      • 2011-08-04
      • 2020-03-06
      • 2023-03-22
      • 2023-03-21
      • 2019-12-02
      • 1970-01-01
      • 1970-01-01
      • 2019-05-05
      相关资源
      最近更新 更多