Tips & Tutorials

How to send email in Magento 2 programmatically

Email is a common communication method between vendors and customers. Email marketing helps you connect with your customers with the aims of brand promotion and sales increasing.


Aside from the default email templates in Magento 2, many extensions require their own emails with various custom templates, parameters, and conditions.


In this blog post, we will learn about sending email in Magento 2, with code snippets taken from our Magento 2 Abandoned Cart Email extension, in which sending email is the main function to remind customers of their pending carts.


1. Declare custom email template


To declare a custom email template that goes with your extension, create the file /etc/email_templates.xml:



Here we have declared a template with id abandonedcart_item1, defined by the file abandonedcart_item1.html, belonged to AbandonedCart module and frontend area. This will create an entry named “Abandoned Cart Item 1” belonged to group “Abandoned Cart” (name of host extension) when admins open Marketing -> Email templates. To declare more templates, you can create more template items inside config tags.


2. Define the email template


As our custom template has frontend area and abandonedcart_item1.html file name, the path to this template is /view/frontend/email/abandonedcart_item1.html.



The email template mostly follows html standard with styling. Magento-specific parameters are placed inside double curly braces {{ }} :


– trans for texts that support translation (if provided by the extension’s i18n). You can use default variables i.e. $store.getFrontendName() to get the store’s name on the email.


– var for variables supplied by the email controller.


– template to automatically inject other templates (i.e. header/footer) based on config_path


Note: Some email clients support only CSS style as inline on the style attribute of HTML tags.


3. Create a transport builder


To customize the email function of Magento we will need to create a transport builder class, extending the default Magento Transport Builder. In our case, we will add the function to send pdf and images attachments to the email, using our transport builder located at Magenest/AbandonedCart/Model/Mail/TransportBuilder.php



Here we have created not one, but two functions for making attachments. This is due to changes in Magento 2.3’s transport builder class compared to Magento 2.1 and 2.2: from 2.3.0 onward, Magento 2 employs Zend Framework 2 for sending emails, with significant changes to email attachments compared to Zend Framework 1.


In our extension, we determine if Zend Framework 1 or 2 should be used for making attachments based on $transport variable passed to createAttachment() function.


4. Send the email


To send emails from a class, its constructor needs to call a transportbuilder class. You can use the default one Magento\Framework\Mail\Template\TransportBuilder or a custom one (Magenest\AbandonedCart\Model\Mail\TransportBuilder in our case).


Below is a snippet of sendMail() function which covers all steps to create an email object:



Finally, our email will look as follows


The final email we got


With the four steps above, we have covered the basics of sending emails programmatically in Magento 2. Please leave a comment below if you have any questions. Thanks for reading!


How to send email in Magento 2 programmatically
4.6 (92%) 5 votes

A  passionate copywriter.

Marketing Executive | Content Manager

Author: Rosie Phan

Rosie Phan

A  passionate copywriter. Marketing Executive | Content Manager

Related Posts

Leave a comment

Your email address will not be published. Required fields are marked *

2 × 2 =

At Magenest, we help our customers thrive by offering advanced solutions and working with excellent business partners.

We’re proud to be one of the top Magento 2 developers with capability in building integration solutions with third party business systems and applications.


Please follow & like us