$status = array('Scheduled', 'Running', 'Complete', 'Paused', 'Canceled', 'Testing'); for ($i = 1; $i <= $numGroups; $i++) { $mailing = new CRM_Mailing_BAO_Mailing(); $alphabet = mt_rand(97, 122); $cnt = sprintf('%05d', $i); $mailing->name = chr($alphabet) . ": {$prefix} {$cnt}"; $mailing->header_id = $mailing->footer_id = $mailing->reply_id = $mailing->unsubscribe_id = $mailing->optout_id = 1; $mailing->is_completed = 1; $mailing->save(); $job = new CRM_Mailing_BAO_MailingJob(); $job->mailing_id = $mailing->id; $job->scheduled_date = generateRandomDate(); $job->start_date = generateRandomDate(); $job->end_date = generateRandomDate(); $job->status = 'Complete'; $job->save(); $group = new CRM_Mailing_DAO_MailingGroup(); $group->mailing_id = $mailing->id; $group->group_type = 'Include'; $group->entity_table = 'civicrm_group'; $group->entity_id = 1; $group->save(); } /** * @return string */ function generateRandomDate() { $year = 2006 + mt_rand(0, 2); $month = 1 + mt_rand(0, 11); $day = 1 + mt_rand(0, 27);
/** * Form rule to send out a test mailing. * * @param aray $testParams * @param array $files * Any files posted to the form. * @param array $self * An current this object. * * @return bool * true on successful SMTP handoff */ public static function testMail($testParams, $files, $self) { $error = NULL; $urlString = 'civicrm/mailing/send'; $urlParams = "_qf_Test_display=true&qfKey={$testParams['qfKey']}"; $ssID = $self->get('ssID'); if ($ssID && $self->_searchBasedMailing) { if ($self->_action == CRM_Core_Action::BASIC) { $fragment = 'search'; } elseif ($self->_action == CRM_Core_Action::PROFILE) { $fragment = 'search/builder'; } elseif ($self->_action == CRM_Core_Action::ADVANCED) { $fragment = 'search/advanced'; } else { $fragment = 'search/custom'; } $urlString = 'civicrm/contact/' . $fragment; } $emails = NULL; if (!empty($testParams['sendtest'])) { if (!($testParams['test_group'] || $testParams['test_email'])) { CRM_Core_Session::setStatus(ts('You did not provide an email address or select a group.'), ts('Test not sent.'), 'error'); $error = TRUE; } if ($testParams['test_email']) { $emailAdd = explode(',', $testParams['test_email']); foreach ($emailAdd as $key => $value) { $email = trim($value); $testParams['emails'][] = $email; $emails .= ($emails ? ',' : '') . "'" . CRM_Core_DAO::escapeString($email) . "'"; if (!CRM_Utils_Rule::email($email)) { CRM_Core_Session::setStatus(ts('Please enter a valid email address.'), ts('Test not sent.'), 'error'); $error = TRUE; } } } if ($error) { $url = CRM_Utils_System::url($urlString, $urlParams); CRM_Utils_System::redirect($url); return $error; } } if (!empty($testParams['_qf_Test_submit'])) { //when user perform mailing from search context //redirect it to search result CRM-3711. if ($ssID && $self->_searchBasedMailing) { $draftURL = CRM_Utils_System::url('civicrm/mailing/browse/unscheduled', 'scheduled=false&reset=1'); $status = ts("You can continue later by clicking the 'Continue' action to resume working on it.<br />From <a href='%1'>Draft and Unscheduled Mailings</a>.", array(1 => $draftURL)); //replace user context to search. $context = $self->get('context'); if (!CRM_Contact_Form_Search::isSearchContext($context)) { $context = 'search'; } $urlParams = "force=1&reset=1&ssID={$ssID}&context={$context}&qfKey={$testParams['qfKey']}"; $url = CRM_Utils_System::url($urlString, $urlParams); } else { $status = ts("Click the 'Continue' action to resume working on it."); $url = CRM_Utils_System::url('civicrm/mailing/browse/unscheduled', 'scheduled=false&reset=1'); } CRM_Core_Session::setStatus($status, ts('Mailing Saved'), 'success'); CRM_Utils_System::redirect($url); } if (CRM_Mailing_Info::workflowEnabled()) { if (!CRM_Core_Permission::check('schedule mailings') && CRM_Core_Permission::check('create mailings')) { $url = CRM_Utils_System::url('civicrm/mailing/browse/unscheduled', 'scheduled=false&reset=1'); CRM_Utils_System::redirect($url); } } if (!empty($testParams['_qf_Test_next']) && $self->get('count') <= 0) { return array('_qf_default' => ts("You can not schedule or send this mailing because there are currently no recipients selected. Click 'Previous' to return to the Select Recipients step, OR click 'Save & Continue Later'.")); } if (!empty($_POST['_qf_Import_refresh']) || !empty($testParams['_qf_Test_next']) || empty($testParams['sendtest'])) { $error = TRUE; return $error; } $job = new CRM_Mailing_BAO_MailingJob(); $job->mailing_id = $self->get('mailing_id'); $job->is_test = TRUE; $job->save(); $newEmails = NULL; $session = CRM_Core_Session::singleton(); if (!empty($testParams['emails'])) { $query = "\nSELECT e.id, e.contact_id, e.email\nFROM civicrm_email e\nINNER JOIN civicrm_contact c ON e.contact_id = c.id\nWHERE e.email IN ({$emails})\nAND e.on_hold = 0\nAND c.is_opt_out = 0\nAND c.do_not_email = 0\nAND c.is_deleted = 0\nAND c.is_deceased = 0\nGROUP BY e.id\nORDER BY e.is_bulkmail DESC, e.is_primary DESC\n"; $dao = CRM_Core_DAO::executeQuery($query); $emailDetail = array(); // fetch contact_id and email id for all existing emails while ($dao->fetch()) { $emailDetail[$dao->email] = array('contact_id' => $dao->contact_id, 'email_id' => $dao->id); } $dao->free(); foreach ($testParams['emails'] as $key => $email) { // Email addresses are forced to lower case when saved, so ensure // we have the same case when comparing. $email = trim(strtolower($email)); $contactId = $emailId = NULL; if (array_key_exists($email, $emailDetail)) { $emailId = $emailDetail[$email]['email_id']; $contactId = $emailDetail[$email]['contact_id']; } if (!$contactId) { //create new contact. $params = array('contact_type' => 'Individual', 'email' => array(1 => array('email' => $email, 'is_primary' => 1, 'location_type_id' => 1))); $contact = CRM_Contact_BAO_Contact::create($params); $emailId = $contact->email[0]->id; $contactId = $contact->id; $contact->free(); } $params = array('job_id' => $job->id, 'email_id' => $emailId, 'contact_id' => $contactId); CRM_Mailing_Event_BAO_Queue::create($params); } } $testParams['job_id'] = $job->id; $isComplete = FALSE; while (!$isComplete) { $isComplete = CRM_Mailing_BAO_MailingJob::runJobs($testParams); } if (!empty($testParams['sendtest'])) { $status = NULL; if (CRM_Mailing_Info::workflowEnabled()) { if (CRM_Core_Permission::check('schedule mailings') && CRM_Core_Permission::check('create mailings') || CRM_Core_Permission::check('access CiviMail')) { $status = ts("Click 'Next' when you are ready to Schedule or Send your live mailing (you will still have a chance to confirm or cancel sending this mailing on the next page)."); } } else { $status = ts("Click 'Next' when you are ready to Schedule or Send your live mailing (you will still have a chance to confirm or cancel sending this mailing on the next page)."); } if ($status) { CRM_Core_Session::setStatus($status, ts('Test message sent'), 'success'); } $url = CRM_Utils_System::url($urlString, $urlParams); CRM_Utils_System::redirect($url); } $error = TRUE; return $error; }
/** * Cancel a mailing. * * @param int $mailingId * The id of the mailing to be canceled. */ public static function cancel($mailingId) { $sql = "\nSELECT *\nFROM civicrm_mailing_job\nWHERE mailing_id = %1\nAND is_test = 0\nAND ( ( job_type IS NULL ) OR\n job_type <> 'child' )\n"; $params = array(1 => array($mailingId, 'Integer')); $job = CRM_Core_DAO::executeQuery($sql, $params); if ($job->fetch() && in_array($job->status, array('Scheduled', 'Running', 'Paused'))) { $newJob = new CRM_Mailing_BAO_MailingJob(); $newJob->id = $job->id; $newJob->end_date = date('YmdHis'); $newJob->status = 'Canceled'; $newJob->save(); // also cancel all child jobs $sql = "\nUPDATE civicrm_mailing_job\nSET status = 'Canceled',\n end_date = %2\nWHERE parent_id = %1\nAND is_test = 0\nAND job_type = 'child'\nAND status IN ( 'Scheduled', 'Running', 'Paused' )\n"; $params = array(1 => array($job->id, 'Integer'), 2 => array(date('YmdHis'), 'Timestamp')); CRM_Core_DAO::executeQuery($sql, $params); CRM_Core_Session::setStatus(ts('The mailing has been canceled.'), ts('Canceled'), 'success'); } }
/** * Construct a new mailing object, along with job and mailing_group * objects, from the form values of the create mailing wizard. * * This function is a bit evil. It not only merges $params and saves * the mailing -- it also schedules the mailing and chooses the recipients. * Since it merges $params, it's also the only place to correctly trigger * multi-field validation. It should be broken up. * * In the mean time, use-cases which break under the weight of this * evil may find reprieve in these extra evil params: * * - _skip_evil_bao_auto_recipients_: bool * - _skip_evil_bao_auto_schedule_: bool * - _evil_bao_validator_: string|callable * * </twowrongsmakesaright> * * @params array $params * Form values. * * @param array $params * @param array $ids * * @return object * $mailing The new mailing object * @throws \Exception */ public static function create(&$params, $ids = array()) { // WTH $ids if (empty($ids) && isset($params['id'])) { $ids['mailing_id'] = $ids['id'] = $params['id']; } // CRM-12430 // Do the below only for an insert // for an update, we should not set the defaults if (!isset($ids['id']) && !isset($ids['mailing_id'])) { // Retrieve domain email and name for default sender $domain = civicrm_api('Domain', 'getsingle', array('version' => 3, 'current_domain' => 1, 'sequential' => 1)); if (isset($domain['from_email'])) { $domain_email = $domain['from_email']; $domain_name = $domain['from_name']; } else { $domain_email = '*****@*****.**'; $domain_name = 'EXAMPLE.ORG'; } if (!isset($params['created_id'])) { $session =& CRM_Core_Session::singleton(); $params['created_id'] = $session->get('userID'); } $defaults = array('override_verp' => TRUE, 'forward_replies' => FALSE, 'open_tracking' => TRUE, 'url_tracking' => TRUE, 'visibility' => 'Public Pages', 'replyto_email' => $domain_email, 'header_id' => CRM_Mailing_PseudoConstant::defaultComponent('header_id', ''), 'footer_id' => CRM_Mailing_PseudoConstant::defaultComponent('footer_id', ''), 'from_email' => $domain_email, 'from_name' => $domain_name, 'msg_template_id' => NULL, 'created_id' => $params['created_id'], 'approver_id' => NULL, 'auto_responder' => 0, 'created_date' => date('YmdHis'), 'scheduled_date' => NULL, 'approval_date' => NULL); // Get the default from email address, if not provided. if (empty($defaults['from_email'])) { $defaultAddress = CRM_Core_OptionGroup::values('from_email_address', NULL, NULL, NULL, ' AND is_default = 1'); foreach ($defaultAddress as $id => $value) { if (preg_match('/"(.*)" <(.*)>/', $value, $match)) { $defaults['from_email'] = $match[2]; $defaults['from_name'] = $match[1]; } } } $params = array_merge($defaults, $params); } /** * Could check and warn for the following cases: * * - groups OR mailings should be populated. * - body html OR body text should be populated. */ $transaction = new CRM_Core_Transaction(); $mailing = self::add($params, $ids); if (is_a($mailing, 'CRM_Core_Error')) { $transaction->rollback(); return $mailing; } // update mailings with hash values CRM_Contact_BAO_Contact_Utils::generateChecksum($mailing->id, NULL, NULL, NULL, 'mailing', 16); $groupTableName = CRM_Contact_BAO_Group::getTableName(); $mailingTableName = CRM_Mailing_BAO_Mailing::getTableName(); /* Create the mailing group record */ $mg = new CRM_Mailing_DAO_MailingGroup(); $groupTypes = array('include' => 'Include', 'exclude' => 'Exclude', 'base' => 'Base'); foreach (array('groups', 'mailings') as $entity) { foreach (array('include', 'exclude', 'base') as $type) { if (isset($params[$entity][$type])) { self::replaceGroups($mailing->id, $groupTypes[$type], $entity, $params[$entity][$type]); } } } if (!empty($params['search_id']) && !empty($params['group_id'])) { $mg->reset(); $mg->mailing_id = $mailing->id; $mg->entity_table = $groupTableName; $mg->entity_id = $params['group_id']; $mg->search_id = $params['search_id']; $mg->search_args = $params['search_args']; $mg->group_type = 'Include'; $mg->save(); } // check and attach and files as needed CRM_Core_BAO_File::processAttachment($params, 'civicrm_mailing', $mailing->id); // If we're going to autosend, then check validity before saving. if (!empty($params['scheduled_date']) && $params['scheduled_date'] != 'null' && !empty($params['_evil_bao_validator_'])) { $cb = Civi\Core\Resolver::singleton()->get($params['_evil_bao_validator_']); $errors = call_user_func($cb, $mailing); if (!empty($errors)) { $fields = implode(',', array_keys($errors)); throw new CRM_Core_Exception("Mailing cannot be sent. There are missing or invalid fields ({$fields}).", 'cannot-send', $errors); } } $transaction->commit(); // Create parent job if not yet created. // Condition on the existence of a scheduled date. if (!empty($params['scheduled_date']) && $params['scheduled_date'] != 'null' && empty($params['_skip_evil_bao_auto_schedule_'])) { $job = new CRM_Mailing_BAO_MailingJob(); $job->mailing_id = $mailing->id; $job->status = 'Scheduled'; $job->is_test = 0; if (!$job->find(TRUE)) { $job->scheduled_date = $params['scheduled_date']; $job->save(); } // Populate the recipients. if (empty($params['_skip_evil_bao_auto_recipients_'])) { self::getRecipients($job->id, $mailing->id, TRUE, $mailing->dedupe_email); } } return $mailing; }
/** * @param $mailingId * * @return CRM_Mailing_BAO_MailingJob */ private static function createTestMailingJob($mailingId) { $job = new CRM_Mailing_BAO_MailingJob(); $job->mailing_id = $mailingId; $job->is_test = TRUE; $job->save(); return $job; }
/** * Construct a new mailing object, along with job and mailing_group * objects, from the form values of the create mailing wizard. * * @params array $params Form values * * @return object $mailing The new mailing object * @access public * @static */ public static function create(&$params, $ids = array()) { // CRM-12430 // Do the below only for an insert // for an update, we should not set the defaults if (!isset($ids['id']) && !isset($ids['mailing_id'])) { // Retrieve domain email and name for default sender $domain = civicrm_api('Domain', 'getsingle', array('version' => 3, 'current_domain' => 1, 'sequential' => 1)); if (isset($domain['from_email'])) { $domain_email = $domain['from_email']; $domain_name = $domain['from_name']; } else { $domain_email = '*****@*****.**'; $domain_name = 'EXAMPLE.ORG'; } if (!isset($params['created_id'])) { $session =& CRM_Core_Session::singleton(); $params['created_id'] = $session->get('userID'); } $defaults = array('override_verp' => TRUE, 'forward_replies' => FALSE, 'open_tracking' => TRUE, 'url_tracking' => TRUE, 'visibility' => 'User and User Admin Only', 'replyto_email' => $domain_email, 'header_id' => CRM_Mailing_PseudoConstant::defaultComponent('header_id', ''), 'footer_id' => CRM_Mailing_PseudoConstant::defaultComponent('footer_id', ''), 'from_email' => $domain_email, 'from_name' => $domain_name, 'msg_template_id' => NULL, 'created_id' => $params['created_id'], 'approver_id' => NULL, 'auto_responder' => 0, 'created_date' => date('YmdHis'), 'scheduled_date' => NULL, 'approval_date' => NULL); // Get the default from email address, if not provided. if (empty($defaults['from_email'])) { $defaultAddress = CRM_Core_OptionGroup::values('from_email_address', NULL, NULL, NULL, ' AND is_default = 1'); foreach ($defaultAddress as $id => $value) { if (preg_match('/"(.*)" <(.*)>/', $value, $match)) { $defaults['from_email'] = $match[2]; $defaults['from_name'] = $match[1]; } } } $params = array_merge($defaults, $params); } /** * Could check and warn for the following cases: * * - groups OR mailings should be populated. * - body html OR body text should be populated. */ $transaction = new CRM_Core_Transaction(); $mailing = self::add($params, $ids); if (is_a($mailing, 'CRM_Core_Error')) { $transaction->rollback(); return $mailing; } $groupTableName = CRM_Contact_BAO_Group::getTableName(); $mailingTableName = CRM_Mailing_BAO_Mailing::getTableName(); /* Create the mailing group record */ $mg = new CRM_Mailing_DAO_MailingGroup(); foreach (array('groups', 'mailings') as $entity) { foreach (array('include', 'exclude', 'base') as $type) { if (isset($params[$entity]) && CRM_Utils_Array::value($type, $params[$entity]) && is_array($params[$entity][$type])) { foreach ($params[$entity][$type] as $entityId) { $mg->reset(); $mg->mailing_id = $mailing->id; $mg->entity_table = $entity == 'groups' ? $groupTableName : $mailingTableName; $mg->entity_id = $entityId; $mg->group_type = $type; $mg->save(); } } } } if (!empty($params['search_id']) && !empty($params['group_id'])) { $mg->reset(); $mg->mailing_id = $mailing->id; $mg->entity_table = $groupTableName; $mg->entity_id = $params['group_id']; $mg->search_id = $params['search_id']; $mg->search_args = $params['search_args']; $mg->group_type = 'Include'; $mg->save(); } // check and attach and files as needed CRM_Core_BAO_File::processAttachment($params, 'civicrm_mailing', $mailing->id); $transaction->commit(); /** * create parent job if not yet created * condition on the existence of a scheduled date */ if (!empty($params['scheduled_date']) && $params['scheduled_date'] != 'null') { $job = new CRM_Mailing_BAO_MailingJob(); $job->mailing_id = $mailing->id; $job->status = 'Scheduled'; $job->is_test = 0; if (!$job->find(TRUE)) { $job->scheduled_date = $params['scheduled_date']; $job->save(); } // Populate the recipients. $mailing->getRecipients($job->id, $mailing->id, NULL, NULL, TRUE, FALSE); } return $mailing; }
/** * Store Mails into Spool table. * * @param string|array $recipient * Either a comma-seperated list of recipients * (RFC822 compliant), or an array of recipients, * each RFC822 valid. This may contain recipients not * specified in the headers, for Bcc:, resending * messages, etc. * @param array $headers * The array of headers to send with the mail. * * @param string $body * The full text of the message body, including any mime parts, etc. * * @param int $job_id * * @return bool|CRM_Core_Error * true if successful */ public function send($recipient, $headers, $body, $job_id = NULL) { $headerStr = array(); foreach ($headers as $name => $value) { $headerStr[] = "{$name}: {$value}"; } $headerStr = implode("\n", $headerStr); if (is_null($job_id)) { // This is not a bulk mailing. Create a dummy job for it. $session = CRM_Core_Session::singleton(); $params = array(); $params['created_id'] = $session->get('userID'); $params['created_date'] = date('YmdHis'); $params['scheduled_id'] = $params['created_id']; $params['scheduled_date'] = $params['created_date']; $params['is_completed'] = 1; $params['is_archived'] = 1; $params['body_html'] = htmlspecialchars($headerStr) . "\n\n" . $body; $params['subject'] = $headers['Subject']; $params['name'] = $headers['Subject']; $ids = array(); $mailing = CRM_Mailing_BAO_Mailing::create($params, $ids); if (empty($mailing) || is_a($mailing, 'CRM_Core_Error')) { return PEAR::raiseError('Unable to create spooled mailing.'); } $job = new CRM_Mailing_BAO_MailingJob(); $job->is_test = 0; // if set to 1 it doesn't show in the UI $job->status = 'Complete'; $job->scheduled_date = CRM_Utils_Date::processDate(date('Y-m-d'), date('H:i:s')); $job->start_date = $job->scheduled_date; $job->end_date = $job->scheduled_date; $job->mailing_id = $mailing->id; $job->save(); $job_id = $job->id; // need this for parent_id below $job = new CRM_Mailing_BAO_MailingJob(); $job->is_test = 0; $job->status = 'Complete'; $job->scheduled_date = CRM_Utils_Date::processDate(date('Y-m-d'), date('H:i:s')); $job->start_date = $job->scheduled_date; $job->end_date = $job->scheduled_date; $job->mailing_id = $mailing->id; $job->parent_id = $job_id; $job->job_type = 'child'; $job->save(); $job_id = $job->id; // this is the one we want for the spool if (is_array($recipient)) { $recipient = implode(';', $recipient); } } $session = CRM_Core_Session::singleton(); $params = array('job_id' => $job_id, 'recipient_email' => $recipient, 'headers' => $headerStr, 'body' => $body, 'added_at' => date("YmdHis"), 'removed_at' => NULL); $spoolMail = new CRM_Mailing_DAO_Spool(); $spoolMail->copyValues($params); $spoolMail->save(); return TRUE; }
function postProcess() { $params = array(); $params['mailing_id'] = $ids['mailing_id'] = $this->_mailingID; if (empty($params['mailing_id'])) { CRM_Core_Error::fatal(ts('Could not find a mailing id')); } foreach (array('now', 'start_date', 'start_date_time') as $parameter) { $params[$parameter] = $this->controller->exportValue($this->_name, $parameter); } $mailing = new CRM_Mailing_BAO_Mailing(); $mailing->id = $ids['mailing_id']; if ($mailing->find(TRUE)) { $job = new CRM_Mailing_BAO_MailingJob(); $job->mailing_id = $mailing->id; $job->is_test = 0; if ($job->find(TRUE)) { CRM_Core_Error::fatal(ts('A job for this mailing already exists')); } if (empty($mailing->is_template)) { $job->status = 'Scheduled'; if ($params['now']) { $job->scheduled_date = date('YmdHis'); } else { $job->scheduled_date = CRM_Utils_Date::processDate($params['start_date'] . ' ' . $params['start_date_time']); } $job->save(); } // set approval details if workflow is not enabled if (!CRM_Mailing_Info::workflowEnabled()) { $session = CRM_Core_Session::singleton(); $mailing->approver_id = $session->get('userID'); $mailing->approval_date = date('YmdHis'); $mailing->approval_status_id = 1; } else { // reset them in case this mailing was rejected $mailing->approver_id = 'null'; $mailing->approval_date = 'null'; $mailing->approval_status_id = 'null'; } if ($mailing->approval_date) { $mailing->approval_date = CRM_Utils_Date::isoToMysql($mailing->approval_date); } // also set the scheduled_id $session = CRM_Core_Session::singleton(); $mailing->scheduled_id = $session->get('userID'); $mailing->scheduled_date = date('YmdHis'); $mailing->created_date = CRM_Utils_Date::isoToMysql($mailing->created_date); $mailing->save(); } $status = ts("Your mailing has been saved."); CRM_Core_Session::setStatus($status); $url = CRM_Utils_System::url('civicrm/view/quickbulkemail'); return $this->controller->setDestination($url); }