1

I visited and tried all related solutions available on stack exchange so decided to add my question too as issue is not resolved.

Function is:

  public function sendEmail()
    {
        $email="[email protected]";
        $name="Ajwad";
        $subject="test";
        $message="msg";
        $templateVars = array(
            'store' => $this->storeManager->getStore(),
            'customer_name' =>$name,
            'email' => $email,
            'subject'   => $subject,
            'message'   => $message
        );
        try {
            $this->inlineTranslation->suspend();
            $error = false;
            $sender = [
                'name' => $this->escaper->escapeHtml($name),
                'email' => $this->escaper->escapeHtml($email),
            ];
        $to = array('[email protected]');
        //$storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
            $transport = $this->transportBuilder
                ->setTemplateIdentifier('email_demo_template')
                ->setTemplateOptions(
                    [
                        'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
                        'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID,
                    ]
                )
                ->setTemplateVars($templateVars)
                ->setFrom($sender)
                ->addTo($to)
                ->getTransport();
            $transport->sendMessage();
            $this->inlineTranslation->resume();
        } catch (\Exception $e) {
            $this->logger->debug($e->getMessage());
        }
    }

In etc/email_templates.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Email:etc/email_templates.xsd">
<template id="email_demo_template" label="This is email demo" file="email_demo_template.html" type="html" module="Vendor_ModuleName" area="frontend"/>
</config>

In view/frontend/email/email_demo_template.html

<!--@subject Customer Inquiry @-->
<!--@vars
{"store url=\"\"":"Store Url",
"skin url=\"images/logo_email.gif\" _area='frontend'":"Email Logo Image"}
@-->
<!--@styles
body,td { color:#2f2f2f; font:11px/1.35em Verdana, Arial, Helvetica, sans-serif; }
@-->
<!--@vars {
"template config_path=\"design\/email\/footer_template\"":"Email Footer Template",
"template config_path=\"design\/email\/header_template\"":"Email Header Template"
} @-->
{Error in template processing}
        <table cellspacing="0" cellpadding="0" border="0" width="100%">
            <tr>
                <td align="center" valign="top" style="padding:20px 0 20px 0">
                    <table bgcolor="#FFFFFF" cellspacing="0" cellpadding="10" border="0" width="650" style="border:1px solid #E0E0E0;">
                        <tr>
                            <td valign="top">
                                <h1 style="font-size:22px;font-weight:normal;line-height:22px;margin:0 0 11px 0;">{{trans "Hello"}},</h1>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <table cellspacing="0" cellpadding="0" border="0" width="650">
                                    <tbody>
                                        <tr>
                                                <td colspan="2" valign="top" style="font-size:12px;padding:7px 9px 9px 9px;border:1px solid #EAEAEA;">
                                                    Name: {{var customer_name}}
                                                </td>
                                        </tr>
                                        <tr>
                                                <td colspan="2" valign="top" style="font-size:12px;padding:7px 9px 9px 9px;border:1px solid #EAEAEA;">
                                                    Subject: {{var subject}}
                                                </td>
                                        </tr>
                                        <tr>
                                            <td colspan="2" valign="top" style="font-size:12px;padding:7px 9px 9px 9px;border:1px solid #EAEAEA;">
                                                Message: {{var message}}
                                            </td>
                                        </tr>
                                    </tbody>
                                </table>
                            </td>
                        </tr>
                        <tr>
                            <td bgcolor="#EAEAEA" align="center" style="background:#EAEAEA;text-align:center;">
                                <center>
                                    <p style="font-size:12px;margin:0;">
                                        <strong>{{trans "Thank you"}}</strong>
                                    </p>
                                </center>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
        {Error in template processing}

It sends email as:

enter image description here

but I want to get Header and footer in email for that I added

{{template config_path="design/email/header_template"}} <!-- header -->
<!-- custom table content--->
{{template config_path="design/email/footer_template"}} <!-- footer -->

As

<!--@subject Customer Inquiry @-->
<!--@vars
{"store url=\"\"":"Store Url",
"skin url=\"images/logo_email.gif\" _area='frontend'":"Email Logo Image"}
@-->
<!--@styles
body,td { color:#2f2f2f; font:11px/1.35em Verdana, Arial, Helvetica, sans-serif; }
@-->
<!--@vars {
"template config_path=\"design\/email\/footer_template\"":"Email Footer Template",
"template config_path=\"design\/email\/header_template\"":"Email Header Template"
} @-->
{{template config_path="design/email/header_template"}}
        <table cellspacing="0" cellpadding="0" border="0" width="100%">
            <tr>
                <td align="center" valign="top" style="padding:20px 0 20px 0">
                    <table bgcolor="#FFFFFF" cellspacing="0" cellpadding="10" border="0" width="650" style="border:1px solid #E0E0E0;">
                        <tr>
                            <td valign="top">
                                <h1 style="font-size:22px;font-weight:normal;line-height:22px;margin:0 0 11px 0;">{{trans "Hello"}},</h1>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <table cellspacing="0" cellpadding="0" border="0" width="650">
                                    <tbody>
                                        <tr>
                                                <td colspan="2" valign="top" style="font-size:12px;padding:7px 9px 9px 9px;border:1px solid #EAEAEA;">
                                                    Name: {{var customer_name}}
                                                </td>
                                        </tr>
                                        <tr>
                                                <td colspan="2" valign="top" style="font-size:12px;padding:7px 9px 9px 9px;border:1px solid #EAEAEA;">
                                                    Subject: {{var subject}}
                                                </td>
                                        </tr>
                                        <tr>
                                            <td colspan="2" valign="top" style="font-size:12px;padding:7px 9px 9px 9px;border:1px solid #EAEAEA;">
                                                Message: {{var message}}
                                            </td>
                                        </tr>
                                    </tbody>
                                </table>
                            </td>
                        </tr>
                        <tr>
                            <td bgcolor="#EAEAEA" align="center" style="background:#EAEAEA;text-align:center;">
                                <center>
                                    <p style="font-size:12px;margin:0;">
                                        <strong>{{trans "Thank you"}}</strong>
                                    </p>
                                </center>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
        {{template config_path="design/email/footer_template"}}

But doing this email is not created (no response). When I set area to ADMINHTML and created template file at view/adminhtml/eamil/email_demo_template.html including header and footer tag email is created but with this error:

enter image description here

Please guide towards solution.

1 Answer 1

0

It seems you are using magento default store id i.e. 0

Please try to change store code like this and try

$transport = $this->transportBuilder
            ->setTemplateIdentifier('email_demo_template')
            ->setTemplateOptions(
                [
                    'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
                    'store' => 1, /* just add 1 for testing if work change dynamically*/ 
                ]
            )

You can override your header and footer template like this

    app/design/frontend/Vendor/themename/Magento_Email/email/header.html
    app/design/frontend/Vendor/themename/Magento_Email/email/footer.html
4
  • with 'store' => 1 email is creating without header and footer tag, but with header and footer tag no response
    – Ajwad Syed
    Commented Sep 16, 2019 at 14:29
  • check header footer template is exists in you theme or parent theme
    – Zahirabbas
    Commented Sep 17, 2019 at 6:52
  • how can I check header and footer ? if I go to Marketing->Email Templates and look at the preview the email looks fine
    – Ajwad Syed
    Commented Sep 22, 2019 at 7:56
  • check it is present in your theme or parent theme if not create or copy file in your theme
    – Zahirabbas
    Commented Sep 23, 2019 at 6:04

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.