Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP Symfony 2.8 Swiftmailer bundle 2.5,从代码中创建带有PDF附件的消息_Php_Symfony_Email_Email Attachments_Swiftmailer - Fatal编程技术网

PHP Symfony 2.8 Swiftmailer bundle 2.5,从代码中创建带有PDF附件的消息

PHP Symfony 2.8 Swiftmailer bundle 2.5,从代码中创建带有PDF附件的消息,php,symfony,email,email-attachments,swiftmailer,Php,Symfony,Email,Email Attachments,Swiftmailer,我正在尝试使用symfony2.8和SwiftMailer Bundle 2.5创建一条SwiftMailer消息,在这里我将一个带有简单HTML消息的PDF发送到一个地址。我可以发送电子邮件,但是所有示例代码都使用addPart()和attach()等方法,这些方法在Swift\u Message给出的方法列表中不存在,我找不到任何使用其他方法的示例 我从呈现的细枝模板创建PDF,然后创建一条消息将其附加到其中 $pdf = $this->get('knp_snappy.pdf')-&g

我正在尝试使用symfony2.8和SwiftMailer Bundle 2.5创建一条SwiftMailer消息,在这里我将一个带有简单HTML消息的PDF发送到一个地址。我可以发送电子邮件,但是所有示例代码都使用addPart()和attach()等方法,这些方法在
Swift\u Message
给出的方法列表中不存在,我找不到任何使用其他方法的示例

我从呈现的细枝模板创建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=\Swift\u attachment::newInstance($pdf,$pdf\u name,'application/pdf')

两者

方法根本不存在

奇怪的是,突出显示的消息说

Method 'attach' not found in class \Swift_Mime_MimePart less...
Referenced method is not found in subject class.

但是我没有成功地弄清楚这是从哪里来的,或者如何正确地调用message类,因为
Swift\u Mime\u MimePart
类在类层次结构中有好几层,从来没有在代码中直接调用或引用过,它们都以
return$this
结尾,它将对象的实例作为泛型类型返回,该方法位于层次结构的最底层

由于PHP中没有泛型类型加载,setBody返回
Swift\u Mime\u MimePart
,而其他方法返回
Swift\u Mime\u SimpleMessage
,而不是
?扩展Swift\u Mime\u MimePart
?扩展Swift\u Mime\u SimpleMessage
。由于示例将消息设置在同一个链中,并且
setBody
是最后调用的方法,
$message
现在是
Swift\u Mime\u MimePart
而不是
Swift\u message
方法和其他方法所在的位置

因此,解决方案相当简单;将通话放在单独的线路上,使$message保持为
Swift\u消息

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

我觉得这里缺少一些代码,这将显示问题所在。从创建
$message
变量到尝试运行
attach()
方法,您能包含所有代码吗?@Augwa-它几乎是直接从Swiftmailer文档本身复制的,这就是为什么我如此困惑的原因。我创建消息,然后立即尝试
attach()
。应该有代码吗?我也试着重新安装了整个包。我可以试着回滚到一个旧版本,看看是否可以修复它,但是很多其他捆绑包都依赖于这个版本,比如FOSUserBundle,所以我认为这本身会带来一系列问题。我不认为这是问题所在。你说你说这是非常直接的,但这并不是确切的意思。我有99.9%的把握,如果你发布了所有请求的代码,那么错误就会暴露出来是否出错?
Method 'attach' not found in class \Swift_Mime_MimePart less...
Referenced method is not found in subject class.
$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);