5

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();
14

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()}} @-->
| improve this answer | |
  • It's working fine thanks.I have another doubt in case if it dynamic means how can we pass to email.html file – Jeeva Chezhiyan Jun 28 '17 at 9:47
  • See my updated answer. – Suresh Chikani Jun 28 '17 at 9:53
  • 1
    @SureshChikani i would like to add in the controller how to do? – Nagaraju K Jul 10 '18 at 7:02
1

please make ensure you added the below line in your email template file.

<!--@subject Subject Of your email  @-->

More reference follow the link

https://stackoverflow.com/questions/35123779/how-to-add-subject-in-tranctional-email-template-for-magento-2-version

| improve this answer | |
0

Use the method clearSubject(), but to apply when you are to call getMessage() method, let this example over here

$transport = $this->transportBuilder->setTemplateIdentifier('send_email_email_template')
                    ->setTemplateOptions( [
                                                'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
                                                'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID,
                                          ])
                    ->setTemplateVars(array())
                    ->setFrom($sender)
                    ->addTo($toEmail, '')
                    ->getTransport();
$transport->getMessage()->setMessageType( \Magento\Framework\Mail\MessageInterface::TYPE_HTML)->setBody($MyHtml)->clearSubject()->setSubject("MySubject");
$transport->sendMessage();
| improve this answer | |

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.