8

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

5 Answers 5

22

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()}} @-->
4
  • 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, 2017 at 9:47
  • See my updated answer. Jun 28, 2017 at 9:53
  • 1
    @SureshChikani i would like to add in the controller how to do? Jul 10, 2018 at 7:02
  • Thank you for good answer. it working till today, i hope this solution work in future @SureshChikani May 29, 2023 at 12:30
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
1

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}} @-->

1
  • its working for me <!--@subject {{trans "Kaffee Widerrufsbelehrung"}} @-->
    – matinict
    Oct 26, 2022 at 6:13
1

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();
1
  • i got error on m2 as Error: Call to undefined method Vianetz\AdvancedInvoiceLayout\Model\Mail\Template\MultiPartTransportBuilder\Interceptor::clearSubject() in
    – matinict
    Oct 26, 2022 at 6:04
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.