# Issue with EMAIL\_ON\_SEND event and Leads informations in a Plugin

**URL:** <https://forum.mautic.org/t/issue-with-email-on-send-event-and-leads-informations-in-a-plugin/14066>\
**Category:** Product Support\
**Created:** [April 27, 2020, 6:42am UTC](https://forum.mautic.org/t/issue-with-email-on-send-event-and-leads-informations-in-a-plugin/14066 "2020-04-27T06:42:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![elbzh22](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/elbzh22/32/1197_2.png) [@elbzh22](https://forum.mautic.org/u/elbzh22)\
**Post date:** [April 27, 2020, 6:42am UTC](https://forum.mautic.org/t/issue-with-email-on-send-event-and-leads-informations-in-a-plugin/14066/1 "2020-04-27T06:42:00Z")

</div>

**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)_.

```auto
<?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` :

```auto
Hi A, your Reference is A

```

```auto
Hi B, your Reference is B

```

```auto
Hi C, your Reference is C

```

But instead of above, every lead receives an email with the `reference` of the first lead processed :

```auto
Hi A, your Reference is A

```

```auto
Hi B, your Reference is A

```

```auto
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)_ :

```auto
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 !

---

<div class="post-metadata">

**Author:** ![elbzh22](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/elbzh22/32/1197_2.png) [@elbzh22](https://forum.mautic.org/u/elbzh22)\
**Post date:** [April 29, 2020, 6:13am UTC](https://forum.mautic.org/t/issue-with-email-on-send-event-and-leads-informations-in-a-plugin/14066/2 "2020-04-29T06:13:17Z")

</div>

Any idea, anyone ? I’m turning crazy with this problem, save a simple man, help me ;D

---

<div class="post-metadata">

**Author:** ![elbzh22](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/elbzh22/32/1197_2.png) [@elbzh22](https://forum.mautic.org/u/elbzh22)\
**Post date:** [May 11, 2020, 7:07am UTC](https://forum.mautic.org/t/issue-with-email-on-send-event-and-leads-informations-in-a-plugin/14066/3 "2020-05-11T07:07:47Z")

</div>

Hello, sorry for posting again, but still looking for help… ☹

---

<div class="post-metadata">

**Author:** ![elbzh22](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/elbzh22/32/1197_2.png) [@elbzh22](https://forum.mautic.org/u/elbzh22)\
**Post date:** [June 5, 2020, 12:48pm UTC](https://forum.mautic.org/t/issue-with-email-on-send-event-and-leads-informations-in-a-plugin/14066/4 "2020-06-05T12:48:30Z")

</div>

Hello,

Nobody have an idea ? Thanks.
