10

As done in Magento 1.x.x, how can custom transactional emails be sent.

Additional

  • Add email in bcc

  • Send attachment along email

2
  • I would suggest to take a look at Contact Module.
    – MagePsycho
    Commented Oct 28, 2015 at 12:34
  • Yes I studied that and answered it below, for sharing purposes
    – huzefam
    Commented Oct 28, 2015 at 12:56

3 Answers 3

10

okie bit of debugging helps,

Sending a transactional email in Magento2

class Dummy
{

   /**
     * @var  \Magento\Framework\Mail\Template\TransportBuilder
     */
    private $_transportBuilder;

  public function __construct( \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder){

    $this->_transportBuilder = $transportBuilder;
  }

  public function sendEmail($templateId =1, $storeId =1,$templateParams)
  {

     $transport = $this->_transportBuilder->setTemplateIdentifier($templateId)
            ->setTemplateOptions(['area' => Magento\Framework\App\Area::AREA_FRONTEND, 'store' => $storeId])
            ->setTemplateVars($templateParams)
            ->setFrom('[email protected]')
            ->addTo('[email protected]')
            ->setReplyTo('[email protected]')
            ->addBcc('[email protected]')
            ->getTransport();
        $transport->sendMessage();
  }

}
8
  • how to add subject in email template
    – Vishal
    Commented Jan 27, 2016 at 12:31
  • @MagikVishal Your answer is here magento.stackexchange.com/questions/124446/… <!--@subject Promopoup@-->
    – Jackson
    Commented Oct 28, 2016 at 9:23
  • Doesn't work in Magento 2.1.2 anymore Commented Dec 16, 2016 at 18:54
  • @FredOroskoDias sure..will check and add ablock for 2.1.x as well..thanks
    – huzefam
    Commented Oct 15, 2017 at 8:56
  • How do we add multiple BCC ? Commented Nov 20, 2017 at 7:53
1

it's working for me

$emailTemplateVariables = array();
$emailTempVariables['myvar'] = $myvar;

$senderName = 'test';
$senderEmail = '[email protected]';
$postObject = new \Magento\Framework\DataObject();
$postObject->setData($emailTempVariables);
$sender = [
        'name' => $senderName,
        'email' => $senderEmail,
        ];

$transport = $this->_transportBuilder->setTemplateIdentifier('myemail_email_template')
->setTemplateOptions(['area' => \Magento\Framework\App\Area::AREA_FRONTEND, 'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID])
->setTemplateVars(['data' => $postObject])
->setFrom($sender)
->addTo($email)
->setReplyTo($senderEmail)            
->getTransport();               
$transport->sendMessage();

more info

how to send custom email no template in magento2?

0
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 and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.