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.
-
maybe this link can help you: magento.stackexchange.com/questions/318627/… if you have any question ask here in commentsRiccardo Ugolini– Riccardo Ugolini2020年09月02日 08:25:44 +00:00Commented Sep 2, 2020 at 8:25
-
try this magento.stackexchange.com/q/104044Msquare– Msquare2020年09月02日 11:03:26 +00:00Commented Sep 2, 2020 at 11:03
-
@Msquare I have added the code like this nimbusweb.me/nimbus-screenshots/… it's showing error nimbusweb.me/nimbus-screenshots/…Mohit Rane– Mohit Rane2020年09月02日 11:12:20 +00:00Commented Sep 2, 2020 at 11:12
1 Answer 1
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;
}
}
Explore related questions
See similar questions with these tags.