示例#1
0
 /**
  * @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);
 }
示例#2
0
 /**
  * @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);
 }
 /**
  * Sends campaign emails.
  *
  * @param MessageQueueBatchProcessEvent $event
  */
 public function onProcessMessageQueueBatch(MessageQueueBatchProcessEvent $event)
 {
     if (!$event->checkContext('email')) {
         return;
     }
     $messages = $event->getMessages();
     $emailId = $event->getChannelId();
     $email = $this->emailModel->getEntity($emailId);
     $sendTo = [];
     $messagesByContact = [];
     $options = ['email_type' => 'marketing'];
     /** @var MessageQueue $message */
     foreach ($messages as $id => $message) {
         $contact = $message->getLead()->getProfileFields();
         if (empty($contact['email'])) {
             // No email so just let this slide
             $message->setProcessed();
             $message->setSuccess();
         }
         $sendTo[$contact['id']] = $contact;
         $messagesByContact[$contact['id']] = $message;
     }
     if (count($sendTo)) {
         $options['resend_message_queue'] = $messagesByContact;
         $errors = $this->emailModel->sendEmail($email, $sendTo, $options);
         // Let's see who was successful
         foreach ($messagesByContact as $contactId => $message) {
             // If the message is processed, it was rescheduled by sendEmail
             if (!$message->isProcessed()) {
                 $message->setProcessed();
                 if (empty($errors[$contactId])) {
                     $message->setSuccess();
                 }
             }
         }
     }
     $event->stopPropagation();
 }