如何在Symfony中添加电子邮件附件


how to add an attachment to an email in Symfony?

我想在电子邮件中添加附件。我正在使用 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) {
}

您可以通过多种方式使用 swift mailer 将文档附加到电子邮件中。

来自symfony文档:

$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);

以及来自快速邮件文档的许多其他可能性。

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

$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'));