Vishal
Vishal

Reputation: 900

How to add subject in tranctional email template for magento 2 version

I create below but not idea how to add custom subject in this code

$transport = $this->transportBuilder->setTemplateIdentifier($emailTemplateId)
                      ->setTemplateOptions(['area' => \Magento\Framework\App\Area::AREA_FRONTEND, 'store' => $storeId])
                        ->setTemplateVars($templateParams)
                       ->setFrom(['name' => $senderEmailName, 'email' => $senderEmail])
                        ->addTo($email)
                       ->getTransport();
                $transport->sendMessage();   

For Magento 1 we use add line:-

->setTemplateSubject($mailSubject)

Upvotes: 0

Views: 6060

Answers (2)

hakre
hakre

Reputation: 198209

As it has already been answered, you do set it within the template via

<!--@subject [...] @-->

You can read it out then within the template by accessing the following template variable:

{{var this.getSubject()}}

If you for example use the trans directive to create the subject, this will return the translated subject.

Setting the subject for the message then is transparently done when the TransportBuilder prepares the message (\Magento\Framework\Mail\Template\TransportBuilder::prepareMessage).

So technically, as long as you make use of the email-templates (which is recommended, see email_templates.xml files for reference) this is how the subject is handled.

In your code this happens already within the call to

->getTransport();

If the subject is empty, set it in the template first.


References:

Upvotes: 1

Jaanek Liiskmaa
Jaanek Liiskmaa

Reputation: 101

You can set the subject in the email template header

<!--@subject This is subject @-->

Upvotes: 0

Related Questions