3

I wanted to load a particular processed email template by using its id. how to process this in magento 2. in magento 1.x the code is like

 $sTemplate = Mage::getModel('core/email_template')
->load($templateId)
->getProcessedTemplate(array(
    'firstname' => 'John',
    'lastname' => 'Doe'
));

Please help me.

Thanks.!

0

To do the equivalent in Magento 2 you need to use the \Magento\Framework\Mail\Template\TransportBuilder class, as a very simplified example:

class Email
{
    protected $_storeManager;
    protected $_transportBuilder;

    public function __construct(
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder
    )
    {
        $this->_storeManager = $storeManager;
        $this->_transportBuilder= $transportBuilder;
    }

    public function generateTemplate($templateId)
    {
        $emailTemplateVariables = [
            'firstname' => 'John',
            'lastname' => 'Doe',
        ];

        $template = $this->_transportBuilder->setTemplateIdentifier($templateId)
            ->setTemplateOptions([
                'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
                'store' => $this->_storeManager->getStore()->getId(),
            ])->setTemplateVars($emailTemplateVariables);

        return $template;
    }
}
0
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$transportBuilder = $objectManager->get('\Magento\Framework\Mail\Template\TransportBuilder');
$link = "Store Link";
$subject = "Link for Orders on";


$myfile = 'Attachment_file';
$vars = array( 'subject' => $subject,'link' => $link);
$filename = $myfile;

$transport = $this->_transportBuilder->setTemplateIdentifier('12')
                    ->setTemplateOptions(['area' => \Magento\Framework\App\Area::AREA_FRONTEND,'store' => $this->storeManager->getStore()->getId(),])
                    ->setTemplateVars($vars)
                    ->setFrom(array('email' => 'noreply@abc.com','name' => 'general'))
                    ->addTo(array('To' => 'To@abc.net','To2' => 'To2@abc.net'))
                    ->addAttachment($filename, $filename);

$transport = $transport->getTransport();
$transport->sendMessage();

You need to check this from live.

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.