Mautic Community Forums

Attachment is added multiple times to email using EMAIL_ON_SEND event (/hook)

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

When I do an error_log 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) :arrow_down_small: :arrow_down_small:.

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.