phpmautic

Mautic hook is not called (ON_SENT_EMAIL_TO_USER)


I am writing a plug-on that should add an (dynamic) attachment to the email that is send to the end user. But I am stuck on one thing.

Firstly I was using the EMAIL_ON_SEND hook to add an attachment to the email. But it seems that it will add a attachment to each email everytime it is called.

For each email it is called two times. So to the first mail it will add 2 attachments and for the second one 4, etc etc.

The second approach was to use the ON_SENT_EMAIL_TO_USER hook. But this one does not seems to be called before the email (in a segment) is send.

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],
            EmailEvents::ON_SENT_EMAIL_TO_USER => ['onEmailSend', 100],
        ];
    }

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

    public function onEmailSend(EmailSendEvent $event)
    {
        error_log('123');
    }

Someway I have to hook on the actual action that is sending the email instead of the event (?). But I can't figure out which one


Solution

  • Hey Firstly thank you for the response.

    The hook is called multiple times so I needed to tweak it. So we track where it is called and filter it. Besides that we need to clean the attachments every time.

    Anyway even if it is not that clean, it does the trick

    $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 5);
    
                if (strpos($trace[4]['file'], 'SendEmailToContact.php') !== false) {
                    $helper = $event->getHelper();
    
                    $messageChildren = $helper->message->getChildren();
    
                    if (count($messageChildren) > 0) {
                        $helper->message->detach($messageChildren[0]);
                    }