Sending Email with Attachment in Magento 2.3.x

Some of the functionalities in Magento 2.3.x is different from previous page. The reason is that in most cases the 2.3.x framework uses Zend Framework 2 (ZF2) and almost totally refuses to applyZend Framework 1 (ZF1). So, due to this, the previous method Magento\Framework\Mail\MailMessageInterface::createAttachment() is deprecated and there is no direct method to do so. In this blog, we will see how to add attachment to emails.

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();


    $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();


    $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();


    $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();

    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

Magento 2 Console Command Sortcuts

While working on Magento 2.x, you often need to run several console commands which are actually very handy in terms of managing the things such as cache, upgrades, deploy modes, indexers etc. You can check the entire list of commands by running this command:

php bin/magento list

Well, typing the full long commands are not very pleasant if it has to be done multiple times. So, we can use shortcuts instead full commands. This feature comes due to the Symphony framework which is used by each console class that implement command line functionality. The use Symfony\Component\Console\Command component to implement command line features. You can learn more in the documentation of Symphony framework. You can use shortest unambiguous name intead of full name, e.g. s instead of setup.

Now, let’s see shortcuts for some of the commonly used Magento 2 console commands:

1. Cache Commands

php bin/magento cache:clean ==> php bin/magento c:c

php bin/magento cache:disable ==> php bin/magento c:d

php bin/magento cache:enable [cache_type] ==> php bin/magento c:e [cache_type]

php bin/magento cache:flush [cache_type] ==> php bin/magento c:f [cache_type]

php bin/magento cache:status ==> php bin/magento c:s

Where cache_type is the parameter in which you can pass one or more cache types separated by whitespaces, e.g.

php bin/magento c:c config layout

2. Indexer Commands

php bin/magento indexer:reindex ==> php bin/magento i:rei

php bin/magento indexer:info ==> php bin/magento i:i

php bin/magento indexer:status ==> php bin/magento i:sta

php bin/magento indexer:show-mode ==> php bin/magento i:sho

php bin/magento indexer:reset ==> php bin/magento i:res

You may pass parameters for particular indexer type as well.

3. Compile Command

php bin/magento setup:di:compile ==> php bin/magento s:di:c

4. Setup Upgrade Command

php bin/magento setup:upgrade ==> php bin/magento s:up

5. Static Content Deploy Command

php bin/magento setup:static-content:deploy ==> php bin/magento s:s:d

6. Module Management Commands

php bin/magento module:status ==> php bin/magento mo:s

php bin/magento module:disable Namespace_Module ==> php bin/magento mo:d Namespace_Module

php bin/magento module:enable Namespace_Module ==> php bin/magento mo:e Namespace_Module

php bin/magento module:uninstall Namespace_Module ==> php bin/magento m:u Namespace_Module