20

Does Magento 2 have any built-in integration with third party transaction email deliverability systems like Amazon SES, Mandrill, etc., or is the built-in email functionality still based around PHP's mail function? Or are the emails sent with some third thing?

If the answer is mail, are there any third party extensions like SMTPPro for Magento 2 that would let a developer replace the standard email system?

12

I started searching around in the 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;

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

This is what you are looking for. 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);
    }
}
8

there is an often made assumption about PHPs mail() beeing outdated and not supporting any 3th. party services.
That is actually wrong, as every good 3th. party service supports an SMTP interface, and so does mail(), too.

Amazon SES does support SMTP.

and sending via SMTP with mail() is described in this answer: https://stackoverflow.com/a/14457410/716029

  • 1
    +1 for useful information -- but I'm still interested in what, if any, abstractions Magento has for supporting the various email via HTTP services. – Alan Storm Oct 22 '15 at 5:12
  • No specific implementations besides Zend_Mail, unfortunately. – Peter Jaap Blaakmeer Jan 25 '16 at 21:18
8

Magento 2 TransportBuilder uses a email templates to compose email's body. The simplest way I found to send plain text - use Zend1 mail (that is used by Magento 2 itself):

        $email = new \Zend_Mail();
        $email->setSubject("Feedback email");
        $email->setBodyText($body);
        $email->setFrom($from, $nameFrom);
        $email->addTo($to, $nameTo);
        $email->send();
  • 3
    If you want to send the body in the form of html, you can use $email->setBodyHtml($body) – Anish Karim Jul 31 '17 at 6:44
  • this is great, however, in case you are using smtp via something like google then follow this thread stackoverflow.com/questions/3455674/… – Ahdee Sep 24 '17 at 14:53
0

Magento 2 Uses \Magento\Framework\Mail\Template\TransportBuilder class for send custom email.

You need to create a simple module and set email_tempaltes.xml file for defining your transaction mail templates.

You can send mail from Adminhtml or Frontend Scope area.

I have shared a link for step by step guide to send a custom mail from Magento 2 using a module.

Send Custom Mail from Magento 2 Module

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.