0

I have been struggling to find way to replace From and Reply To addresses before email is sent out in the Mautic software.

I have created mautic_root/plugins/SenderRandBundle/EventListener/EmailSubscriber.php and its content:

<?php
namespace MauticPlugin\SenderRandBundle\EventListener;

use Doctrine\ORM\EntityManager;
use Mautic\CoreBundle\Helper\EmojiHelper;
use Mautic\CoreBundle\Helper\IpLookupHelper;
use Mautic\CoreBundle\Model\AuditLogModel;
// use Mautic\EmailBundle\EmailEvents;
// use Mautic\EmailBundle\Event as Events;
use Mautic\EmailBundle\Event\TransportWebhookEvent;
use Mautic\EmailBundle\Model\EmailModel;
// use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Translation\TranslatorInterface;

use Mautic\EmailBundle\EmailEvents;
use Mautic\EmailBundle\Event as Events;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Psr\Log\LoggerInterface;
use MauticPlugin\SenderRandBundle\Integration\Config;

/**
 * Class EmailSubscriber
 */
class EmailSubscriber implements EventSubscriberInterface
{
    /**
     * @var AuditLogModel
     */
    private $auditLogModel;

    /**
     * @var IpLookupHelper
     */
    private $ipLookupHelper;

    /**
     * @var EmailModel
     */
    private $emailModel;

    /**
     * @var TranslatorInterface
     */
    private $translator;

    /**
     * @var EntityManager
     */
    private $entityManager;

    private $config;

    public function __construct(
        IpLookupHelper $ipLookupHelper,
        AuditLogModel $auditLogModel,
        EmailModel $emailModel,
        TranslatorInterface $translator,
        EntityManager $entityManager,
        LoggerInterface $logger,
        Config $config
    ) {
        $this->ipLookupHelper = $ipLookupHelper;
        $this->auditLogModel  = $auditLogModel;
        $this->emailModel     = $emailModel;
        $this->translator     = $translator;
        $this->entityManager  = $entityManager;
        $this->logger = $logger;
        $this->config = $config;
    }

    /**
     * @return array
     */
    static public function getSubscribedEvents()
    {
        return array(
            EmailEvents::EMAIL_ON_SEND    => array('onEmailGenerate', 0),
        );
    }

    /**
     * Search and replace with content
     *
     * @param EmailSendEvent $event
     */
    public function onEmailGenerate(Events\EmailSendEvent $event)
    {
        // Check if this plugin is enabled
        if (!$this->config->isPublished()) {
            return;
        }
        $email = $event->getEmail();
        $fromAddress = $email->getFromAddress();
        $replyToAddress = $email->getReplyToAddress();
        if (!$email->getUseOwnerAsMailer()) {
            // Check if email from address contains @sender.rand
            if (strpos($fromAddress, '@sender.rand') !== false) {
                // Get domains from plugin settings
                $domains = explode(',', $this->config->getFeatureSettings()['integration']['domains']);
                if (count($domains) > 0) {
                    // Choose random domain from domains
                    $domain = $domains[array_rand($domains)];
                    // Replace @sender.rand with chosen domain
                    $sender = str_replace('@sender.rand', '@' . $domain, $fromAddress);
                    // Update email from address
                    $email->setFromAddress($sender);
                    // Check if email reply to contains @sender.rand
                    if (strpos($replyToAddress, '@sender.rand') !== false) {
                        // Replace @sender.rand with chosen domain
                        $newReplyToAddress = str_replace('@sender.rand', '@' . $domain, $replyToAddress);
                        // Update email reply to address
                        $email->setReplyToAddress($newReplyToAddress);
                    }
                }
            }
        }
    }
}

Currently with this code, I am getting an email which From address is changed as intended, but Reply To address remains unchanged - this is problem I am trying to solve.

What's the best way to modify From and Reply To addresses before email is sent out?

Typhome
  • 35
  • 7

0 Answers0