cancel
Showing results for 
Search instead for 
Did you mean: 

How to use Mandrill templates for transactional emails in Magento 2?

How to use Mandrill templates for transactional emails in Magento 2?

I am working in Magento 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="BronzeByte\MandrillEmail\Model\EmailSender">
        <arguments>
            <argument name="transportBuilder" xsi:type="object">Magento\Framework\Mail\Template\TransportBuilder</argument>
            <argument name="mandrillTemplateMapping" xsi:type="object">BronzeByte\MandrillEmail\Model\MandrillTemplateMapping</argument>
            <argument name="logger" xsi:type="object">Psr\Log\LoggerInterface</argument>
        </arguments>
    </type>

</config>

Email Sender Class

 

<?php

namespace BronzeByte\MandrillEmail\Model;

use Mandrill;
use Psr\Log\LoggerInterface;
use Magento\Framework\Mail\Template\TransportBuilder;
use BronzeByte\MandrillEmail\Model\MandrillTemplateMapping;

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