Site icon Qaisar Satti's Blogs

Magento 2 Send Email Programmatically

Magento 2 Send Email Programmatically

Magento 2 Send Email Programmatically

This tutorial is about Magento 2 Send email programmatically,functionality of Magento 2.We will discuss how we can send email programmatically in Magento 2.People often ask whether Magento 2 have any built-in integration with third-party transaction email delivery systems like Amazon SES, Mandrill, etc., or is the built-in email functionality still based around PHP’s mail function? Or the emails sent with some third party software? Also, people ask are there any third party extensions like SMTP Pro for Magento 2 that would let a developer replace the standard email system?

Here we will discuss how it actually is done. After thorough research and digging of Magento 2 codebase for strings such as ’email’, ‘message’, etc until I found something that sends out an email. I stumbled upon sendPaymentFailedEmail() in

vendor/magento/module-checkout/Helper/Data.php

This sets a lot of variables but eventually ties them to a transport object, which is created through a ‘transportBuilder’. This transportBuilder is an instance of

\Magento\Framework\Mail\Template\TransportBuilder

In that file a

$transport

variable exists, which is an instance of

\Magento\Framework\Mail\TransportInterface

Because there is an interface,there is also a regular class called

\Magento\Framework\Mail\Transport

. When we open the file

vendor/magento/framework/Mail/Transport.php

we see that this extends

Zend_Mail_Transport_Sendmail

.

It actually look like this

class Transport extends \Zend_Mail_Transport_Sendmail implements \Magento\Framework\Mail\TransportInterface

So using DI, you’ll be able to replace this transport with another email framework instead of Zend_Mail, such as Mandrill or Amazon SES.

Just be sure to include the send() method since that is the method called in sendMessage().

public function sendMessage()
{
    try {
        parent::send($this->_message);
    } catch (\Exception $e) {
        throw new \Magento\Framework\Exception\MailException(new \Magento\Framework\Phrase($e->getMessage()), $e);
    }
}

Furthermore, there are assumptions about PHPs mail() being outdated and not supporting any 3rd party services.But it is not correct, as every good 3rd party service supports an SMTP interface, and so does mail() too.Also, Amazon SES does support SMTP.

Similarly, there is another simple and easy method to send the email. Magento 2 TransportBuilder uses email templates to compose email’s body. You can send plain text simply using – Zend1 mail

$email = new \Zend_Mail();
        $email->setSubject("Feedback email");
        $email->setBodyText($body);
        $email->setFrom($from, $nameFrom);
        $email->addTo($to, $nameTo);
        $email->send();

That’s it from this tutorial. I hope it serves the purpose. Since these are learning tutorials, please feel free to drop any suggestions or queries in the comments section. That will be highly appreciated surely.

Exit mobile version