Sending Email with Attachment in Magento 2.3.x

The Magento 2.3.x framework (actually as all previous versions) does not provide an opportunity for Sending Emails with Attachment to email messages. However, the implementation of this functionality for Magento 2.3.x is different from that of previous versions.  That’s the reason the Magento 2.3.x framework uses Zend Framework 2 (ZF2) and almost totally refuses to apply ZF1. 

Reference Link having various purposes for Various Magento Extension. Link Here : https://xmagestore.com/magento2.html  

In This Article, we are going to describe how to implement the email with attachment in Magento 2.3.X with using the Coding technique.

To understand this process, it’s recommened to get an Idea of some of the ZF2 components such as:

As createAttachment() method is no more available, we will override Magento\Framework\Mail\Message and create a class which will implement Magento\Framework\Mail\MailMessageInterface.

In this method, we will redifine/create some methods which would be responsible for different MIME parts which would be merged later before being sent. Let’s see some of the important ones:

Message::setBodyText() Method

public function setBodyText($content)
{
$textPart = $this->partFactory->create();

$textPart->setContent($content)
->setType(Mime::TYPE_TEXT)
->setCharset($this->zendMessage->getEncoding());

$this->parts[] = $textPart;

return $this;
}

In this method, we are adding the text MIME part to the email and storing in $this->parts array.

Message::setBodyHtml() Method

public function setBodyHtml($content)
{
$htmlPart = $this->partFactory->create();

$htmlPart->setContent($content)
->setType(Mime::TYPE_HTML)
->setCharset($this->zendMessage->getEncoding());

$this->parts[] = $htmlPart;

return $this;
}

In this method, we are doing the same as above but we are setting the HTML part and storing in the same $this->parts array.

Message::setBodyAttachment() Method

public function setBodyAttachment($content, $fileName, $fileType)
{
$attachmentPart = $this->partFactory->create();

$attachmentPart->setContent($content)
->setType($fileType)
->setFileName($fileName)
->setDisposition(Mime::DISPOSITION_ATTACHMENT)
->setEncoding(Mime::ENCODING_BASE64);

$this->parts[] = $attachmentPart;

return $this;
}

Here we set the content of the file, then set a file type and specify its name. In addition, we set disposition. It’s necessary for the file to be treated as an attachment.

Above we've described all methods that help us to add mime parts to the message. But we also added to this class one more important method - Message::setPartsToBody().

Message::setPartsToBody() Method

public function setPartsToBody()
{
$mimeMessage = $this->mimeMessageFactory->create();
$mimeMessage->setParts($this->parts);
$this->zendMessage->setBody($mimeMessage);

return $this;
}

This method is used to put all the pieces together.

Now, we will create a new class TransportBuilder which will extend the actual core class Magento\Framework\Mail\Template\TransportBuilder and instead of the core class, we will be using this new class.

TransportBuilder::addAttachment() Method

public function addAttachment($content, $fileName, $fileType)
{
$this->message->setBodyAttachment($content, $fileName, $fileType);

return $this;
}

Now, you can call this method while sending mail as used in this file.

Further, you can find the entire module here. GitHub