0

I am working on a module in which I need to attach an attachment with sales email. However, Magento 2.3 use zendframework2 so email attachment is not the same as zendframework1 (in Magento 2.2).

Can anyone help me to achieve the same task? How to implement attachment functions in Magento 2.3

I have searched in google and read several questions but I didn't find the solution.

1

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;
}
0

I had the same issue.

To solve this i created a helper.

use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\Mail\MessageInterface;
use Magento\Framework\Mail\Template\FactoryInterface;
use Magento\Framework\Mail\Template\SenderResolverInterface;
use Magento\Framework\Mail\TransportInterfaceFactory;   
use Magento\Framework\Mail\Template\TransportBuilder;

class UploadTransportBuilder extends TransportBuilder {

public function __construct(FactoryInterface $templateFactory,
    MessageInterface $message,
    SenderResolverInterface $senderResolver,
    ObjectManagerInterface $objectManager,
    TransportInterfaceFactory $mailTransportFactory) {
    parent::__construct($templateFactory,
        $message,
        $senderResolver,
        $objectManager,
        $mailTransportFactory);
}

public function attachFile(
    $body,
    $filename    = null,
    $mimeType    = \Zend_Mime::TYPE_OCTETSTREAM,
    $disposition = \Zend_Mime::DISPOSITION_ATTACHMENT,
    $encoding    = \Zend_Mime::ENCODING_BASE64
)
{
    $this->message->createAttachment($body, $mimeType, $disposition, $encoding, $filename);
    return $this;
}

}

Then I loaded this on my controller.

use BVZ\Wishlist\Helper\UploadTransportBuilder;

UploadTransportBuilder $transportBuilder
$this->_transportBuilder = $transportBuilder

$transport = $this->_transportBuilder->setTemplateIdentifier('bvz_customer_wishlist')
            ->setTemplateOptions(['area' => 'frontend', 'store' => $store])
            ->setTemplateVars(['data' => $postObject])
            ->setFrom('support')
            ->addTo($formEmail, $customerName)
            ->attachFile($pdf,'Seleção de Produtos.pdf')
            ->getTransport();

$this->messageManager->addSuccess(__('Um e-mail foi enviado com sua lista de desejo.'));

            $transport->sendMessage();

The code method for attach a file is attachFile();

  • Thank you @Gabriel Fernandes I will try this. Is it working in magento 2.3 ? – Sameer Bhayani Jan 8 at 13:39
  • i'm using 2.2.3, maybe works on 2.3 – Gabriel Fernandes Jan 8 at 13:42
  • Thanks @Gabriel. Magento 2.3 using zendframework2 & zendframework had deleted createAttachment() function so it is not working in 2.3 however older magento 2 version use zendframework1 so It is working in those versions error: prnt.sc/m4gr1e – Sameer Bhayani Jan 8 at 13:54
0

Send Email with Attachment in Magento 2.3

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

0

This solution is working so charming

 $transport = $_transportBuilder->setTemplateIdentifier(20)
->setTemplateOptions($templateOptions)
->setTemplateVars($templateVars)
->setFrom($from)
->addTo($vendor_email)
->getTransport();

 $html= $transport->getMessage()->getBody()->generateMessage();            
$bodyMessage = new \Zend\Mime\Part($html);
$bodyMessage->type = 'text/html';
$attachment=$_transportBuilder->addAttachment($pdfData,$fileName);      
$bodyPart = new \Zend\Mime\Message();
$bodyPart->setParts(array($bodyMessage,$attachment));
$transport->getMessage()->setBody($bodyPart);                
$transport->sendMessage();
$inlineTranslation->resume();

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.