1

Can anyone help us on Add Attachment with email Magento 2.3? We have tried but face the same issue like this.

Fatal error: Uncaught Error: Call to undefined method Magento\Framework\Mail\Message\Interceptor::createAttachment() in Mail/TransportBuilder.php

Thank you in advance.

2

https://extait.com/blog/how-to-send-email-with-attachment-in-magento-2-3/

As from above link add encoding concept..

 public function setBodyAttachment($content, $fileName, $fileType)
{
    $attachmentPart = $this->partFactory->create();
    $attachmentPart->setContent($content)
        ->setType($fileType)
        ->setFileName($fileName)
        ->setEncoding(Mime::ENCODING_BASE64) /*Add this*/
        ->setDisposition(Mime::DISPOSITION_ATTACHMENT);

    $this->parts[] = $attachmentPart;

    return $this;
}
0

I gave the advice there already. Try the method described here - https://extait.com/blog/how-to-send-email-with-attachment-in-magento-2-3/ as well as links provided to GitHub to see a sample module.

0

1) in your custom transport builder add the below function for atatchment:

<?php namespace Magebees\Demo\Model\Mail;

class TransportBuilder extends \Magento\Framework\Mail\Template\TransportBuilder
{
    /**
     * @param Api\AttachmentInterface $attachment
     */
    public function addAttachment($pdfString,$filename)
    {
       $attachment = new \Zend\Mime\Part($pdfString);
            $attachment->type = \Zend_Mime::TYPE_OCTETSTREAM;
            $attachment->disposition = \Zend_Mime::DISPOSITION_ATTACHMENT;
            $attachment->encoding = \Zend_Mime::ENCODING_BASE64;
            $attachment->filename = $filename;
        return $attachment;
}
}

2) Then where you have used
$transport = $this->_transportBuilder->getTransport(); in that file add below code for send mail

$html='my testing html';            
            $bodyMessage = new \Zend\Mime\Part($html);
            $bodyMessage->type = 'text/html';
            $attachment=$this->_transportBuilder->addAttachment($pdfString,$pdf_name);      
            $bodyPart = new \Zend\Mime\Message();
            $bodyPart->setParts(array($bodyMessage,$attachment));
            $transport->getMessage()->setBody($bodyPart);
 $transport->sendMessage();
0

Fixed this issue by extending Magento\Framework\Mail\Message and adding new function createCustomAttachment in this, also edited the createHtmlMimeFromString function.

protected $attachment;

public function createCustomAttachment($body, $mimeType, $disposition, $encoding, $filename){
    $attachment = new Part($body);
    $attachment->setType($mimeType);
    $attachment->setDisposition($disposition);
    $attachment->setEncoding($encoding);
    $attachment->setFileName($filename);
    $this->attachment = $attachment;
    return $this;
}

Called the global variable $this->attachment in the function createHtmlMimeFromString. If the variable has value then we are adding the attachment data to the addPart function.

The code be like this

private function createHtmlMimeFromString($htmlBody)
{
    $htmlPart = new Part($htmlBody);
    $htmlPart->setCharset($this->zendMessage->getEncoding());
    $htmlPart->setType(Mime::TYPE_HTML);
    $mimeMessage = new \Zend\Mime\Message();
    $mimeMessage->addPart($htmlPart);
    if ($this->attachment) {
        $mimeMessage->addPart($this->attachment);
    }

    return $mimeMessage;
}

We need to copy the Magento\Framework\Mail\Message entire content in the extended file because the zendMessage is private and this is called in almost all functions.

We can call the createCustomAttachment function from the transport builder to pass the attachment details.

public function addAttachment($body,
                              $mimeType = Mime::TYPE_OCTETSTREAM,
                              $disposition = Mime::DISPOSITION_ATTACHMENT,
                              $encoding = Mime::ENCODING_BASE64,
                              $filename = null)
{
    //$this->message->createAttachment($body, $mimeType, $disposition, $encoding, $filename);
    $this->message->createCustomAttachment($body, $mimeType, $disposition, $encoding, $filename);
    return $this;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.