Your software
My Mautic version is: 2.15.1
My PHP version is: 7.1.27
I made a plugin to manage some specifics custom tokens.
It works well when I send a test email, but when I send an email to a segment, I have a problem.
Here is a simplified version of my EventListener.
In this example, I want to replace {testref}
token by a specific customer field named reference
.
I know I can do this with {customerfield=reference}
, but it’s only for example (in real plugin, I use some lead fields to generate specifics links from custom tokens - and no I can’t do what in the email editor).
<?php
namespace MauticPlugin\MyBundle\EventListener;
use Mautic\CoreBundle\EventListener\CommonSubscriber;
use Mautic\EmailBundle\EmailEvents;
use Mautic\EmailBundle\Event\EmailBuilderEvent;
use Mautic\EmailBundle\Event\EmailSendEvent;
/**
* Class EmailSubscriber
*/
class EmailSubscriber extends CommonSubscriber
{
/**
* @return array
*/
static public function getSubscribedEvents()
{
return array(
EmailEvents::EMAIL_ON_SEND => array('onEmailSend', 0),
);
}
/**
* @param EmailSendEvent $event
*/
public function onEmailSend(EmailSendEvent $event)
{
$lead = $event->getLead();
$lead_reference = ($lead !== null) ? $lead['reference'] : 'unknown_reference';
$debug = 'Lead Reference : '.$lead_reference;
file_put_contents('mylog.log', $debug . PHP_EOL, FILE_APPEND);
$contentText = $event->getPlainText();
$content = $event->getContent();
$content = str_replace('{testref}', $lead_reference, $content);
$event->setContent($content);
$event->setPlainText($contentText);
}
}
The problem is : let’s say I want to send an email to a segment with 3 leads.
I want every lead to receives an email with its own reference
:
Hi A, your Reference is A
Hi B, your Reference is B
Hi C, your Reference is C
But instead of above, every lead receives an email with the reference
of the first lead processed :
Hi A, your Reference is A
Hi B, your Reference is A
Hi C, your Reference is A
When I output this reference
in a log, I can see that the loop is okay (2 times, I don’t know why, and right now I dont care ahah) :
Lead Reference : A
Lead Reference : A
Lead Reference : B
Lead Reference : B
Lead Reference : C
Lead Reference : C
So I know the code get the correct datas… Yet I don’t get why everybody see the data of the first processed lead only.
PS : when I say everybody, I mean all my test emails in a test segment… Not in production ofc.
Any help is welcomed, thanks !