Example #1
0
 /**
  * Generate an event queue for a test job.
  *
  * @param array $testParams
  *   Contains form values.
  *
  * @return void
  */
 public function getTestRecipients($testParams)
 {
     $session = CRM_Core_Session::singleton();
     $senderId = $session->get('userID');
     list($aclJoin, $aclWhere) = CRM_ACL_BAO_ACL::buildAcl($senderId);
     if (array_key_exists($testParams['test_group'], CRM_Core_PseudoConstant::group())) {
         $contacts = civicrm_api('contact', 'get', array('version' => 3, 'group' => $testParams['test_group'], 'return' => 'id', 'options' => array('limit' => 100000000000)));
         foreach (array_keys($contacts['values']) as $groupContact) {
             $query = "\nSELECT     civicrm_email.id AS email_id,\n           civicrm_email.is_primary as is_primary,\n           civicrm_email.is_bulkmail as is_bulkmail\nFROM       civicrm_email\nINNER JOIN civicrm_contact contact_a ON civicrm_email.contact_id = contact_a.id\n{$aclJoin}\nWHERE      (civicrm_email.is_bulkmail = 1 OR civicrm_email.is_primary = 1)\nAND        contact_a.id = {$groupContact}\nAND        contact_a.do_not_email = 0\nAND        contact_a.is_deceased <> 1\nAND        civicrm_email.on_hold = 0\nAND        contact_a.is_opt_out = 0\n{$aclWhere}\nGROUP BY   civicrm_email.id\nORDER BY   civicrm_email.is_bulkmail DESC\n";
             $dao = CRM_Core_DAO::executeQuery($query);
             if ($dao->fetch()) {
                 $params = array('job_id' => $testParams['job_id'], 'email_id' => $dao->email_id, 'contact_id' => $groupContact);
                 CRM_Mailing_Event_BAO_Queue::create($params);
             }
         }
     }
 }
Example #2
0
 /**
  * Send the mailing.
  *
  * @param object $mailer
  *   A Mail object to send the messages.
  *
  * @param array $testParams
  */
 public function deliver(&$mailer, $testParams = NULL)
 {
     $mailing = new CRM_Mailing_BAO_Mailing();
     $mailing->id = $this->mailing_id;
     $mailing->find(TRUE);
     $mailing->free();
     $eq = new CRM_Mailing_Event_BAO_Queue();
     $eqTable = CRM_Mailing_Event_BAO_Queue::getTableName();
     $emailTable = CRM_Core_BAO_Email::getTableName();
     $phoneTable = CRM_Core_DAO_Phone::getTableName();
     $contactTable = CRM_Contact_BAO_Contact::getTableName();
     $edTable = CRM_Mailing_Event_BAO_Delivered::getTableName();
     $ebTable = CRM_Mailing_Event_BAO_Bounce::getTableName();
     list($aclJoin, $aclWhere) = CRM_ACL_BAO_ACL::buildAcl($mailing->created_id);
     $query = "  SELECT      {$eqTable}.id,\n                                {$emailTable}.email as email,\n                                {$eqTable}.contact_id,\n                                {$eqTable}.hash,\n                                NULL as phone\n                    FROM        {$eqTable}\n                    INNER JOIN  {$emailTable}\n                            ON  {$eqTable}.email_id = {$emailTable}.id\n                    INNER JOIN  {$contactTable} contact_a\n                            ON  contact_a.id = {$emailTable}.contact_id\n                    LEFT JOIN   {$edTable}\n                            ON  {$eqTable}.id = {$edTable}.event_queue_id\n                    LEFT JOIN   {$ebTable}\n                            ON  {$eqTable}.id = {$ebTable}.event_queue_id\n                    {$aclJoin}\n                    WHERE       {$eqTable}.job_id = " . $this->id . "\n                        AND     {$edTable}.id IS null\n                        AND     {$ebTable}.id IS null\n                        AND    contact_a.is_opt_out = 0 \n                        {$aclWhere}";
     if ($mailing->sms_provider_id) {
         $query = "\n                    SELECT      {$eqTable}.id,\n                                {$phoneTable}.phone as phone,\n                                {$eqTable}.contact_id,\n                                {$eqTable}.hash,\n                                NULL as email\n                    FROM        {$eqTable}\n                    INNER JOIN  {$phoneTable}\n                            ON  {$eqTable}.phone_id = {$phoneTable}.id\n                    INNER JOIN  {$contactTable} contact_a\n                            ON  contact_a.id = {$phoneTable}.contact_id\n                    LEFT JOIN   {$edTable}\n                            ON  {$eqTable}.id = {$edTable}.event_queue_id\n                    LEFT JOIN   {$ebTable}\n                            ON  {$eqTable}.id = {$ebTable}.event_queue_id\n                    {$aclJoin}\n                    WHERE       {$eqTable}.job_id = " . $this->id . "\n                        AND     {$edTable}.id IS null\n                        AND     {$ebTable}.id IS null\n                        AND    ( contact_a.is_opt_out = 0\n                        OR       contact_a.do_not_sms = 0 )\n                        {$aclWhere}";
     }
     $eq->query($query);
     $config = NULL;
     if ($config == NULL) {
         $config = CRM_Core_Config::singleton();
     }
     if (property_exists($mailing, 'language') && $mailing->language && $mailing->language != 'en_US') {
         $swapLang = CRM_Utils_AutoClean::swap('global://dbLocale?getter', 'call://i18n/setLocale', $mailing->language);
     }
     $job_date = CRM_Utils_Date::isoToMysql($this->scheduled_date);
     $fields = array();
     if (!empty($testParams)) {
         $mailing->subject = ts('[CiviMail Draft]') . ' ' . $mailing->subject;
     }
     CRM_Mailing_BAO_Mailing::tokenReplace($mailing);
     // get and format attachments
     $attachments = CRM_Core_BAO_File::getEntityFile('civicrm_mailing', $mailing->id);
     if (defined('CIVICRM_MAIL_SMARTY') && CIVICRM_MAIL_SMARTY) {
         CRM_Core_Smarty::registerStringResource();
     }
     // CRM-12376
     // This handles the edge case scenario where all the mails
     // have been delivered in prior jobs.
     $isDelivered = TRUE;
     // make sure that there's no more than $mailerBatchLimit mails processed in a run
     $mailerBatchLimit = Civi::settings()->get('mailerBatchLimit');
     while ($eq->fetch()) {
         // if ( ( $mailsProcessed % 100 ) == 0 ) {
         // CRM_Utils_System::xMemory( "$mailsProcessed: " );
         // }
         if ($mailerBatchLimit > 0 && self::$mailsProcessed >= $mailerBatchLimit) {
             if (!empty($fields)) {
                 $this->deliverGroup($fields, $mailing, $mailer, $job_date, $attachments);
             }
             $eq->free();
             return FALSE;
         }
         self::$mailsProcessed++;
         $fields[] = array('id' => $eq->id, 'hash' => $eq->hash, 'contact_id' => $eq->contact_id, 'email' => $eq->email, 'phone' => $eq->phone);
         if (count($fields) == self::MAX_CONTACTS_TO_PROCESS) {
             $isDelivered = $this->deliverGroup($fields, $mailing, $mailer, $job_date, $attachments);
             if (!$isDelivered) {
                 $eq->free();
                 return $isDelivered;
             }
             $fields = array();
         }
     }
     $eq->free();
     if (!empty($fields)) {
         $isDelivered = $this->deliverGroup($fields, $mailing, $mailer, $job_date, $attachments);
     }
     return $isDelivered;
 }