简体   繁体   中英

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. 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.

I create a PDF from a rendered Twig template and then create a message to attach it to

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

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.

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.

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 ? extends Swift_Mime_MimePart or ? 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.

The solution is therefore pretty simple; put the calls on separate lines so $message stays a 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM