Magento2 – How to Add Subject in Custom Email Template

email-templatesmagento2

I have created custom email template in magento2. Mail function is working fine, but subject is not sent in that email. How can I add the subject in my custom email template.

$transport = $this->transportBuilder
       ->setTemplateIdentifier('my_custom_email_template')
       ->setTemplateOptions(
            [
                'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
                'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID,
            ]
        )
       ->setTemplateVars($vars)
       ->setFrom($sender)
       ->addTo($toEmail)
       ->getTransport();
    $transport->sendMessage();
    $this->inlineTranslation->resume();

Best Answer

You can write email subject to in your email .html file

<!--@subject {{trans "Your Subject Write Here "}}  @-->

You can pass subject dynamic by follow below example

<!--@subject {{trans "Welcome to %store_name" store_name=$store.getFrontendName()}} @-->
Related Topic