[英]PHP Symfony 2.8 Swiftmailer bundle 2.5, create message with PDF attachment from code
I'm trying to create a SwiftMailer message using Symfony 2.8 and the SwiftMailer Bundle 2.5, where I send a PDF with a simple HTML message to an address. 我正在尝试使用Symfony 2.8和SwiftMailer Bundle 2.5创建SwiftMailer消息,在其中我将带有简单HTML消息的PDF发送到地址。 I can send emails, however all the example code uses methods such as addPart() and attach() which don't exist in the list of methods given by
Swift_Message
and I can't find any example that uses anything else. 我可以发送电子邮件,但是所有示例代码都使用诸如addPart()和attach()之类的方法,这些方法在
Swift_Message
给出的方法列表中不存在,并且找不到任何使用其他方法的示例。
I create a PDF from a rendered Twig template and then create a message to attach it to 我从渲染的Twig模板创建PDF,然后创建一条消息以将其附加到
$pdf = $this->get('knp_snappy.pdf')->getOutputFromHtml($response);
$message = \Swift_Message::newInstance()
->setSubject('Hello Email')
->setFrom('send@example.com')
->setTo('recipient@example.com')
->setBody(
$this->renderView(
// app/Resources/views/Emails/registration.html.twig
'Emails/registration.html.twig',
array('name' => "test")
),
'text/html'
);
Attachment: $attachment = \\Swift_Attachment::newInstance($pdf, $pdf_name, 'application/pdf');
附件:
$attachment = \\Swift_Attachment::newInstance($pdf, $pdf_name, 'application/pdf');
Both 都
$message->addPart($attachment, $contentType = "application/pdf", $charset = null);
and 和
$message->attach(\Swift_Attachment::fromPath('/path/to/image.jpg')->setFilename('myfilename.jpg'));
methods simply do not exist. 方法根本不存在。
Strangely enough, the highlight message says 奇怪的是,高亮消息说
Method 'attach' not found in class \Swift_Mime_MimePart less...
Referenced method is not found in subject class.
But I've had no success figuring out where this comes from or how I can call the message class properly given that the Swift_Mime_MimePart
class is several layers upward in the class hierarchy and is never called or referenced directly in the code. 但是,鉴于
Swift_Mime_MimePart
类在类层次结构中位于向上几层,并且从未在代码中直接调用或引用过,因此我无法成功地弄清消息的来源或如何正确调用消息类。
Looking at all the individual methods called, they all end in return $this
, which returns the instance of the object as the generic type in which the method is located in lowest the hierarchy. 查看所有被调用的方法,它们都以
return $this
结尾,该返回值将对象的实例作为泛型类型返回,该方法位于该类的最低层次结构中。
Since there's no generic type loading in PHP, setBody returns Swift_Mime_MimePart
while the other methods return Swift_Mime_SimpleMessage
, instead of ? extends Swift_Mime_MimePart
由于PHP中没有加载通用类型,因此setBody返回
Swift_Mime_MimePart
而其他方法返回Swift_Mime_SimpleMessage
,而不是? extends Swift_Mime_MimePart
? extends Swift_Mime_MimePart
or ? extends Swift_Mime_SimpleMessage
? extends Swift_Mime_MimePart
或? extends Swift_Mime_SimpleMessage
? extends Swift_Mime_SimpleMessage
. ? extends Swift_Mime_SimpleMessage
。 Because the example sets the message in the same chain and setBody
is the last method called, $message
is now a Swift_Mime_MimePart
instead of the Swift_Message
where the attach()
method and others are located. 因为该示例将消息设置在同一链中,并且
setBody
是最后一个调用的方法,所以$message
现在是Swift_Mime_MimePart
而不是attach()
方法和其他方法所在的Swift_Message
。
The solution is therefore pretty simple; 因此,解决方案非常简单。 put the calls on separate lines so $message stays a
Swift_Message
将调用放在单独的行上,以便$ message保持为
Swift_Message
$message = \Swift_Message::newInstance();
$message->setSubject('Hello Email')
$message->setFrom('send@example.com')
$message->setTo('recipient@example.com')
$message->setBody(
$this->renderView(
// app/Resources/views/Emails/registration.html.twig
'Emails/registration.html.twig',
array('name' => "test")
),
'text/html'
);
$attachment = \Swift_Attachment::newInstance($pdf, $pdf_name, 'application/pdf');
$message->attach($attachment);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.