7

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

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()}} @-->
3
  • It's working fine thanks.I have another doubt in case if it dynamic means how can we pass to email.html file Jun 28 '17 at 9:47
  • See my updated answer. 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

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

You have to add the file vendor\magento\module-contact\view\frontend\email\submitted_form.html to your Theme at THEME\Magento_Contact\email\submitted_form.html

then change the title like this:

Before

<!--
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<!--@subject {{trans "Contact Form"}} @-->
<!--@vars {
"var data.comment":"Comment",
"var data.email":"Sender Email",
"var data.name":"Sender Name",
"var data.telephone":"Sender Telephone"
} @-->

{{template config_path="design/email/header_template"}}

<table class="message-details">
    <tr>
        <td><strong>{{trans "Name"}}</strong></td>
        <td>{{var data.name}}</td>
    </tr>
    <tr>
        <td><strong>{{trans "Email"}}</strong></td>
        <td>{{var data.email}}</td>
    </tr>
    <tr>
        <td><strong>{{trans "Phone"}}</strong></td>
        <td>{{var data.telephone}}</td>
    </tr>
</table>
<p><strong>{{trans "Message"}}</strong></p>
<p>{{var data.comment}}</p>

{{template config_path="design/email/footer_template"}}

After

Example: Changes subject to "Welcome at %name_of_your_shop%"

<!--
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<!--@subject {{trans "Welcome to %store_name" store_name=$store.getFrontendName()}} @-->
<!--@vars {
"var data.comment":"Comment",
"var data.email":"Sender Email",
"var data.name":"Sender Name",
"var data.telephone":"Sender Telephone"
} @-->

{{template config_path="design/email/header_template"}}

<table class="message-details">
    <tr>
        <td><strong>{{trans "Name"}}</strong></td>
        <td>{{var data.name}}</td>
    </tr>
    <tr>
        <td><strong>{{trans "Email"}}</strong></td>
        <td>{{var data.email}}</td>
    </tr>
    <tr>
        <td><strong>{{trans "Phone"}}</strong></td>
        <td>{{var data.telephone}}</td>
    </tr>
</table>
<p><strong>{{trans "Message"}}</strong></p>
<p>{{var data.comment}}</p>

{{template config_path="design/email/footer_template"}}

What can you use?

There is a method which contains all data which you can use, search the method addEmailVariables() in the file vendor\magento\module-email\Model\AbstractTemplate.php

For example the store object is set here like this:

protected function addEmailVariables($variables, $storeId)
{
    $store = $this->storeManager->getStore($storeId);
    if (!isset($variables['store'])) {
        $variables['store'] = $store;
    }
    ...

If this is not enough, then you have to extend this method from your module.

0

I tried and It was working

For multiple replacements, try this

<!--@subject {{trans "%candidate_name applied to %job_name" candidate_name=$candidate.full_name job_name=$job_name}} @-->

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.