# Attachment is added multiple times to email using EMAIL\_ON\_SEND event (/hook)

**URL:** <https://forum.mautic.org/t/attachment-is-added-multiple-times-to-email-using-email-on-send-event-hook/11376>\
**Category:** Product Support\
**Created:** [October 14, 2019, 12:12pm UTC](https://forum.mautic.org/t/attachment-is-added-multiple-times-to-email-using-email-on-send-event-hook/11376 "2019-10-14T12:12:57Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dseegers](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/dseegers/32/888_2.png) [@dseegers](https://forum.mautic.org/u/dseegers)\
**Post date:** [October 14, 2019, 12:12pm UTC](https://forum.mautic.org/t/attachment-is-added-multiple-times-to-email-using-email-on-send-event-hook/11376/1 "2019-10-14T12:12:57Z")

</div>

Hey Everyone,

I am writing a plug-in for Mautic that allows you to add an dynamic attachment to the e-mail before it is shipped to the end-user, based on some value. But I am stuck on the next thing.

On the `EMAIL_ON_SEND` event the attachment is added to the e-mail. This is working fine for example mails ( ![image](https://us1.discourse-cdn.com/flex020/uploads/mautic/original/1X/569161ab530cf02b242092929f68f6a56d9d1f7b.png) ).  
But when I use a segment the attachment will be added multiple times to the email.  
(The first mail get 2 of the same attachment, the second 4, etc etc).

**Versions**

- _PHP 7.1.16_
- _Mautic v2.15.3_

**Below an example of the email**

 ![66741632-3ee11200-ee76-11e9-82c6-e8970b454a1e](https://us1.discourse-cdn.com/flex020/uploads/mautic/original/1X/e24dd3ff892daada8e4d14ec668b3d6e58ae0ae3.png)

When I do an [error\_log](https://www.php.net/manual/en/function.error-log.php) it is showing me that the event is running 2 times for each e-mail. I have written a simple class that should add just one file (loaded from Wikipedia) 🔽 🔽.

Also the `$helper->attachFile('file.png')` seems to add the file to all the emails every time it is called.

```
class EmailSubscriber extends CommonSubscriber
{
    protected $helper;

    public function __construct(IntegrationHelper $helper)
    {
// $this->helper = $helper;
// $this->parser = new ApiParser();

    }

    /**
     * @return array
     */
    public static function getSubscribedEvents()
    {
        return [
            EmailEvents::EMAIL_ON_SEND => ['onEmailSend', 100],
        ];
    }

    /**
     * Search and replace tokens with content
     *
     * @param EmailSendEvent $event
     */

    public function onEmailSend(EmailSendEvent $event)
    {

        $helper = $event->getHelper();

        $helper->attachFile('https://upload.wikimedia.org/wikipedia/commons/thumb/6/68/Voorbeeld_nl.png/600px-Voorbeeld_nl.png');

        $event->setSubject('mail included attachment');

    }

```

I have two questions:

- How can I make sure this event is only triggered once (of a trigger based on sending the email itself).

- How can I make sure only one attachment is added to the email (should I use another **event trigger?** )

Steps that I have taken in the mautic dashboard.

 ![image](https://us1.discourse-cdn.com/flex020/uploads/mautic/original/1X/e8c3b30d63802aede9740fbe22239a86738b7c41.png)

 ![image](https://us1.discourse-cdn.com/flex020/uploads/mautic/original/1X/15377110329df752628eef6f5dc75212c6507d50.png)

 ![image](https://us1.discourse-cdn.com/flex020/uploads/mautic/original/1X/30fc0e66c2b4875058393691b8a2401caf6179ed.png)

---

<div class="post-metadata">

**Author:** ![silavapi](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/silavapi/32/7424_2.png) [@silavapi](https://forum.mautic.org/u/silavapi)\
**Post date:** [January 25, 2021, 8:52pm UTC](https://forum.mautic.org/t/attachment-is-added-multiple-times-to-email-using-email-on-send-event-hook/11376/2 "2021-01-25T20:52:57Z")

</div>


