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')
    'firstname' => 'John',
    'lastname' => 'Doe'

Please help me.



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)
                'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
                'store' => $this->_storeManager->getStore()->getId(),

        return $template;
$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(),])
                    ->setFrom(array('email' => 'noreply@abc.com','name' => 'general'))
                    ->addTo(array('To' => 'To@abc.net','To2' => 'To2@abc.net'))
                    ->addAttachment($filename, $filename);

$transport = $transport->getTransport();

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.