0

I have installed the pdf invoice Extension in Magento 2.2.11. I'm getting this error while placing the order,

Call to undefined method Magento\Framework\Mail\Message\Interceptor::createAttachment() in /app/code/MyVendor/PdfInvoice/Model/TransportBuilder.php:19

MyVendor/PdfInvoice/Model/TransportBuilder.php

<?php 
namespace MyVendor\PdfInvoice\Model;
class TransportBuilder extends \Magento\Framework\Mail\Template\TransportBuilder
{
 /**
 * @param Api\AttachmentInterface $attachment
 */
 public function addAttachment($pdfString, $type = '')
 {
 $fileattached = @file_get_contents($pdfString);
 $this->message->createAttachment(
 $fileattached,
 'application/pdf',
 \Zend_Mime::DISPOSITION_ATTACHMENT,
 \Zend_Mime::ENCODING_BASE64,
 $type.'.pdf'
 );
 return $this;
 }
 /**
 * clear header
 * @param $headername
 */
 public function clearHeader($headerName)
 {
 if (isset($this->_headers[$headerName])) {
 unset($this->_headers[$headerName]);
 }
 return $this;
 }
}

When I replace this below code,

$this->message->createAttachment(
 $fileattached,
 'application/pdf',
 \Zend_Mime::DISPOSITION_ATTACHMENT,
 \Zend_Mime::ENCODING_BASE64,
 $type.'.pdf'
 );

with this code

$attachment = new \Zend\Mime\Part($fileattached);
 $attachment->type = 'application/pdf';
 $attachment->disposition = \Zend_Mime::DISPOSITION_ATTACHMENT;
 $attachment->encoding = \Zend_Mime::ENCODING_BASE64;
 $attachment->filename = $type.'.pdf';
 return $attachment;

Emails are sending but the attachment is not included in the mail.

I have tried the below solution but didn't work.

Magento 2.3 custom email attachment not working

https://github.com/mageplaza/magento-2-email-attachments/issues/4

Let me know if anyone has any ideas.

asked Sep 2, 2020 at 6:00
3

1 Answer 1

0

Finally, I have figured it out. I made it work in Magento 2.2.11. I Had to make some changes to the TransportBuilder.php file.

Followed this link https://magento.stackexchange.com/a/274723/73212 and now it's working.

TransportBuilder.php

<?php
namespace MyVendor\PdfInvoice\Model;
use Magento\Framework\Mail\MessageInterface;
use Magento\Framework\Mail\MessageInterfaceFactory;
use Magento\Framework\Mail\Template\FactoryInterface;
use Magento\Framework\Mail\Template\SenderResolverInterface;
use Magento\Framework\Mail\TransportInterfaceFactory;
use Magento\Framework\ObjectManagerInterface;
use Zend\Mime\Mime;
use Zend\Mime\Part as MimePart;
use Zend\Mime\PartFactory as MimePartFactory;
use Zend\Mime\Message as MimeMessage;
use Zend\Mime\MessageFactory as MimeMessageFactory;
class TransportBuilder extends \Magento\Framework\Mail\Template\TransportBuilder
{
 /** @var MimePart[] */
 private $parts = [];
 /** @var MimeMessageFactory */
 private $mimeMessageFactory;
 /** @var MimePartFactory */
 private $mimePartFactory;
 public function __construct(
 FactoryInterface $templateFactory,
 MessageInterface $message,
 SenderResolverInterface $senderResolver,
 ObjectManagerInterface $objectManager,
 TransportInterfaceFactory $mailTransportFactory,
 MimePartFactory $mimePartFactory,
 MimeMessageFactory $mimeMessageFactory,
 MessageInterfaceFactory $messageFactory = null
 ) {
 parent::__construct(
 $templateFactory,
 $message,
 $senderResolver,
 $objectManager,
 $mailTransportFactory,
 $messageFactory
 );
 $this->mimePartFactory = $mimePartFactory;
 $this->mimeMessageFactory = $mimeMessageFactory;
 }
 protected function prepareMessage()
 {
 parent::prepareMessage();
 $mimeMessage = $this->getMimeMessage($this->message);
 foreach ($this->parts as $part) {
 $mimeMessage->addPart($part);
 }
 $this->message->setBody($mimeMessage);
 return $this;
 }
 public function addAttachment($body, $type = '')
 {
 $fileattached = @file_get_contents($body);
 $mimeType = 'application/pdf';
 $disposition = Mime::DISPOSITION_ATTACHMENT;
 $encoding = Mime::ENCODING_BASE64;
 $this->parts[] = $this->createMimePart($fileattached, $mimeType, $disposition, $encoding, $type.'.pdf');
 return $this;
 }
 private function createMimePart(
 $content,
 $mimeType = 'application/pdf',
 $disposition = Mime::DISPOSITION_ATTACHMENT,
 $encoding = Mime::ENCODING_BASE64,
 $filename = null
 ) {
 /** @var MimePart $mimePart */
 $mimePart = $this->mimePartFactory->create(['content' => $content]);
 $mimePart->setType($mimeType);
 $mimePart->setDisposition($disposition);
 $mimePart->setEncoding($encoding);
 $mimePart->setFileName($filename);
 if ($filename) {
 $mimePart->setFileName($filename);
 }
 return $mimePart;
 }
 private function getMimeMessage(MessageInterface $message)
 {
 $body = $message->getBody();
 if ($body instanceof MimeMessage) {
 return $body;
 }
 /** @var MimeMessage $mimeMessage */
 $mimeMessage = $this->mimeMessageFactory->create();
 if ($body) {
 $mimePart = $this->createMimePart($body, 'application/pdf', Mime::DISPOSITION_ATTACHMENT);
 $mimeMessage->setParts([$mimePart]);
 }
 return $mimeMessage;
 }
 /**
 * clear header
 * @param $headername
 */
 public function clearHeader($headerName)
 {
 if (isset($this->_headers[$headerName])) {
 unset($this->_headers[$headerName]);
 }
 return $this;
 }
}
answered Sep 2, 2020 at 12:57

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.