1

I made a custom module that send an email to my customers.

app/code/Local/CustomEmail/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Local_CustomEmail',
    __DIR__
);

app/code/Local/CustomEmail/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Local_CustomEmail" setup_version="1.0.0">
    </module>
</config>

app/code/Local/CustomEmail/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="myemail_email_template" label="My Label" file="custom_email.html" type="html" module="Local_CustomEmail" area="frontend"/>
</config>

app/code/Local/CustomEmail/view/frontend/email/custom_email.html

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

{{trans "Text to translate"}}

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

These emails should be sent by an external script not registered in Magento, so I have to use objectManager.

My website have 2 store views: (Italian (ID:1), English (ID:2))

myCustomScript.php

<?php
use Magento\Framework\App\Bootstrap;
require('../app/bootstrap.php');
use Magento\Catalog\Api\ProductRepositoryInterface;
Bootstrap::create(BP, $_SERVER)->getObjectManager();
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$appState = $objectManager->get("Magento\Framework\App\State");
$appState->setAreaCode("frontend");
$transportBuilder = $objectManager->get("Magento\Framework\Mail\Template\TransportBuilder");

// I have the receiver email address and I know his language.
$userMail = 'a@a.com';
$userLang = 1; // 1=Italian; 2=English

$emailTemplateVariables = array();
$emailTemplateVariables['foo'] = 'bar';

$sender = [ 'name' => 'Sender Name', 'email' => 'sender@email.com'];

$postObject = new \Magento\Framework\DataObject();
$postObject->setData($emailTempVariables);

$transport = $transportBuilder->setTemplateIdentifier('myemail_email_template')
            ->setTemplateOptions(['area' => 'fontend', 'store' => $userLang]) // here I tried with 1 and 2 (the IDs of my store views in magento)
            ->setTemplateVars(['data' => $postObject])
            ->setFrom($sender)
            ->addTo($userEmail)           
            ->getTransport();               
$transport->sendMessage();

The email was sent but the receiver receive this email always in english.

The header and footer template are included in all the default transactional emails and the translation work fine, but with this email all are not translated.

How can I solve this issue?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.