8

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

Additional

  • Add email in bcc

  • Send attachment along email

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

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('someemail@email.com')
            ->addTo('toemail@email.com')
            ->setReplyTo('replyto@email.com')
            ->addBcc('bcc@email.com')
            ->getTransport();
        $transport->sendMessage();
  }

}
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

0

it's working for me

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

$senderName = 'test';
$senderEmail = 'sender@test.com';
$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?

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.