I am working in magneto 2 and want to use Mandrill templates for transactional emails. So emails sent through Mandrill use the templates I have set up in my Mandrill account instead of magento templates.
I’ve installed the Magepal SMTP module on my website and successfully configured it to use Mandrill for sending transactional emails. Currently, all transactional emails are being routed through Mandrill’s SMTP. However, I would like to leverage Mandrill’s templates for these transactional emails instead of using Magento’s default email templates.
I have tried to override default email functionality of Magento 2 but my code is not working. Is there a way to map Magento templates with Mandrill templates? Can any please guide me how can I achieve this?
Here is my custom module code.
di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="BronzeByteMandrillEmailModelEmailSender">
<arguments>
<argument name="transportBuilder" xsi:type="object">MagentoFrameworkMailTemplateTransportBuilder</argument>
<argument name="mandrillTemplateMapping" xsi:type="object">BronzeByteMandrillEmailModelMandrillTemplateMapping</argument>
<argument name="logger" xsi:type="object">PsrLogLoggerInterface</argument>
</arguments>
</type>
</config>
Email Sender Class
<?php
namespace BronzeByteMandrillEmailModel;
use Mandrill;
use PsrLogLoggerInterface;
use MagentoFrameworkMailTemplateTransportBuilder;
use BronzeByteMandrillEmailModelMandrillTemplateMapping;
class EmailSender
{
protected $transportBuilder;
protected $mandrillTemplateMapping;
protected $logger;
public function __construct(
TransportBuilder $transportBuilder,
MandrillTemplateMapping $mandrillTemplateMapping,
LoggerInterface $logger
) {
$this->transportBuilder = $transportBuilder;
$this->mandrillTemplateMapping = $mandrillTemplateMapping;
$this->logger = $logger;
}
public function sendEmail($magentoTemplateId, $recipientEmail, $variables)
{
$this->logger->info("Email file template");
$mandrillTemplate = $this->mandrillTemplateMapping->getMandrillTemplate($magentoTemplateId);
if ($mandrillTemplate) {
try {
$mandrill = new Mandrill('api_kay');
$message = [
'to' => [['email' => $recipientEmail]],
'global_merge_vars' => array_map(function ($key, $value) {
return ['name' => $key, 'content' => $value];
}, array_keys($variables), $variables),
];
$result = $mandrill->messages->sendTemplate($mandrillTemplate, [], $message);
$this->logger->info("Email sent using Mandrill template '{$mandrillTemplate}' to {$recipientEmail}.", ['result' => $result]);
} catch (Exception $e) {
$this->logger->error("Failed to send email using Mandrill template '{$mandrillTemplate}' to {$recipientEmail}.", ['exception' => $e->getMessage()]);
}
} else {
try {
$this->transportBuilder->setTemplateIdentifier($magentoTemplateId)
->setTemplateOptions(['area' => 'frontend', 'store' => 1])
->setTemplateVars($variables)
->setFrom('general')
->addTo($recipientEmail)
->getTransport()
->sendMessage();
$this->logger->info("Email sent using Magento template '{$magentoTemplateId}' to {$recipientEmail}.");
} catch (Exception $e) {
$this->logger->error("Failed to send email using Magento template '{$magentoTemplateId}' to {$recipientEmail}.", ['exception' => $e->getMessage()]);
}
}
}
}