• Blog
  • 2 MINUTES READ

Send Multiple Emails via Mautic Core

  • POSTED ON
  • January 15, 2016
  • POSTED BY
  • Muhammad Ahmad
  • POSTED ON January 15, 2016
  • POSTED BY Muhammad Ahmad

vteam #480 inherited a project of an existing web based application for email marketing that was built using X2CRM on Yii2 framework. vteams engineer Zeeshan Ahmad was required to shift this application to Mautic CRM based on Symfony2. Reason being that client wanted a way in this application by which multiple emails could be sent per lead/contact existed in....

vteam #480 inherited a project of an existing web based application for email marketing that was built using X2CRM on Yii2 framework. vteams engineer Zeeshan Ahmad was required to shift this application to Mautic CRM based on Symfony2. Reason being that client wanted a way in this application by which multiple emails could be sent per lead/contact existed in the system.

Challenges:

As Mautic took the core email address as one of the identifiers for a lead, so there was hardly any chance that Zeeshan could use multiple email IDs for a single lead or send an email to all those email IDs attached with that particular single lead.

After digging into Mautic core code further, it was quite difficult to understand code flow and logic as they were using PHP Reflections to process and hide the flow of sending emails. It was also found that they were using Swift Mailer that use a Spooling mechanism to store temp.file against the core email address of a single lead along with the configurations in a serialized form. Once the Cron job hit the send email function, it would read that folder/file. Then an email would be sent. After that, it would delete that particular temp.file. No help was available over the internet or at Mautic’s forum.

Solution:

After an extensive R&D, Zeeshan found a way for adding custom fields to store multiple emails per lead. He utilized an already developed option in Mautic to create nth number of extra email fields for a single lead.

The major work was done in Mautic’s core code. The logic of creating temp.file and sending email was changed. Mautic used “Field Alias” to identify the email. As the rule states that you cannot make same name columns in a database table, so we decide to change the logic and made Mautic to read “Field Type” rather than “Field Alias”.

By doing so, vteam #480 was able to achieve the required outcome. The code for the changed logic is as follows:

foreach($lead->getFields(1) as $field)
            {
                if($otherActions) {
                    $otherActions = false;
                    //trigger the action
                    $response = $this->invokeEventCallback($event, $thisEventSettings, $lead, null, true, $log, $field['value'], true);
                }else {
                    if($field['type'] == 'email') {
                        //trigger the action

                        $response = $this->invokeEventCallback($event, $thisEventSettings, $lead, null, true, $log, $field['value'], false);
                    }
                }   
            }


public static function sendEmailAction(MauticFactory $factory, $lead, $event, $email)
    {
        $emailSent = false;

        if ($lead instanceof Lead) {
            $fields = $lead->getFields();
            /** @var \Mautic\LeadBundle\Model\LeadModel $leadModel */
            $leadModel             = $factory->getModel('lead');
            $leadCredentials       = $leadModel->flattenFields($fields);
            $leadCredentials['id'] = $lead->getId();
        } else {
            $leadCredentials = $lead;
        }
        
            $leadCredentials['email'] = $email; // Modified by V-Teams (Zeeshan Ahmad)
            /** @var \Mautic\EmailBundle\Model\EmailModel $emailModel */
            $emailModel = $factory->getModel('email');

            $emailId = (int) $event['properties']['email'];

            $email = $emailModel->getEntity($emailId);

            if ($email != null && $email->isPublished()) {
                $options   = array('source' => array('campaign', $event['campaign']['id']));
                $emailSent = $emailModel->sendEmail($email, $leadCredentials, $options);
            }
           
            unset($lead, $leadCredentials, $email, $emailModel, $factory);

        return $emailSent;
    }
}

Conclusion:

Mautic team must analyze the above mentioned code’s health and test it. If find it good enough then they should make it available for public in their next release.

ABOUT THE AUTHOR

Muhammad Ahmad

0 Comments

Leave a Reply

More Related Article
We provide tips and advice on delivering excellent customer service, engaging your customers, and building a customer-centric business.