1

This question already has an answer here:

I have code like below in Magento1 to send Transaction email to customer

public function createPostAction()
{
  $user = Mage::getModel('customer/customer')->setId(46210);
  $user->save();
  $this->sendNewSubUserAccountEmail($user);
}

 private function sendNewSubUserAccountEmail($customer, $backUrl = '')
{
    $translate = Mage::getSingleton('core/translate');
    $translate->setTranslateInline(false);

    $storeId = ($customer->getStoreId() == '0')?$customer->getSendemailStoreId():$customer->getStoreId();
    if ($customer->getWebsiteId() != '0' && $storeId == '0') {
        $storeIds = Mage::app()->getWebsite($customer->getWebsiteId())->getStoreIds();
        reset($storeIds);
        $storeId = current($storeIds);
    }

    $emailTemplate = Mage::getStoreConfig('customer/create_account/sub_user_email', $storeId);

    if (strcasecmp($emailTemplate, 'customer_create_account_sub_user_email') == 0) {
        $emailTemplate = Mage::getStoreConfig('customer/create_account/email_template', $storeId);
    }

    Mage::getModel('core/email_template')
        ->setDesignConfig(array('area'=>'frontend', 'store'=>$storeId))
        ->sendTransactional(
            $emailTemplate,
            Mage::getStoreConfig('customer/create_account/email_identity', $storeId),
            $customer->getEmail(),
            $customer->getName(),
            array('customer' => $customer, 'back_url' => $backUrl));

    $translate->setTranslateInline(true);

    return $customer;
}

How can the same code be implemented in Magento2?

marked as duplicate by Prince Patel, Jai, Amit Bera Aug 9 '18 at 7:12

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • please share the answered link of previous one – jafar pinjar Aug 9 '18 at 8:41
2

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?

  • Please update full code – jafar pinjar Aug 9 '18 at 9:44

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