/** * @param CampaignExecutionEvent $event */ public function onCampaignTriggerAction(CampaignExecutionEvent $event) { $emailSent = false; $lead = $event->getLead(); $leadCredentials = $lead instanceof Lead ? $lead->getProfileFields() : $lead; $leadCredentials['owner_id'] = $lead instanceof Lead && ($owner = $lead->getOwner()) ? $owner->getId() : 0; if (!empty($leadCredentials['email'])) { $config = $event->getConfig(); $emailId = (int) $config['email']; $email = $this->emailModel->getEntity($emailId); if ($email != null && $email->isPublished()) { // Determine if this email is transactional/marketing $type = isset($config['email_type']) ? $config['email_type'] : 'transactional'; if ('marketing' == $type) { // Determine if this lead has received the email before $stats = $this->emailModel->getStatRepository()->findContactEmailStats($leadCredentials['id'], $emailId); if (count($stats)) { // Already sent return $event->setResult(true); } } $eventDetails = $event->getEventDetails(); $options = ['source' => ['campaign', $eventDetails['campaign']['id']]]; $emailSent = $this->emailModel->sendEmail($email, $leadCredentials, $options); } $event->setChannel('email', $emailId); } return $event->setResult($emailSent); }
/** * @param CampaignExecutionEvent $event */ public function onCampaignTriggerAction(CampaignExecutionEvent $event) { $emailSent = false; $lead = $event->getLead(); $leadCredentials = $lead instanceof Lead ? $lead->getProfileFields() : $lead; $leadCredentials['owner_id'] = $lead instanceof Lead && ($owner = $lead->getOwner()) ? $owner->getId() : 0; if (!empty($leadCredentials['email'])) { $config = $event->getConfig(); $emailId = (int) $config['email']; $email = $this->emailModel->getEntity($emailId); $type = isset($config['email_type']) ? $config['email_type'] : 'transactional'; $options = ['source' => ['campaign.event', $event->getEvent()['id']], 'email_attempts' => isset($config['attempts']) ? $config['attempts'] : 3, 'email_priority' => isset($config['priority']) ? $config['priority'] : 2, 'email_type' => $type]; $event->setChannel('email', $emailId); if ($email != null && $email->isPublished()) { // Determine if this email is transactional/marketing $stats = []; if ('marketing' == $type) { // Determine if this lead has received the email before $leadIds = implode(',', [$leadCredentials['id']]); $stats = $this->emailModel->getStatRepository()->checkContactsSentEmail($leadIds, $emailId); $emailSent = true; // Assume it was sent to prevent the campaign event from getting rescheduled over and over } if (empty($stats)) { $emailSent = $this->emailModel->sendEmail($email, $leadCredentials, $options); } } else { return $event->setFailed('Email not found or published'); } } else { return $event->setFailed('Contact does not have an email'); } return $event->setResult($emailSent); }