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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10846210/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:12:57  来源:igfitidea点击:

how to add an attachment to an email in Symfony?

phpemailsymfony1symfony-1.4swiftmailer

提问by nic

I want to add an attachment to an email. I am using sfmailer class.

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

Here I have given my code below:

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

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

}

回答by j0k

You have several options to attach a document to an email using swift mailer.

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

From the symfony doc:

symfony 文档

$message = Swift_Message::newInstance()
  ->setFrom('[email protected]')
  ->setTo('[email protected]')
  ->setSubject('Subject')
  ->setBody('Body')
  ->attach(Swift_Attachment::fromPath('/path/to/a/file.zip'))
;

$this->getMailer()->send($message);

And many others possibility from the swift mailer doc.

以及来自swift mailer doc 的许多其他可能性。

回答by Danil Pyatnitsev

Also you can attach a file by resource.

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

$message = Swift_Message::newInstance()
  ->setFrom('[email protected]')
  ->setTo('[email protected]')
  ->setSubject('Subject')
  ->setBody('Body')
  ->attach(Swift_Attachment::newInstance($content, 'invoice.pdf','application/pdf'));