3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 require_once 'Mail/mime.php';
20 * Class CRM_Mailing_BAO_Mailing
22 class CRM_Mailing_BAO_Mailing
extends CRM_Mailing_DAO_Mailing
{
25 * An array that holds the complete templates
26 * including any headers or footers that need to be prepended
27 * or appended to the body.
30 private $preparedTemplates = NULL;
33 * An array that holds the complete templates
34 * including any headers or footers that need to be prepended
35 * or appended to the body.
38 private $templates = NULL;
41 * An array that holds the tokens that are specifically found in our text and html bodies.
44 private $tokens = NULL;
47 * An array that holds the tokens that are specifically found in our text and html bodies.
50 private $flattenedTokens = NULL;
53 * The header associated with this mailing.
56 private $header = NULL;
59 * The footer associated with this mailing.
62 private $footer = NULL;
65 * The HTML content of the message.
71 * The text content of the message.
77 * Cached BAO for the domain.
80 private $_domain = NULL;
85 public function __construct() {
86 parent
::__construct();
92 * @param int $mailingID
96 public static function getRecipientsCount($mailingID) {
97 //rebuild the recipients
98 self
::getRecipients($mailingID);
100 return civicrm_api3('MailingRecipients', 'getcount', ['mailing_id' => $mailingID]);
104 * This function retrieve recipients of selected mailing groups.
106 * @param int $mailingID
110 public static function getRecipients($mailingID) {
111 // load mailing object
112 $mailingObj = new self();
113 $mailingObj->id
= $mailingID;
114 $mailingObj->find(TRUE);
116 $mailing = CRM_Mailing_BAO_Mailing
::getTableName();
117 $contact = CRM_Contact_DAO_Contact
::getTableName();
118 $isSMSmode = (!CRM_Utils_System
::isNull($mailingObj->sms_provider_id
));
120 $mailingGroup = new CRM_Mailing_DAO_MailingGroup();
121 $recipientsGroup = $excludeSmartGroupIDs = $includeSmartGroupIDs = $priorMailingIDs = [];
122 $dao = CRM_Utils_SQL_Select
::from('civicrm_mailing_group')
123 ->select('GROUP_CONCAT(DISTINCT entity_id SEPARATOR ",") as group_ids, group_type, entity_table')
124 ->where('mailing_id = #mailing_id AND entity_table RLIKE "^civicrm_(group.*|mailing)$" ')
125 ->groupBy(['group_type', 'entity_table'])
126 ->param('!groupTableName', CRM_Contact_BAO_Group
::getTableName())
127 ->param('#mailing_id', $mailingID)
129 while ($dao->fetch()) {
130 if ($dao->entity_table
== 'civicrm_mailing') {
131 $priorMailingIDs[$dao->group_type
] = explode(',', $dao->group_ids
);
134 $recipientsGroup[$dao->group_type
] = empty($recipientsGroup[$dao->group_type
]) ?
explode(',', $dao->group_ids
) : array_merge($recipientsGroup[$dao->group_type
], explode(',', $dao->group_ids
));
138 // there is no need to proceed further if no mailing group is selected to include recipients,
139 // but before return clear the mailing recipients populated earlier since as per current params no group is selected
140 if (empty($recipientsGroup['Include']) && empty($priorMailingIDs['Include'])) {
141 CRM_Core_DAO
::executeQuery(" DELETE FROM civicrm_mailing_recipients WHERE mailing_id = %1 ", [
150 list($location_filter, $order_by) = self
::getLocationFilterAndOrderBy($mailingObj->email_selection_method
, $mailingObj->location_type_id
);
152 // get all the saved searches AND hierarchical groups
153 // and load them in the cache
154 foreach ($recipientsGroup as $groupType => $groupIDs) {
155 $groupDAO = CRM_Utils_SQL_Select
::from('civicrm_group')
156 ->where('id IN (#groupIDs)')
157 ->where('saved_search_id != 0 OR saved_search_id IS NOT NULL OR children IS NOT NULL')
158 ->param('#groupIDs', $groupIDs)
160 while ($groupDAO->fetch()) {
161 // hidden smart groups always have a cache date and there is no other way
162 // we can rebuilt the contact list from UI so consider such smart group
163 if ($groupDAO->cache_date
== NULL ||
$groupDAO->is_hidden
) {
164 CRM_Contact_BAO_GroupContactCache
::load($groupDAO);
166 if ($groupType == 'Include') {
167 $includeSmartGroupIDs[] = $groupDAO->id
;
169 elseif ($groupType == 'Exclude') {
170 $excludeSmartGroupIDs[] = $groupDAO->id
;
172 //NOTE: Do nothing for base
176 // Create a temp table for contact exclusion.
177 $excludeTempTable = CRM_Utils_SQL_TempTable
::build()->setCategory('exrecipient')->setMemory()->createWithColumns('contact_id int primary key');
178 $excludeTempTablename = $excludeTempTable->getName();
179 // populate exclude temp-table with recipients to be excluded from the list
180 // on basis of selected recipients groups and/or previous mailing
181 if (!empty($recipientsGroup['Exclude'])) {
182 CRM_Utils_SQL_Select
::from('civicrm_group_contact')
183 ->select('DISTINCT contact_id')
184 ->where('status = "Added" AND group_id IN (#groups)')
185 ->param('#groups', $recipientsGroup['Exclude'])
186 ->insertInto($excludeTempTablename, ['contact_id'])
189 if (count($excludeSmartGroupIDs)) {
190 CRM_Utils_SQL_Select
::from('civicrm_group_contact_cache')
191 ->select('contact_id')
192 ->where('group_id IN (#groups)')
193 ->param('#groups', $excludeSmartGroupIDs)
194 ->insertIgnoreInto($excludeTempTablename, ['contact_id'])
198 if (!empty($priorMailingIDs['Exclude'])) {
199 CRM_Utils_SQL_Select
::from('civicrm_mailing_recipients')
200 ->select('DISTINCT contact_id')
201 ->where('mailing_id IN (#mailings)')
202 ->param('#mailings', $priorMailingIDs['Exclude'])
203 ->insertIgnoreInto($excludeTempTablename, ['contact_id'])
207 if (!empty($recipientsGroup['Base'])) {
208 CRM_Utils_SQL_Select
::from('civicrm_group_contact')
209 ->select('DISTINCT contact_id')
210 ->where('status = "Removed" AND group_id IN (#groups)')
211 ->param('#groups', $recipientsGroup['Base'])
212 ->insertIgnoreInto($excludeTempTablename, ['contact_id'])
216 $entityColumn = $isSMSmode ?
'phone_id' : 'email_id';
217 $entityTable = $isSMSmode ? CRM_Core_DAO_Phone
::getTableName() : CRM_Core_DAO_Email
::getTableName();
218 // Get all the group contacts we want to include.
219 $includedTempTable = CRM_Utils_SQL_TempTable
::build()->setCategory('inrecipient')->setMemory()->createWithColumns('contact_id int primary key, ' . $entityColumn . ' int');
220 $includedTempTablename = $includedTempTable->getName();
224 'is_deleted' => CRM_Utils_SQL_Select
::fragment()->where("$contact.is_deleted = 0"),
225 'is_opt_out' => CRM_Utils_SQL_Select
::fragment()->where("$contact.is_opt_out = 0"),
226 'is_deceased' => CRM_Utils_SQL_Select
::fragment()->where("$contact.is_deceased <> 1"),
227 'do_not_sms' => CRM_Utils_SQL_Select
::fragment()->where("$contact.do_not_sms = 0"),
228 'location_filter' => CRM_Utils_SQL_Select
::fragment()->where("$entityTable.phone_type_id = " . CRM_Core_PseudoConstant
::getKey('CRM_Core_DAO_Phone', 'phone_type_id', 'Mobile')),
229 'phone_not_null' => CRM_Utils_SQL_Select
::fragment()->where("$entityTable.phone IS NOT NULL"),
230 'phone_not_empty' => CRM_Utils_SQL_Select
::fragment()->where("$entityTable.phone != ''"),
231 'mailing_id' => CRM_Utils_SQL_Select
::fragment()->where("mg.mailing_id = #mailingID"),
232 'temp_contact_null' => CRM_Utils_SQL_Select
::fragment()->where('temp.contact_id IS null'),
233 'order_by' => CRM_Utils_SQL_Select
::fragment()->orderBy("$entityTable.is_primary"),
237 // Criteria to filter recipients that need to be included
239 'is_deleted' => CRM_Utils_SQL_Select
::fragment()->where("$contact.is_deleted = 0"),
240 'do_not_email' => CRM_Utils_SQL_Select
::fragment()->where("$contact.do_not_email = 0"),
241 'is_opt_out' => CRM_Utils_SQL_Select
::fragment()->where("$contact.is_opt_out = 0"),
242 'is_deceased' => CRM_Utils_SQL_Select
::fragment()->where("$contact.is_deceased <> 1"),
243 'location_filter' => CRM_Utils_SQL_Select
::fragment()->where($location_filter),
244 'email_not_null' => CRM_Utils_SQL_Select
::fragment()->where("$entityTable.email IS NOT NULL"),
245 'email_not_empty' => CRM_Utils_SQL_Select
::fragment()->where("$entityTable.email != ''"),
246 'email_not_on_hold' => CRM_Utils_SQL_Select
::fragment()->where("$entityTable.on_hold = 0"),
247 'mailing_id' => CRM_Utils_SQL_Select
::fragment()->where("mg.mailing_id = #mailingID"),
248 'temp_contact_null' => CRM_Utils_SQL_Select
::fragment()->where('temp.contact_id IS NULL'),
249 'order_by' => CRM_Utils_SQL_Select
::fragment()->orderBy($order_by),
253 // Allow user to alter query responsible to fetch mailing recipients before build,
254 // by changing the mail filters identified $params
255 CRM_Utils_Hook
::alterMailingRecipients($mailingObj, $criteria, 'pre');
257 // Get the group contacts, but only those which are not in the
258 // exclusion temp table.
259 if (!empty($recipientsGroup['Include'])) {
260 CRM_Utils_SQL_Select
::from($entityTable)
261 ->select("$contact.id as contact_id, $entityTable.id as $entityColumn")
262 ->join($contact, " INNER JOIN $contact ON $entityTable.contact_id = $contact.id ")
263 ->join('gc', " INNER JOIN civicrm_group_contact gc ON gc.contact_id = $contact.id ")
264 ->join('mg', " INNER JOIN civicrm_mailing_group mg ON gc.group_id = mg.entity_id AND mg.search_id IS NULL ")
265 ->join('temp', " LEFT JOIN $excludeTempTablename temp ON $contact.id = temp.contact_id ")
266 ->where('gc.group_id IN (#groups) AND gc.status = "Added"')
268 ->groupBy(["$contact.id", "$entityTable.id"])
269 ->replaceInto($includedTempTablename, ['contact_id', $entityColumn])
270 ->param('#groups', $recipientsGroup['Include'])
271 ->param('#mailingID', $mailingID)
275 // Get recipients selected in prior mailings
276 if (!empty($priorMailingIDs['Include'])) {
277 CRM_Utils_SQL_Select
::from('civicrm_mailing_recipients')
278 ->select("DISTINCT civicrm_mailing_recipients.contact_id, $entityColumn")
279 ->join('temp', " LEFT JOIN $excludeTempTablename temp ON civicrm_mailing_recipients.contact_id = temp.contact_id ")
280 ->where('mailing_id IN (#mailings)')
281 ->where('temp.contact_id IS NULL')
282 ->param('#mailings', $priorMailingIDs['Include'])
283 ->insertIgnoreInto($includedTempTablename, [
290 if (count($includeSmartGroupIDs)) {
291 $query = CRM_Utils_SQL_Select
::from($contact)
292 ->select("$contact.id as contact_id, $entityTable.id as $entityColumn")
293 ->join($entityTable, " INNER JOIN $entityTable ON $entityTable.contact_id = $contact.id ")
294 ->join('gc', " INNER JOIN civicrm_group_contact_cache gc ON $contact.id = gc.contact_id ")
295 ->join('gcr', " LEFT JOIN civicrm_group_contact gcr ON gc.group_id = gcr.group_id AND gc.contact_id = gcr.contact_id")
296 ->join('mg', " INNER JOIN civicrm_mailing_group mg ON gc.group_id = mg.entity_id AND mg.search_id IS NULL ")
297 ->join('temp', " LEFT JOIN $excludeTempTablename temp ON $contact.id = temp.contact_id ")
298 ->where('gc.group_id IN (#groups)')
299 ->where('gcr.status IS NULL OR gcr.status != "Removed"')
301 ->replaceInto($includedTempTablename, ['contact_id', $entityColumn])
302 ->param('#groups', $includeSmartGroupIDs)
303 ->param('#mailingID', $mailingID)
307 // Construct the filtered search queries.
308 $dao = CRM_Utils_SQL_Select
::from('civicrm_mailing_group')
309 ->select('search_id, search_args, entity_id')
310 ->where('search_id IS NOT NULL AND mailing_id = #mailingID')
311 ->param('#mailingID', $mailingID)
313 while ($dao->fetch()) {
314 $customSQL = CRM_Contact_BAO_SearchCustom
::civiMailSQL($dao->search_id
,
318 $query = "REPLACE INTO {$includedTempTablename} ($entityColumn, contact_id) {$customSQL} ";
319 $mailingGroup->query($query);
322 list($aclFrom, $aclWhere) = CRM_Contact_BAO_Contact_Permission
::cacheClause();
324 // clear all the mailing recipients before populating
325 CRM_Core_DAO
::executeQuery(" DELETE FROM civicrm_mailing_recipients WHERE mailing_id = %1 ", [
332 $selectClause = ['#mailingID', 'i.contact_id', "i.$entityColumn"];
334 $orderBy = ["i.contact_id", "i.$entityColumn"];
336 $query = CRM_Utils_SQL_Select
::from('civicrm_contact contact_a')
337 ->join('i', " INNER JOIN {$includedTempTablename} i ON contact_a.id = i.contact_id ");
338 if (!$isSMSmode && $mailingObj->dedupe_email
) {
339 $orderBy = ["MIN(i.contact_id)", "MIN(i.$entityColumn)"];
340 $query = $query->join('e', " INNER JOIN civicrm_email e ON e.id = i.email_id ")
341 ->groupBy("e.email");
342 if (CRM_Utils_SQL
::supportsFullGroupBy()) {
345 'ANY_VALUE(i.contact_id) contact_id',
346 "ANY_VALUE(i.$entityColumn) $entityColumn",
352 $query = $query->select($selectClause)->orderBy($orderBy);
353 if (!CRM_Utils_System
::isNull($aclFrom)) {
354 $query = $query->join('acl', $aclFrom);
356 if (!CRM_Utils_System
::isNull($aclWhere)) {
357 $query = $query->where($aclWhere);
360 // this mean if dedupe_email AND the mysql 5.7 supports ONLY_FULL_GROUP_BY mode then as
361 // SELECT must contain 'email' column as its used in GROUP BY, so in order to resolve This
362 // here the whole SQL code is wrapped up in FROM table i and not selecting email column for INSERT
363 if ($key = array_search('e.email', $selectClause)) {
364 unset($selectClause[$key]);
365 $sql = $query->toSQL();
366 CRM_Utils_SQL_Select
::from("( $sql ) AS i ")
367 ->select($selectClause)
368 ->insertInto('civicrm_mailing_recipients', ['mailing_id', 'contact_id', $entityColumn])
369 ->param('#mailingID', $mailingID)
373 $query->insertInto('civicrm_mailing_recipients', ['mailing_id', 'contact_id', $entityColumn])
374 ->param('#mailingID', $mailingID)
378 // if we need to add all emails marked bulk, do it as a post filter
379 // on the mailing recipients table
380 if (CRM_Core_BAO_Email
::isMultipleBulkMail()) {
381 self
::addMultipleEmails($mailingID);
384 // Delete the temp table.
385 $mailingGroup->reset();
386 $excludeTempTable->drop();
387 $includedTempTable->drop();
389 CRM_Utils_Hook
::alterMailingRecipients($mailingObj, $criteria, 'post');
393 * Function to retrieve location filter and order by clause later used by SQL query that is used to fetch and include mailing recipients
395 * @param string $email_selection_method
396 * @param int $location_type_id
400 public static function getLocationFilterAndOrderBy($email_selection_method, $location_type_id) {
401 if ($email_selection_method !== 'automatic' && !$location_type_id) {
402 throw new \
CRM_Core_Exception(ts('You have selected an email Selection Method without specifying a Location Type. Please go back and change your recipient settings (using the wrench icon next to "Recipients").'));
404 $email = CRM_Core_DAO_Email
::getTableName();
405 // Note: When determining the ORDER that results are returned, it's
406 // the record that comes last that counts. That's because we are
407 // INSERT'ing INTO a table with a primary id so that last record
408 // over writes any previous record.
409 switch ($email_selection_method) {
410 case 'location-exclude':
411 $location_filter = "($email.location_type_id != $location_type_id)";
412 // If there is more than one email that doesn't match the location,
413 // prefer the one marked is_bulkmail, followed by is_primary.
414 $orderBy = ["$email.is_bulkmail", "$email.is_primary"];
417 case 'location-only':
418 $location_filter = "($email.location_type_id = $location_type_id)";
419 // If there is more than one email of the desired location, prefer
420 // the one marked is_bulkmail, followed by is_primary.
421 $orderBy = ["$email.is_bulkmail", "$email.is_primary"];
424 case 'location-prefer':
425 $location_filter = "($email.is_bulkmail = 1 OR $email.is_primary = 1 OR $email.location_type_id = $location_type_id)";
426 // ORDER BY is more complicated because we have to set an arbitrary
427 // order that prefers the location that we want. We do that using
428 // the FIELD function. For more info, see:
429 // https://dev.mysql.com/doc/refman/5.5/en/string-functions.html#function_field
430 // We assign the location type we want the value "1" by putting it
431 // in the first position after we name the field. All other location
432 // types are left out, so they will be assigned the value 0. That
433 // means, they will all be equally tied for first place, with our
434 // location being last.
436 "FIELD($email.location_type_id, $location_type_id)",
437 "$email.is_bulkmail",
443 // fall through to default
445 $location_filter = "($email.is_bulkmail = 1 OR $email.is_primary = 1)";
446 $orderBy = ["$email.is_bulkmail"];
449 return [$location_filter, $orderBy];
453 * Returns the regex patterns that are used for preparing the text and html templates.
455 * @param bool $onlyHrefs
457 * @return array|string
459 private function getPatterns($onlyHrefs = FALSE) {
463 $protos = '(https?|ftp|mailto)';
465 $gunk = '\{\}/#~:.?+=&;%@!\,\-\|\(\)\*';
467 $any = "{$letters}{$gunk}{$punc}";
469 $pattern = "\\bhref[ ]*=[ ]*([\"'])?(($protos:[$any]+?(?=[$punc]*[^$any]|$)))([\"'])?";
472 $pattern = "\\b($protos:[$any]+?(?=[$punc]*[^$any]|$))";
475 $patterns[] = $pattern;
476 $patterns[] = '\\\\\{\w+\.\w+\\\\\}|\{\{\w+\.\w+\}\}';
477 $patterns[] = '\{\w+\.\w+\}';
479 $patterns = '{' . implode('|', $patterns) . '}imu';
485 * Returns an array that denotes the type of token that we are dealing with
486 * we use the type later on when we are doing a token replacement lookup
488 * @param string $token
489 * The token for which we will be doing adata lookup.
492 * An array that holds the token itself and the type.
493 * the type will tell us which function to use for the data lookup
494 * if we need to do a lookup at all
496 public function &getDataFunc($token) {
497 static $_categories = NULL;
498 static $_categoryString = NULL;
507 CRM_Utils_Hook
::tokens($_categories);
508 $_categoryString = implode('|', array_keys($_categories));
511 $funcStruct = ['type' => NULL, 'token' => $token];
513 if ((preg_match('/^href/i', $token) ||
preg_match('/^http/i', $token))) {
514 // it is a url so we need to check to see if there are any tokens embedded
515 // if so then call this function again to get the token dataFunc
516 // and assign the type 'embedded' so that the data retrieving function
517 // will know what how to handle this token.
518 if (preg_match_all('/(\{\w+\.\w+\})/', $token, $matches)) {
519 $funcStruct['type'] = 'embedded_url';
520 $funcStruct['embed_parts'] = $funcStruct['token'] = [];
521 foreach ($matches[1] as $match) {
522 $preg_token = '/' . preg_quote($match, '/') . '/';
523 $list = preg_split($preg_token, $token, 2);
524 $funcStruct['embed_parts'][] = $list[0];
526 $funcStruct['token'][] = $this->getDataFunc($match);
528 // fixed truncated url, CRM-7113
530 $funcStruct['embed_parts'][] = $token;
534 $funcStruct['type'] = 'url';
537 elseif (preg_match('/^\{(' . $_categoryString . ')\.(\w+)\}$/', $token, $matches)) {
538 $funcStruct['type'] = $matches[1];
539 $funcStruct['token'] = $matches[2];
541 elseif (preg_match('/\\\\\{(\w+\.\w+)\\\\\}|\{\{(\w+\.\w+)\}\}/', $token, $matches)) {
542 // we are an escaped token
543 // so remove the escape chars
544 $unescaped_token = preg_replace('/\{\{|\}\}|\\\\\{|\\\\\}/', '', $matches[0]);
545 $funcStruct['token'] = '{' . $unescaped_token . '}';
551 * Prepares the text and html templates
552 * for generating the emails and returns a copy of the
556 * This is used by CiviMail but will be made redundant by FlexMailer/TokenProcessor.
558 private function getPreparedTemplates() {
559 if (!$this->preparedTemplates
) {
560 $patterns['html'] = $this->getPatterns(TRUE);
561 $patterns['subject'] = $patterns['text'] = $this->getPatterns();
562 $templates = $this->getTemplates();
564 $this->preparedTemplates
= [];
571 if (!isset($templates[$key])) {
577 $split_template = [];
579 $email = $templates[$key];
580 preg_match_all($patterns[$key], $email, $matches, PREG_PATTERN_ORDER
);
581 foreach ($matches[0] as $idx => $token) {
582 $preg_token = '/' . preg_quote($token, '/') . '/im';
583 list($split_template[], $email) = preg_split($preg_token, $email, 2);
584 array_push($tokens, $this->getDataFunc($token));
587 $split_template[] = $email;
589 $this->preparedTemplates
[$key]['template'] = $split_template;
590 $this->preparedTemplates
[$key]['tokens'] = $tokens;
593 return ($this->preparedTemplates
);
597 * Retrieve a ref to an array that holds the email and text templates for this email
598 * assembles the complete template including the header and footer
599 * that the user has uploaded or declared (if they have done that)
602 * reference to an assoc array
604 public function getTemplates() {
605 if (!$this->templates
) {
606 $this->getHeaderFooter();
607 $this->templates
= [];
608 if ($this->body_text ||
!empty($this->header
)) {
610 if (!empty($this->header
->body_text
)) {
611 $template[] = $this->header
->body_text
;
613 elseif (!empty($this->header
->body_html
)) {
614 $template[] = CRM_Utils_String
::htmlToText($this->header
->body_html
);
617 if ($this->body_text
) {
618 $template[] = $this->body_text
;
621 $template[] = CRM_Utils_String
::htmlToText($this->body_html
);
624 if (!empty($this->footer
->body_text
)) {
625 $template[] = $this->footer
->body_text
;
627 elseif (!empty($this->footer
->body_html
)) {
628 $template[] = CRM_Utils_String
::htmlToText($this->footer
->body_html
);
631 $this->templates
['text'] = implode("\n", $template);
634 // To check for an html part strip tags
635 if (trim(strip_tags($this->body_html
, '<img>'))) {
639 $template[] = $this->header
->body_html
;
642 $template[] = $this->body_html
;
645 $template[] = $this->footer
->body_html
;
648 $this->templates
['html'] = implode("\n", $template);
650 // this is where we create a text template from the html template if the text template did not exist
651 // this way we ensure that every recipient will receive an email even if the pref is set to text and the
652 // user uploads an html email only
653 if (empty($this->templates
['text'])) {
654 $this->templates
['text'] = CRM_Utils_String
::htmlToText($this->templates
['html']);
658 if ($this->subject
) {
660 $template[] = $this->subject
;
661 $this->templates
['subject'] = implode("\n", $template);
664 $this->templates
['mailingID'] = $this->id
;
665 $this->templates
['campaign_id'] = $this->campaign_id
;
666 $this->templates
['template_type'] = $this->template_type
;
667 CRM_Utils_Hook
::alterMailContent($this->templates
);
669 return $this->templates
;
674 * Retrieve a ref to an array that holds all of the tokens in the email body
675 * where the keys are the type of token and the values are ordinal arrays
676 * that hold the token names (even repeated tokens) in the order in which
677 * they appear in the body of the email.
679 * note: the real work is done in the _getTokens() function
681 * this function needs to have some sort of a body assigned
682 * either text or html for this to have any meaningful impact
685 * reference to an assoc array
687 public function &getTokens() {
688 if (!$this->tokens
) {
690 $this->tokens
= ['html' => [], 'text' => [], 'subject' => []];
692 if ($this->body_html
) {
693 $this->_getTokens('html');
694 if (!$this->body_text
) {
695 // Since the text template was created from html, use the html tokens.
696 // @see CRM_Mailing_BAO_Mailing::getTemplates()
697 $this->tokens
['text'] = $this->tokens
['html'];
701 if ($this->body_text
) {
702 $this->_getTokens('text');
705 if ($this->subject
) {
706 $this->_getTokens('subject');
710 return $this->tokens
;
714 * Returns the token set for all 3 parts as one set. This allows it to be sent to the
715 * hook in one call and standardizes it across other token workflows
718 * reference to an assoc array
720 public function &getFlattenedTokens() {
721 if (!$this->flattenedTokens
) {
722 $tokens = $this->getTokens();
724 $this->flattenedTokens
= CRM_Utils_Token
::flattenTokens($tokens);
727 return $this->flattenedTokens
;
732 * _getTokens parses out all of the tokens that have been
733 * included in the html and text bodies of the email
734 * we get the tokens and then separate them into an
735 * internal structure named tokens that has the same
736 * form as the static tokens property(?) of the CRM_Utils_Token class.
737 * The difference is that there might be repeated token names as we want the
738 * structures to represent the order in which tokens were found from left to right, top to bottom.
741 * @param string $prop name of the property that holds the text that we want to scan for tokens (html, text).
742 * Name of the property that holds the text that we want to scan for tokens (html, text).
746 private function _getTokens($prop) {
747 $templates = $this->getTemplates();
749 $newTokens = CRM_Utils_Token
::getTokens($templates[$prop]);
751 foreach ($newTokens as $type => $names) {
752 if (!isset($this->tokens
[$prop][$type])) {
753 $this->tokens
[$prop][$type] = [];
755 foreach ($names as $key => $name) {
756 $this->tokens
[$prop][$type][] = $name;
762 * Generate an event queue for a test job.
764 * @param array $testParams
765 * Contains form values.
769 public function getTestRecipients($testParams) {
770 if (!empty($testParams['test_group']) && array_key_exists($testParams['test_group'], CRM_Core_PseudoConstant
::group())) {
771 $contacts = civicrm_api('contact', 'get', [
773 'group' => $testParams['test_group'],
776 'limit' => 100000000000,
780 foreach (array_keys($contacts['values']) as $groupContact) {
782 SELECT civicrm_email.id AS email_id,
783 civicrm_email.is_primary as is_primary,
784 civicrm_email.is_bulkmail as is_bulkmail
786 INNER JOIN civicrm_contact ON civicrm_email.contact_id = civicrm_contact.id
787 WHERE (civicrm_email.is_bulkmail = 1 OR civicrm_email.is_primary = 1)
788 AND civicrm_contact.id = {$groupContact}
789 AND civicrm_contact.do_not_email = 0
790 AND civicrm_contact.is_deceased <> 1
791 AND civicrm_email.on_hold = 0
792 AND civicrm_contact.is_opt_out = 0
793 GROUP BY civicrm_email.id
794 ORDER BY civicrm_email.is_bulkmail DESC
796 $dao = CRM_Core_DAO
::executeQuery($query);
799 'job_id' => $testParams['job_id'],
800 'email_id' => $dao->email_id
,
801 'contact_id' => $groupContact,
803 CRM_Mailing_Event_BAO_Queue
::create($params);
810 * Load this->header and this->footer.
812 private function getHeaderFooter() {
813 if (!$this->header
and $this->header_id
) {
814 $this->header
= new CRM_Mailing_BAO_MailingComponent();
815 $this->header
->id
= $this->header_id
;
816 $this->header
->find(TRUE);
819 if (!$this->footer
and $this->footer_id
) {
820 $this->footer
= new CRM_Mailing_BAO_MailingComponent();
821 $this->footer
->id
= $this->footer_id
;
822 $this->footer
->find(TRUE);
827 * Given and array of headers and a prefix, job ID, event queue ID, and hash,
828 * add a Message-ID header if needed.
830 * i.e. if the global includeMessageId is set and there isn't already a
831 * Message-ID in the array.
832 * The message ID is structured the same way as a verp. However no interpretation
833 * is placed on the values received, so they do not need to follow the verp
836 * @param array $headers
837 * Array of message headers to update, in-out.
838 * @param string $prefix
839 * Prefix for the message ID, use same prefixes as verp.
841 * @param string $job_id
842 * Job ID component of the generated message ID.
843 * @param string $event_queue_id
844 * Event Queue ID component of the generated message ID.
845 * @param string $hash
846 * Hash component of the generated message ID.
850 public static function addMessageIdHeader(&$headers, $prefix, $job_id, $event_queue_id, $hash) {
851 $config = CRM_Core_Config
::singleton();
852 $localpart = CRM_Core_BAO_MailSettings
::defaultLocalpart();
853 $emailDomain = CRM_Core_BAO_MailSettings
::defaultDomain();
854 $includeMessageId = CRM_Core_BAO_MailSettings
::includeMessageId();
856 $fields[] = 'Message-ID';
857 // CRM-17754 check if Resent-Message-id is set also if not add it in when re-laying reply email
858 if ($prefix == 'r') {
859 $fields[] = 'Resent-Message-ID';
861 foreach ($fields as $field) {
862 if ($includeMessageId && (!array_key_exists($field, $headers))) {
863 $headers[$field] = '<' . implode($config->verpSeparator
,
865 $localpart . $prefix,
870 ) . "@{$emailDomain}>";
877 * Static wrapper for getting verp and urls.
880 * ID of the Job associated with this message.
881 * @param int $event_queue_id
882 * ID of the EventQueue.
883 * @param string $hash
884 * Hash of the EventQueue.
885 * @param string $email
886 * Destination address.
889 * (reference) array array ref that hold array refs to the verp info and urls
891 public static function getVerpAndUrls($job_id, $event_queue_id, $hash, $email) {
892 // create a skeleton object and set its properties that are required by getVerpAndUrlsAndHeaders()
893 $config = CRM_Core_Config
::singleton();
894 $bao = new CRM_Mailing_BAO_Mailing();
895 $bao->_domain
= CRM_Core_BAO_Domain
::getDomain();
896 $bao->from_name
= $bao->from_email
= $bao->subject
= '';
898 // use $bao's instance method to get verp and urls
899 list($verp, $urls, $_) = $bao->getVerpAndUrlsAndHeaders($job_id, $event_queue_id, $hash, $email);
900 return [$verp, $urls];
904 * Get verp, urls and headers
907 * ID of the Job associated with this message.
908 * @param int $event_queue_id
909 * ID of the EventQueue.
910 * @param string $hash
911 * Hash of the EventQueue.
912 * @param string $email
913 * Destination address.
915 * @param bool $isForward
918 * array ref that hold array refs to the verp info, urls, and headers
920 public function getVerpAndUrlsAndHeaders($job_id, $event_queue_id, $hash, $email, $isForward = FALSE) {
921 $config = CRM_Core_Config
::singleton();
925 * reply: user replied to mailing
926 * bounce: email address bounced
927 * unsubscribe: contact opts out of all target lists for the mailing
928 * resubscribe: contact opts back into all target lists for the mailing
929 * optOut: contact unsubscribes from the domain
935 'unsubscribe' => 'u',
936 'resubscribe' => 'e',
940 $localpart = CRM_Core_BAO_MailSettings
::defaultLocalpart();
941 $emailDomain = CRM_Core_BAO_MailSettings
::defaultDomain();
942 // Make sure the user configured the site correctly, otherwise you just get "Could not identify any recipients. Perhaps the group is empty?" from the mailing UI
943 if (empty($emailDomain)) {
944 CRM_Core_Error
::debug_log_message('Error setting verp parameters, defaultDomain is NULL. Did you configure the bounce processing account for this domain?');
947 foreach ($verpTokens as $key => $value) {
948 $verp[$key] = implode($config->verpSeparator
,
958 //handle should override VERP address.
962 self
::overrideVerp($job_id)
964 $verp['reply'] = "\"{$this->from_name}\" <{$this->from_email}>";
968 'forward' => CRM_Utils_System
::url('civicrm/mailing/forward', "reset=1&jid={$job_id}&qid={$event_queue_id}&h={$hash}", TRUE, NULL, TRUE, TRUE),
969 'unsubscribeUrl' => CRM_Utils_System
::url('civicrm/mailing/unsubscribe', "reset=1&jid={$job_id}&qid={$event_queue_id}&h={$hash}", TRUE, NULL, TRUE, TRUE),
970 'resubscribeUrl' => CRM_Utils_System
::url('civicrm/mailing/resubscribe', "reset=1&jid={$job_id}&qid={$event_queue_id}&h={$hash}", TRUE, NULL, TRUE, TRUE),
971 'optOutUrl' => CRM_Utils_System
::url('civicrm/mailing/optout', "reset=1&jid={$job_id}&qid={$event_queue_id}&h={$hash}", TRUE, NULL, TRUE, TRUE),
972 'subscribeUrl' => CRM_Utils_System
::url('civicrm/mailing/subscribe', 'reset=1', TRUE, NULL, TRUE, TRUE),
976 'Reply-To' => $verp['reply'],
977 'Return-Path' => $verp['bounce'],
978 'From' => "\"{$this->from_name}\" <{$this->from_email}>",
979 'Subject' => $this->subject
,
980 'List-Unsubscribe' => "<mailto:{$verp['unsubscribe']}>",
982 self
::addMessageIdHeader($headers, 'm', $job_id, $event_queue_id, $hash);
984 $headers['Subject'] = "[Fwd:{$this->subject}]";
986 return [&$verp, &$urls, &$headers];
993 * This is used by CiviMail but will be made redundant by FlexMailer/TokenProcessor.
995 * ID of the Job associated with this message.
996 * @param int $event_queue_id
997 * ID of the EventQueue.
998 * @param string $hash
999 * Hash of the EventQueue.
1000 * @param string $contactId
1001 * ID of the Contact.
1002 * @param string $email
1003 * Destination address.
1004 * @param string $recipient
1005 * To: of the recipient.
1007 * Is this mailing a test?.
1008 * @param $contactDetails
1009 * @param $attachments
1010 * @param bool $isForward
1011 * Is this mailing compose for forward?.
1012 * @param string $fromEmail
1013 * Email address of who is forwardinf it.
1015 * @param null $replyToEmail
1017 * @return Mail_mime The mail object
1019 public function compose(
1020 $job_id, $event_queue_id, $hash, $contactId,
1021 $email, &$recipient, $test,
1022 $contactDetails, &$attachments, $isForward = FALSE,
1023 $fromEmail = NULL, $replyToEmail = NULL
1025 $config = CRM_Core_Config
::singleton();
1028 if ($this->_domain
== NULL) {
1029 $this->_domain
= CRM_Core_BAO_Domain
::getDomain();
1032 list($verp, $urls, $headers) = $this->getVerpAndUrlsAndHeaders(
1040 //set from email who is forwarding it and not original one.
1042 unset($headers['From']);
1043 $headers['From'] = "<{$fromEmail}>";
1046 if ($replyToEmail && ($fromEmail != $replyToEmail)) {
1047 $headers['Reply-To'] = "{$replyToEmail}";
1050 if ($contactDetails) {
1051 $contact = $contactDetails;
1053 elseif ($contactId === 0) {
1056 CRM_Utils_Hook
::tokenValues($contact, [$contactId], $job_id);
1059 $params = [['contact_id', '=', $contactId, 0, 0]];
1060 list($contact) = CRM_Contact_BAO_Query
::apiQuery($params);
1061 // $contact is an array of [ contactID => contactDetails ]
1063 // also call the hook to get contact details
1064 CRM_Utils_Hook
::tokenValues($contact, [$contactId], $job_id);
1066 // Don't send if contact doesn't exist
1067 $contact = reset($contact);
1068 if (!$contact ||
is_a($contact, 'CRM_Core_Error')) {
1069 CRM_Core_Error
::debug_log_message(ts('CiviMail will not send email to a non-existent contact: %1',
1072 // setting this because function is called by reference
1073 //@todo test not calling function by reference
1079 $pTemplates = $this->getPreparedTemplates();
1082 foreach ($pTemplates as $type => $pTemplate) {
1083 $html = $type == 'html';
1084 $pEmails[$type] = [];
1085 $pEmail = &$pEmails[$type];
1086 $template = &$pTemplates[$type]['template'];
1087 $tokens = &$pTemplates[$type]['tokens'];
1089 if (!empty($tokens)) {
1090 foreach ($tokens as $idx => $token) {
1091 $token_data = $this->getTokenData($token, $html, $contact, $verp, $urls, $event_queue_id);
1092 array_push($pEmail, $template[$idx]);
1093 array_push($pEmail, $token_data);
1097 array_push($pEmail, $template[$idx]);
1100 if (isset($template[($idx +
1)])) {
1101 array_push($pEmail, $template[($idx +
1)]);
1106 if (isset($pEmails['html']) && is_array($pEmails['html']) && count($pEmails['html'])) {
1107 $html = &$pEmails['html'];
1111 if (isset($pEmails['text']) && is_array($pEmails['text']) && count($pEmails['text'])) {
1112 $text = &$pEmails['text'];
1115 // push the tracking url on to the html email if necessary
1116 if ($this->open_tracking
&& $html) {
1117 array_push($html, "\n" . '<img src="' . CRM_Utils_System
::externUrl('extern/open', "q=$event_queue_id")
1118 . '" width="1" height="1" alt="" border="0">'
1122 $message = new Mail_mime("\n");
1124 $useSmarty = defined('CIVICRM_MAIL_SMARTY') && CIVICRM_MAIL_SMARTY
;
1126 $smarty = CRM_Core_Smarty
::singleton();
1127 // also add the contact tokens to the template
1128 $smarty->assign_by_ref('contact', $contact);
1131 $mailParams = $headers;
1132 if ($text && ($test ||
$contact['preferred_mail_format'] == 'Text' ||
1133 $contact['preferred_mail_format'] == 'Both' ||
1134 ($contact['preferred_mail_format'] == 'HTML' && !array_key_exists('html', $pEmails))
1137 $textBody = implode('', $text);
1139 $textBody = $smarty->fetch("string:$textBody");
1141 $mailParams['text'] = $textBody;
1144 if ($html && ($test ||
($contact['preferred_mail_format'] == 'HTML' ||
1145 $contact['preferred_mail_format'] == 'Both'
1148 $htmlBody = implode('', $html);
1150 $htmlBody = $smarty->fetch("string:$htmlBody");
1152 $mailParams['html'] = $htmlBody;
1155 if (empty($mailParams['text']) && empty($mailParams['html'])) {
1157 // something went wrong, lets log it and return null (by reference)
1158 CRM_Core_Error
::debug_log_message(ts('CiviMail will not send an empty mail body, Skipping: %1',
1165 $mailParams['attachments'] = $attachments;
1167 $mailParams['Subject'] = $pEmails['subject'] ??
NULL;
1168 if (is_array($mailParams['Subject'])) {
1169 $mailParams['Subject'] = implode('', $mailParams['Subject']);
1172 $mailParams['toName'] = CRM_Utils_Array
::value('display_name',
1175 $mailParams['toEmail'] = $email;
1177 // Add job ID to mailParams for external email delivery service to utilise
1178 $mailParams['job_id'] = $job_id;
1180 CRM_Utils_Hook
::alterMailParams($mailParams, 'civimail');
1182 // CRM-10699 support custom email headers
1183 if (!empty($mailParams['headers'])) {
1184 $headers = array_merge($headers, $mailParams['headers']);
1186 //cycle through mailParams and set headers array
1187 foreach ($mailParams as $paramKey => $paramValue) {
1188 //exclude values not intended for the header
1189 if (!in_array($paramKey, [
1197 $headers[$paramKey] = $paramValue;
1201 if (!empty($mailParams['text'])) {
1202 $message->setTxtBody($mailParams['text']);
1205 if (!empty($mailParams['html'])) {
1206 $message->setHTMLBody($mailParams['html']);
1209 if (!empty($mailParams['attachments'])) {
1210 foreach ($mailParams['attachments'] as $fileID => $attach) {
1211 $message->addAttachment($attach['fullPath'],
1212 $attach['mime_type'],
1213 $attach['cleanName']
1218 //pickup both params from mail params.
1219 $toName = trim($mailParams['toName']);
1220 $toEmail = trim($mailParams['toEmail']);
1221 if ($toName == $toEmail ||
1222 strpos($toName, '@') !== FALSE
1227 $toName = CRM_Utils_Mail
::formatRFC2822Name($toName);
1230 $headers['To'] = "$toName <$toEmail>";
1232 $headers['Precedence'] = 'bulk';
1233 // Will test in the mail processor if the X-VERP is set in the bounced email.
1234 // (As an option to replace real VERP for those that can't set it up)
1235 $headers['X-CiviMail-Bounce'] = $verp['bounce'];
1238 //token replacement of subject
1239 $headers['Subject'] = $mailParams['Subject'];
1241 CRM_Utils_Mail
::setMimeParams($message);
1242 $headers = $message->headers($headers);
1244 //get formatted recipient
1245 $recipient = $headers['To'];
1247 // make sure we unset a lot of stuff
1260 * Get mailing object and replaces subscribeInvite, domain and mailing tokens.
1263 * This is used by CiviMail but will be made redundant by FlexMailer/TokenProcessor.
1264 * @param CRM_Mailing_BAO_Mailing $mailing
1266 public static function tokenReplace(&$mailing) {
1267 $domain = CRM_Core_BAO_Domain
::getDomain();
1269 foreach (['text', 'html'] as $type) {
1270 $tokens = $mailing->getTokens();
1271 if (isset($mailing->templates
[$type])) {
1272 $mailing->templates
[$type] = CRM_Utils_Token
::replaceSubscribeInviteTokens($mailing->templates
[$type]);
1273 $mailing->templates
[$type] = CRM_Utils_Token
::replaceDomainTokens(
1274 $mailing->templates
[$type],
1279 $mailing->templates
[$type] = CRM_Utils_Token
::replaceMailingTokens($mailing->templates
[$type], $mailing, NULL, $tokens[$type]);
1285 * Get data to resolve tokens.
1288 * This is used by CiviMail but will be made redundant by FlexMailer/TokenProcessor.
1290 * @param array $token_a
1292 * Whether to encode the token result for use in HTML email
1293 * @param array $contact
1294 * @param string $verp
1295 * @param array $urls
1296 * @param int $event_queue_id
1298 * @return bool|mixed|null|string
1300 private function getTokenData(&$token_a, $html, &$contact, &$verp, &$urls, $event_queue_id) {
1301 $type = $token_a['type'];
1302 $token = $token_a['token'];
1305 $useSmarty = defined('CIVICRM_MAIL_SMARTY') && CIVICRM_MAIL_SMARTY
;
1307 if ($type == 'embedded_url') {
1309 foreach ($token as $t) {
1310 $embed_data[] = $this->getTokenData($t, $html, $contact, $verp, $urls, $event_queue_id);
1312 $numSlices = count($embed_data);
1314 for ($i = 0; $i < $numSlices; $i++
) {
1315 $embed_url_data = parse_url($embed_data[$i]);
1316 if (!empty($embed_url_data['scheme'])) {
1317 $token_a['embed_parts'][$i] = preg_replace("/href=\"(https*:\/\/)/", "href=\"", $token_a['embed_parts'][$i]);
1319 $url .= "{$token_a['embed_parts'][$i]}{$embed_data[$i]}";
1321 if (isset($token_a['embed_parts'][$numSlices])) {
1322 $url .= $token_a['embed_parts'][$numSlices];
1324 // add trailing quote since we've gobbled it up in a previous regex
1325 // function getPatterns, line 431
1326 if (preg_match("/^href[ ]*=[ ]*'.*[^']$/", $url)) {
1329 elseif (preg_match('/^href[ ]*=[ ]*".*[^"]$/', $url)) {
1333 // CRM-20206 Fix ampersand encoding in plain text emails
1335 $data = CRM_Utils_String
::unstupifyUrl($data);
1338 elseif ($type == 'url') {
1339 if ($this->url_tracking
&& !empty($this->id
)) {
1340 // ensure that Google CSS and any .css files are not tracked.
1341 if (!(strpos($token, 'css?family') ||
strpos($token, '.css'))) {
1342 $data = CRM_Mailing_BAO_TrackableURL
::getTrackerURL($token, $this->id
, $event_queue_id);
1343 if (!empty($html)) {
1344 $data = htmlentities($data, ENT_NOQUOTES
);
1352 elseif ($type == 'contact') {
1353 $data = CRM_Utils_Token
::getContactTokenReplacement($token, $contact, FALSE, FALSE, $useSmarty);
1355 elseif ($type == 'action') {
1356 $data = CRM_Utils_Token
::getActionTokenReplacement($token, $verp, $urls, $html);
1358 elseif ($type == 'domain') {
1359 $domain = CRM_Core_BAO_Domain
::getDomain();
1360 $data = CRM_Utils_Token
::getDomainTokenReplacement($token, $domain, $html);
1362 elseif ($type == 'mailing') {
1363 if ($token == 'name') {
1364 $data = $this->name
;
1366 elseif ($token == 'group') {
1367 $groups = $this->getGroupNames();
1368 $data = implode(', ', $groups);
1372 $data = $contact["{$type}.{$token}"] ??
NULL;
1378 * Return a list of group names for this mailing. Does not work with
1379 * prior-mailing targets.
1382 * Names of groups receiving this mailing
1384 public function &getGroupNames() {
1385 if (!isset($this->id
)) {
1388 $mg = new CRM_Mailing_DAO_MailingGroup();
1389 $mgtable = CRM_Mailing_DAO_MailingGroup
::getTableName();
1390 $group = CRM_Contact_BAO_Group
::getTableName();
1392 $mg->query("SELECT $group.title as name FROM $mgtable
1393 INNER JOIN $group ON $mgtable.entity_id = $group.id
1394 WHERE $mgtable.mailing_id = {$this->id}
1395 AND $mgtable.entity_table = '$group'
1396 AND $mgtable.group_type = 'Include'
1397 ORDER BY $group.name");
1400 while ($mg->fetch()) {
1401 $groups[] = $mg->name
;
1409 * @param array $params
1410 * Reference array contains the values submitted by the form.
1412 * Reference array contains the id.
1415 * @return CRM_Mailing_DAO_Mailing
1417 public static function add(&$params, $ids = []) {
1418 $id = $params['id'] ??
$ids['mailing_id'] ??
NULL;
1420 if (empty($params['id']) && !empty($ids)) {
1421 CRM_Core_Error
::deprecatedWarning('Parameter $ids is no longer used by Mailing::add. Use the api or just pass $params');
1425 CRM_Utils_Hook
::pre('edit', 'Mailing', $id, $params);
1428 CRM_Utils_Hook
::pre('create', 'Mailing', NULL, $params);
1431 $mailing = new static();
1434 $mailing->find(TRUE);
1436 $mailing->domain_id
= CRM_Utils_Array
::value('domain_id', $params, CRM_Core_Config
::domainID());
1438 if (((!$id && empty($params['replyto_email'])) ||
!isset($params['replyto_email'])) &&
1439 isset($params['from_email'])
1441 $params['replyto_email'] = $params['from_email'];
1443 $mailing->copyValues($params);
1445 // CRM-20892 Unset Modifed Date here so that MySQL can correctly set an updated modfied date.
1446 unset($mailing->modified_date
);
1447 $result = $mailing->save();
1449 // CRM-20892 Re find record after saing so we can set the updated modified date in the result.
1450 $mailing->find(TRUE);
1452 if (isset($mailing->modified_date
)) {
1453 $result->modified_date
= $mailing->modified_date
;
1457 CRM_Utils_Hook
::post('edit', 'Mailing', $mailing->id
, $mailing);
1460 CRM_Utils_Hook
::post('create', 'Mailing', $mailing->id
, $mailing);
1467 * Construct a new mailing object, along with job and mailing_group
1468 * objects, from the form values of the create mailing wizard.
1470 * This function is a bit evil. It not only merges $params and saves
1471 * the mailing -- it also schedules the mailing and chooses the recipients.
1472 * Since it merges $params, it's also the only place to correctly trigger
1473 * multi-field validation. It should be broken up.
1475 * In the mean time, use-cases which break under the weight of this
1476 * evil may find reprieve in these extra evil params:
1478 * - _skip_evil_bao_auto_recipients_: bool
1479 * - _skip_evil_bao_auto_schedule_: bool
1480 * - _evil_bao_validator_: string|callable
1482 * </twowrongsmakesaright>
1484 * @params array $params
1487 * @param array $params
1490 * $mailing The new mailing object
1492 * @throws \CRM_Core_Exception
1493 * @throws \CiviCRM_API3_Exception
1495 public static function create(&$params) {
1498 // If it is a mass sms, set url_tracking to false
1499 if (!empty($params['sms_provider_id'])) {
1500 $params['url_tracking'] = 0;
1504 // Do the below only for an insert
1505 // for an update, we should not set the defaults
1506 if (!isset($params['id'])) {
1507 // Retrieve domain email and name for default sender
1508 $domain = civicrm_api(
1513 'current_domain' => 1,
1517 if (isset($domain['from_email'])) {
1518 $domain_email = $domain['from_email'];
1519 $domain_name = $domain['from_name'];
1522 $domain_email = 'info@EXAMPLE.ORG';
1523 $domain_name = 'EXAMPLE.ORG';
1525 if (!isset($params['created_id'])) {
1526 $params['created_id'] = CRM_Core_Session
::getLoggedInContactID();
1529 // load the default config settings for each
1530 // eg reply_id, unsubscribe_id need to use
1531 // correct template IDs here
1532 'override_verp' => TRUE,
1533 'forward_replies' => FALSE,
1534 'open_tracking' => Civi
::settings()->get('open_tracking_default'),
1535 'url_tracking' => Civi
::settings()->get('url_tracking_default'),
1536 'visibility' => 'Public Pages',
1537 'replyto_email' => $domain_email,
1538 'header_id' => CRM_Mailing_PseudoConstant
::defaultComponent('header_id', ''),
1539 'footer_id' => CRM_Mailing_PseudoConstant
::defaultComponent('footer_id', ''),
1540 'from_email' => $domain_email,
1541 'from_name' => $domain_name,
1542 'msg_template_id' => NULL,
1543 'created_id' => $params['created_id'],
1544 'approver_id' => NULL,
1545 'auto_responder' => 0,
1546 'created_date' => date('YmdHis'),
1547 'scheduled_date' => NULL,
1548 'approval_date' => NULL,
1551 // Get the default from email address, if not provided.
1552 if (empty($defaults['from_email'])) {
1553 $defaultAddress = CRM_Core_BAO_Domain
::getNameAndEmail(TRUE, TRUE);
1554 foreach ($defaultAddress as $id => $value) {
1555 if (preg_match('/"(.*)" <(.*)>/', $value, $match)) {
1556 $defaults['from_email'] = $match[2];
1557 $defaults['from_name'] = $match[1];
1562 $params = array_merge($defaults, $params);
1566 * Could check and warn for the following cases:
1568 * - groups OR mailings should be populated.
1569 * - body html OR body text should be populated.
1572 $transaction = new CRM_Core_Transaction();
1574 $mailing = self
::add($params);
1576 if (is_a($mailing, 'CRM_Core_Error')) {
1577 $transaction->rollback();
1580 // update mailings with hash values
1581 CRM_Contact_BAO_Contact_Utils
::generateChecksum($mailing->id
, NULL, NULL, NULL, 'mailing', 16);
1583 $groupTableName = CRM_Contact_BAO_Group
::getTableName();
1585 /* Create the mailing group record */
1586 $mg = new CRM_Mailing_DAO_MailingGroup();
1588 'include' => 'Include',
1589 'exclude' => 'Exclude',
1592 foreach (['groups', 'mailings'] as $entity) {
1593 foreach (['include', 'exclude', 'base'] as $type) {
1594 if (isset($params[$entity][$type])) {
1595 self
::replaceGroups($mailing->id
, $groupTypes[$type], $entity, $params[$entity][$type]);
1600 if (!empty($params['search_id']) && !empty($params['group_id'])) {
1602 $mg->mailing_id
= $mailing->id
;
1603 $mg->entity_table
= $groupTableName;
1604 $mg->entity_id
= $params['group_id'];
1605 $mg->search_id
= $params['search_id'];
1606 $mg->search_args
= $params['search_args'];
1607 $mg->group_type
= 'Include';
1611 // check and attach and files as needed
1612 CRM_Core_BAO_File
::processAttachment($params, 'civicrm_mailing', $mailing->id
);
1614 // If we're going to autosend, then check validity before saving.
1615 if (empty($params['is_completed']) && !empty($params['scheduled_date']) && $params['scheduled_date'] != 'null' && !empty($params['_evil_bao_validator_'])) {
1616 $cb = Civi\Core\Resolver
::singleton()
1617 ->get($params['_evil_bao_validator_']);
1618 $errors = call_user_func($cb, $mailing);
1619 if (!empty($errors)) {
1620 $fields = implode(',', array_keys($errors));
1621 throw new CRM_Core_Exception("Mailing cannot be sent. There are missing or invalid fields ($fields).", 'cannot-send', $errors);
1625 $transaction->commit();
1627 // Create parent job if not yet created.
1628 // Condition on the existence of a scheduled date.
1629 if (!empty($params['scheduled_date']) && $params['scheduled_date'] != 'null' && empty($params['_skip_evil_bao_auto_schedule_'])) {
1630 $job = new CRM_Mailing_BAO_MailingJob();
1631 $job->mailing_id
= $mailing->id
;
1632 // If we are creating a new Completed mailing (e.g. import from another system) set the job to completed.
1633 // Keeping former behaviour when an id is present is precautionary and may warrant reconsideration later.
1634 $job->status
= ((empty($params['is_completed']) ||
!empty($params['id'])) ?
'Scheduled' : 'Complete');
1637 if (!$job->find(TRUE)) {
1638 // Don't schedule job until we populate the recipients.
1639 $job->scheduled_date
= NULL;
1642 // Schedule the job now that it has recipients.
1643 $job->scheduled_date
= $params['scheduled_date'];
1647 // Populate the recipients.
1648 if (empty($params['_skip_evil_bao_auto_recipients_'])) {
1649 self
::getRecipients($mailing->id
);
1657 * This is used by CiviMail but will be made redundant by FlexMailer.
1658 * @param CRM_Mailing_DAO_Mailing $mailing
1659 * The mailing which may or may not be sendable.
1661 * List of error messages.
1663 public static function checkSendable($mailing) {
1665 foreach (['subject', 'name', 'from_name', 'from_email'] as $field) {
1666 if (empty($mailing->{$field})) {
1667 $errors[$field] = ts('Field "%1" is required.', [
1672 if (empty($mailing->body_html
) && empty($mailing->body_text
)) {
1673 $errors['body'] = ts('Field "body_html" or "body_text" is required.');
1676 if (!Civi
::settings()->get('disable_mandatory_tokens_check')) {
1677 $header = $mailing->header_id
&& $mailing->header_id
!= 'null' ? CRM_Mailing_BAO_MailingComponent
::findById($mailing->header_id
) : NULL;
1678 $footer = $mailing->footer_id
&& $mailing->footer_id
!= 'null' ? CRM_Mailing_BAO_MailingComponent
::findById($mailing->footer_id
) : NULL;
1679 foreach (['body_html', 'body_text'] as $field) {
1680 if (empty($mailing->{$field})) {
1683 $str = ($header ?
$header->{$field} : '') . $mailing->{$field} . ($footer ?
$footer->{$field} : '');
1684 $err = CRM_Utils_Token
::requiredTokens($str);
1685 if ($err !== TRUE) {
1686 foreach ($err as $token => $desc) {
1687 $errors["{$field}:{$token}"] = ts('This message is missing a required token - {%1}: %2',
1688 [1 => $token, 2 => $desc]
1699 * Replace the list of recipients on a given mailing.
1701 * @param int $mailingId
1702 * @param string $type
1703 * 'include' or 'exclude'.
1704 * @param string $entity
1705 * 'groups' or 'mailings'.
1706 * @param array $entityIds
1707 * @throws CiviCRM_API3_Exception
1709 public static function replaceGroups($mailingId, $type, $entity, $entityIds) {
1711 foreach ($entityIds as $entityId) {
1712 $values[] = ['entity_id' => $entityId];
1714 civicrm_api3('mailing_group', 'replace', [
1715 'mailing_id' => $mailingId,
1716 'group_type' => $type,
1717 'entity_table' => ($entity == 'groups') ? CRM_Contact_BAO_Group
::getTableName() : CRM_Mailing_BAO_Mailing
::getTableName(),
1718 'values' => $values,
1723 * Get hash value of the mailing.
1727 * @return null|string
1729 public static function getMailingHash($id) {
1731 if (Civi
::settings()->get('hash_mailing_url') && !empty($id)) {
1732 $hash = CRM_Core_DAO
::getFieldValue('CRM_Mailing_BAO_Mailing', $id, 'hash', 'id');
1738 * Generate a report. Fetch event count information, mailing data, and job
1742 * The mailing id to report.
1743 * @param bool $skipDetails
1744 * Whether return all detailed report.
1746 * @param bool $isSMS
1749 * Associative array of reporting data
1751 public static function &report($id, $skipDetails = FALSE, $isSMS = FALSE) {
1752 $mailing_id = CRM_Utils_Type
::escape($id, 'Integer');
1754 $mailing = new CRM_Mailing_BAO_Mailing();
1757 'mailing' => self
::getTableName(),
1758 'mailing_group' => CRM_Mailing_DAO_MailingGroup
::getTableName(),
1759 'group' => CRM_Contact_BAO_Group
::getTableName(),
1760 'job' => CRM_Mailing_BAO_MailingJob
::getTableName(),
1761 'queue' => CRM_Mailing_Event_BAO_Queue
::getTableName(),
1762 'delivered' => CRM_Mailing_Event_BAO_Delivered
::getTableName(),
1763 'opened' => CRM_Mailing_Event_BAO_Opened
::getTableName(),
1764 'reply' => CRM_Mailing_Event_BAO_Reply
::getTableName(),
1765 'unsubscribe' => CRM_Mailing_Event_BAO_Unsubscribe
::getTableName(),
1766 'bounce' => CRM_Mailing_Event_BAO_Bounce
::getTableName(),
1767 'forward' => CRM_Mailing_Event_BAO_Forward
::getTableName(),
1768 'url' => CRM_Mailing_BAO_TrackableURL
::getTableName(),
1769 'urlopen' => CRM_Mailing_Event_BAO_TrackableURLOpen
::getTableName(),
1770 'component' => CRM_Mailing_BAO_MailingComponent
::getTableName(),
1771 'spool' => CRM_Mailing_BAO_Spool
::getTableName(),
1775 $additionalWhereClause = " AND ";
1777 $additionalWhereClause .= " {$t['mailing']}.sms_provider_id IS NULL ";
1780 $additionalWhereClause .= " {$t['mailing']}.sms_provider_id IS NOT NULL ";
1783 /* Get the mailing info */
1786 SELECT {$t['mailing']}.*
1787 FROM {$t['mailing']}
1788 WHERE {$t['mailing']}.id = $mailing_id {$additionalWhereClause}");
1792 $report['mailing'] = [];
1793 foreach (array_keys(self
::fields()) as $field) {
1794 $field = self
::fields()[$field]['name'];
1795 $report['mailing'][$field] = $mailing->$field;
1799 if ($campaignId = CRM_Utils_Array
::value('campaign_id', $report['mailing'])) {
1800 $campaigns = CRM_Campaign_BAO_Campaign
::getCampaigns($campaignId);
1801 $report['mailing']['campaign'] = $campaigns[$campaignId];
1804 //mailing report is called by activity
1805 //we dont need all detail report
1810 /* Get the component info */
1815 'header' => ts('Header'),
1816 'footer' => ts('Footer'),
1817 'reply' => ts('Reply'),
1818 'optout' => ts('Opt-Out'),
1819 'resubscribe' => ts('Resubscribe'),
1820 'unsubscribe' => ts('Unsubscribe'),
1822 foreach (array_keys($components) as $type) {
1823 $query[] = "SELECT {$t['component']}.name as name,
1825 {$t['component']}.id as id
1826 FROM {$t['component']}
1827 INNER JOIN {$t['mailing']}
1828 ON {$t['mailing']}.{$type}_id =
1829 {$t['component']}.id
1830 WHERE {$t['mailing']}.id = $mailing_id";
1832 $q = '(' . implode(') UNION (', $query) . ')';
1833 $mailing->query($q);
1835 $report['component'] = [];
1836 while ($mailing->fetch()) {
1837 $report['component'][] = [
1838 'type' => $components[$mailing->type
],
1839 'name' => $mailing->name
,
1840 'link' => CRM_Utils_System
::url('civicrm/mailing/component', "reset=1&action=update&id={$mailing->id}"),
1844 /* Get the recipient group info */
1847 SELECT {$t['mailing_group']}.group_type as group_type,
1848 {$t['group']}.id as group_id,
1849 {$t['group']}.title as group_title,
1850 {$t['group']}.is_hidden as group_hidden,
1851 {$t['mailing']}.id as mailing_id,
1852 {$t['mailing']}.name as mailing_name
1853 FROM {$t['mailing_group']}
1854 LEFT JOIN {$t['group']}
1855 ON {$t['mailing_group']}.entity_id = {$t['group']}.id
1856 AND {$t['mailing_group']}.entity_table =
1858 LEFT JOIN {$t['mailing']}
1859 ON {$t['mailing_group']}.entity_id =
1861 AND {$t['mailing_group']}.entity_table =
1864 WHERE {$t['mailing_group']}.mailing_id = $mailing_id
1867 $report['group'] = ['include' => [], 'exclude' => [], 'base' => []];
1868 while ($mailing->fetch()) {
1870 if (isset($mailing->group_id
)) {
1871 $row['id'] = $mailing->group_id
;
1872 $row['name'] = $mailing->group_title
;
1873 $row['link'] = CRM_Utils_System
::url('civicrm/group/search',
1874 "reset=1&force=1&context=smog&gid={$row['id']}"
1878 $row['id'] = $mailing->mailing_id
;
1879 $row['name'] = $mailing->mailing_name
;
1880 $row['mailing'] = TRUE;
1881 $row['link'] = CRM_Utils_System
::url('civicrm/mailing/report',
1886 /* Rename hidden groups */
1888 if ($mailing->group_hidden
== 1) {
1889 $row['name'] = "Search Results";
1892 if ($mailing->group_type
== 'Include') {
1893 $report['group']['include'][] = $row;
1895 elseif ($mailing->group_type
== 'Base') {
1896 $report['group']['base'][] = $row;
1899 $report['group']['exclude'][] = $row;
1903 /* Get the event totals, grouped by job (retries) */
1906 SELECT {$t['job']}.*,
1907 COUNT(DISTINCT {$t['queue']}.id) as queue,
1908 COUNT(DISTINCT {$t['delivered']}.id) as delivered,
1909 COUNT(DISTINCT {$t['reply']}.id) as reply,
1910 COUNT(DISTINCT {$t['forward']}.id) as forward,
1911 COUNT(DISTINCT {$t['bounce']}.id) as bounce,
1912 COUNT(DISTINCT {$t['urlopen']}.id) as url,
1913 COUNT(DISTINCT {$t['spool']}.id) as spool
1915 LEFT JOIN {$t['queue']}
1916 ON {$t['queue']}.job_id = {$t['job']}.id
1917 LEFT JOIN {$t['reply']}
1918 ON {$t['reply']}.event_queue_id = {$t['queue']}.id
1919 LEFT JOIN {$t['forward']}
1920 ON {$t['forward']}.event_queue_id = {$t['queue']}.id
1921 LEFT JOIN {$t['bounce']}
1922 ON {$t['bounce']}.event_queue_id = {$t['queue']}.id
1923 LEFT JOIN {$t['delivered']}
1924 ON {$t['delivered']}.event_queue_id = {$t['queue']}.id
1925 AND {$t['bounce']}.id IS null
1926 LEFT JOIN {$t['urlopen']}
1927 ON {$t['urlopen']}.event_queue_id = {$t['queue']}.id
1928 LEFT JOIN {$t['spool']}
1929 ON {$t['spool']}.job_id = {$t['job']}.id
1930 WHERE {$t['job']}.mailing_id = $mailing_id
1931 AND {$t['job']}.is_test = 0
1932 GROUP BY {$t['job']}.id");
1934 $report['jobs'] = [];
1935 $report['event_totals'] = [];
1936 $path = 'civicrm/mailing/report/event';
1951 // initialize various counters
1952 foreach ($elements as $field) {
1953 $report['event_totals'][$field] = 0;
1956 while ($mailing->fetch()) {
1958 foreach ($elements as $field) {
1959 if (isset($mailing->$field)) {
1960 $row[$field] = $mailing->$field;
1961 $report['event_totals'][$field] +
= $mailing->$field;
1965 // compute open total separately to discount duplicates
1967 $row['opened'] = CRM_Mailing_Event_BAO_Opened
::getTotalCount($mailing_id, $mailing->id
, TRUE);
1968 $report['event_totals']['opened'] +
= $row['opened'];
1969 $row['total_opened'] = CRM_Mailing_Event_BAO_Opened
::getTotalCount($mailing_id, $mailing->id
);
1970 $report['event_totals']['total_opened'] +
= $row['total_opened'];
1972 // compute unsub total separately to discount duplicates
1974 $row['unsubscribe'] = CRM_Mailing_Event_BAO_Unsubscribe
::getTotalCount($mailing_id, $mailing->id
, TRUE, TRUE);
1975 $report['event_totals']['unsubscribe'] +
= $row['unsubscribe'];
1977 $row['optout'] = CRM_Mailing_Event_BAO_Unsubscribe
::getTotalCount($mailing_id, $mailing->id
, TRUE, FALSE);
1978 $report['event_totals']['optout'] +
= $row['optout'];
1980 foreach (array_keys(CRM_Mailing_BAO_MailingJob
::fields()) as $field) {
1981 // Get the field name from the MailingJob fields as that will not have any prefixing.
1983 $field = CRM_Mailing_BAO_MailingJob
::fields()[$field]['name'];
1984 $row[$field] = $mailing->$field;
1987 if ($mailing->queue
) {
1988 $row['delivered_rate'] = (100.0 * $mailing->delivered
) / $mailing->queue
;
1989 $row['bounce_rate'] = (100.0 * $mailing->bounce
) / $mailing->queue
;
1990 $row['unsubscribe_rate'] = (100.0 * $row['unsubscribe']) / $mailing->queue
;
1991 $row['optout_rate'] = (100.0 * $row['optout']) / $mailing->queue
;
1992 $row['opened_rate'] = $mailing->delivered ?
(($row['opened'] / $mailing->delivered
) * 100.0) : 0;
1993 $row['clickthrough_rate'] = $mailing->delivered ?
(($mailing->url
/ $mailing->delivered
) * 100.0) : 0;
1996 $row['delivered_rate'] = 0;
1997 $row['bounce_rate'] = 0;
1998 $row['unsubscribe_rate'] = 0;
1999 $row['optout_rate'] = 0;
2000 $row['opened_rate'] = 0;
2001 $row['clickthrough_rate'] = 0;
2004 $arg = "reset=1&mid=$mailing_id&jid={$mailing->id}";
2006 'clicks' => CRM_Utils_System
::url($path, "$arg&event=click"),
2007 'queue' => CRM_Utils_System
::url($path, "$arg&event=queue"),
2008 'delivered' => CRM_Utils_System
::url($path, "$arg&event=delivered"),
2009 'bounce' => CRM_Utils_System
::url($path, "$arg&event=bounce"),
2010 'unsubscribe' => CRM_Utils_System
::url($path, "$arg&event=unsubscribe"),
2011 'forward' => CRM_Utils_System
::url($path, "$arg&event=forward"),
2012 'reply' => CRM_Utils_System
::url($path, "$arg&event=reply"),
2013 'opened' => CRM_Utils_System
::url($path, "$arg&event=opened"),
2016 foreach (['scheduled_date', 'start_date', 'end_date'] as $key) {
2017 $row[$key] = CRM_Utils_Date
::customFormat($row[$key]);
2019 $report['jobs'][] = $row;
2022 $newTableSize = CRM_Mailing_BAO_Recipients
::mailingSize($mailing_id);
2024 // we need to do this for backward compatibility, since old mailings did not
2025 // use the mailing_recipients table
2026 if ($newTableSize > 0) {
2027 $report['event_totals']['queue'] = $newTableSize;
2030 $report['event_totals']['queue'] = self
::getRecipientsCount($mailing_id);
2033 if (!empty($report['event_totals']['queue'])) {
2034 $report['event_totals']['delivered_rate'] = (100.0 * $report['event_totals']['delivered']) / $report['event_totals']['queue'];
2035 $report['event_totals']['bounce_rate'] = (100.0 * $report['event_totals']['bounce']) / $report['event_totals']['queue'];
2036 $report['event_totals']['unsubscribe_rate'] = (100.0 * $report['event_totals']['unsubscribe']) / $report['event_totals']['queue'];
2037 $report['event_totals']['optout_rate'] = (100.0 * $report['event_totals']['optout']) / $report['event_totals']['queue'];
2038 $report['event_totals']['opened_rate'] = !empty($report['event_totals']['delivered']) ?
(($report['event_totals']['opened'] / $report['event_totals']['delivered']) * 100.0) : 0;
2039 $report['event_totals']['clickthrough_rate'] = !empty($report['event_totals']['delivered']) ?
(($report['event_totals']['url'] / $report['event_totals']['delivered']) * 100.0) : 0;
2042 $report['event_totals']['delivered_rate'] = 0;
2043 $report['event_totals']['bounce_rate'] = 0;
2044 $report['event_totals']['unsubscribe_rate'] = 0;
2045 $report['event_totals']['optout_rate'] = 0;
2046 $report['event_totals']['opened_rate'] = 0;
2047 $report['event_totals']['clickthrough_rate'] = 0;
2050 /* Get the click-through totals, grouped by URL */
2053 SELECT {$t['url']}.url,
2055 COUNT({$t['urlopen']}.id) as clicks,
2056 COUNT(DISTINCT {$t['queue']}.id) as unique_clicks
2058 LEFT JOIN {$t['urlopen']}
2059 ON {$t['urlopen']}.trackable_url_id = {$t['url']}.id
2060 LEFT JOIN {$t['queue']}
2061 ON {$t['urlopen']}.event_queue_id = {$t['queue']}.id
2062 LEFT JOIN {$t['job']}
2063 ON {$t['queue']}.job_id = {$t['job']}.id
2064 WHERE {$t['url']}.mailing_id = $mailing_id
2065 AND {$t['job']}.is_test = 0
2066 GROUP BY {$t['url']}.id
2067 ORDER BY unique_clicks DESC");
2069 $report['click_through'] = [];
2071 while ($mailing->fetch()) {
2072 $report['click_through'][] = [
2073 'url' => $mailing->url
,
2074 'link' => CRM_Utils_System
::url($path, "reset=1&event=click&mid=$mailing_id&uid={$mailing->id}"),
2075 'link_unique' => CRM_Utils_System
::url($path, "reset=1&event=click&mid=$mailing_id&uid={$mailing->id}&distinct=1"),
2076 'clicks' => $mailing->clicks
,
2077 'unique' => $mailing->unique_clicks
,
2078 'rate' => !empty($report['event_totals']['delivered']) ?
(100.0 * $mailing->unique_clicks
) / $report['event_totals']['delivered'] : 0,
2079 'report' => CRM_Report_Utils_Report
::getNextUrl('mailing/clicks', "reset=1&mailing_id_value={$mailing_id}&url_value={$mailing->url}", FALSE, TRUE),
2083 $arg = "reset=1&mid=$mailing_id";
2084 $report['event_totals']['links'] = [
2085 'clicks' => CRM_Utils_System
::url($path, "$arg&event=click"),
2086 'clicks_unique' => CRM_Utils_System
::url($path, "$arg&event=click&distinct=1"),
2087 'queue' => CRM_Utils_System
::url($path, "$arg&event=queue"),
2088 'delivered' => CRM_Utils_System
::url($path, "$arg&event=delivered"),
2089 'bounce' => CRM_Utils_System
::url($path, "$arg&event=bounce"),
2090 'unsubscribe' => CRM_Utils_System
::url($path, "$arg&event=unsubscribe"),
2091 'optout' => CRM_Utils_System
::url($path, "$arg&event=optout"),
2092 'forward' => CRM_Utils_System
::url($path, "$arg&event=forward"),
2093 'reply' => CRM_Utils_System
::url($path, "$arg&event=reply"),
2094 'opened' => CRM_Utils_System
::url($path, "$arg&event=opened"),
2097 $actionLinks = [CRM_Core_Action
::VIEW
=> ['name' => ts('Report')]];
2098 $actionLinks[CRM_Core_Action
::ADVANCED
] = [
2099 'name' => ts('Advanced Search'),
2100 'url' => 'civicrm/contact/search/advanced',
2102 $action = array_sum(array_keys($actionLinks));
2104 $report['event_totals']['actionlinks'] = [];
2105 foreach (['clicks', 'clicks_unique', 'queue', 'delivered', 'bounce', 'unsubscribe', 'forward', 'reply', 'opened', 'opened_unique', 'optout'] as $key) {
2106 $url = 'mailing/detail';
2107 $reportFilter = "reset=1&mailing_id_value={$mailing_id}";
2108 $searchFilter = "force=1&mailing_id=%%mid%%";
2111 $reportFilter .= "&delivery_status_value=successful";
2112 $searchFilter .= "&mailing_delivery_status=Y";
2116 $url = "mailing/bounce";
2117 $searchFilter .= "&mailing_delivery_status=N";
2121 $reportFilter .= "&is_forwarded_value=1";
2122 $searchFilter .= "&mailing_forward=1";
2126 $reportFilter .= "&is_replied_value=1";
2127 $searchFilter .= "&mailing_reply_status=Y";
2131 $reportFilter .= "&is_unsubscribed_value=1";
2132 $searchFilter .= "&mailing_unsubscribe=1";
2136 $reportFilter .= "&is_optout_value=1";
2137 $searchFilter .= "&mailing_optout=1";
2141 // do not use group by clause in report, because same report used for total and unique open
2142 $reportFilter .= "&distinct=0";
2143 case 'opened_unique':
2144 $url = "mailing/opened";
2145 $searchFilter .= "&mailing_open_status=Y";
2149 case 'clicks_unique':
2150 $url = "mailing/clicks";
2151 $searchFilter .= "&mailing_click_status=Y";
2154 $actionLinks[CRM_Core_Action
::VIEW
]['url'] = CRM_Report_Utils_Report
::getNextUrl($url, $reportFilter, FALSE, TRUE);
2155 if (array_key_exists(CRM_Core_Action
::ADVANCED
, $actionLinks)) {
2156 $actionLinks[CRM_Core_Action
::ADVANCED
]['qs'] = $searchFilter;
2158 $report['event_totals']['actionlinks'][$key] = CRM_Core_Action
::formLink(
2161 ['mid' => $mailing_id],
2164 'mailing.report.action',
2174 * Get the count of mailings.
2181 public function getCount() {
2183 $this->selectAdd('COUNT(id) as count');
2185 $session = CRM_Core_Session
::singleton();
2188 return $this->count
;
2196 public static function checkPermission($id) {
2201 $mailingIDs = self
::mailingACLIDs();
2202 if ($mailingIDs === TRUE) {
2206 if (!in_array($id, $mailingIDs)) {
2207 throw new CRM_Core_Exception(ts('You do not have permission to access this mailing report'));
2212 * @param null $alias
2216 public static function mailingACL($alias = NULL) {
2217 $mailingACL = " ( 0 ) ";
2219 $mailingIDs = self
::mailingACLIDs();
2220 if ($mailingIDs === TRUE) {
2224 if (!empty($mailingIDs)) {
2225 $mailingIDs = implode(',', $mailingIDs);
2226 $tableName = !$alias ? self
::getTableName() : $alias;
2227 $mailingACL = " $tableName.id IN ( $mailingIDs ) ";
2233 * Returns all the mailings that this user can access. This is dependent on
2234 * all the groups that the user has access to.
2235 * However since most civi installs dont use ACL's we special case the condition
2236 * where the user has access to ALL groups, and hence ALL mailings and return a
2237 * value of TRUE (to avoid the downstream where clause with a list of mailing list IDs
2239 * @return bool|array
2240 * TRUE if the user has access to all mailings, else array of mailing IDs (possibly empty).
2242 public static function mailingACLIDs() {
2244 // optimize common case where admin has access
2247 CRM_Core_Permission
::check('view all contacts') ||
2248 CRM_Core_Permission
::check('edit all contacts')
2255 // get all the groups that this user can access
2256 // if they dont have universal access
2257 $groupNames = civicrm_api3('Group', 'get', [
2258 'check_permissions' => TRUE,
2259 'return' => ['title', 'id'],
2260 'options' => ['limit' => 0],
2262 foreach ($groupNames['values'] as $group) {
2263 $groups[$group['id']] = $group['title'];
2265 if (!empty($groups)) {
2266 $groupIDs = implode(',', array_keys($groups));
2267 $domain_id = CRM_Core_Config
::domainID();
2269 // get all the mailings that are in this subset of groups
2271 SELECT DISTINCT( m.id ) as id
2272 FROM civicrm_mailing m
2273 LEFT JOIN civicrm_mailing_group g ON g.mailing_id = m.id
2274 WHERE ( ( g.entity_table like 'civicrm_group%' AND g.entity_id IN ( $groupIDs ) )
2275 OR ( g.entity_table IS NULL AND g.entity_id IS NULL AND m.domain_id = $domain_id ) )
2277 $dao = CRM_Core_DAO
::executeQuery($query);
2280 while ($dao->fetch()) {
2281 $mailingIDs[] = $dao->id
;
2283 //CRM-18181 Get all mailings that use the mailings found earlier as receipients
2284 if (!empty($mailingIDs)) {
2285 $mailings = implode(',', $mailingIDs);
2287 SELECT DISTINCT ( m.id ) as id
2288 FROM civicrm_mailing m
2289 LEFT JOIN civicrm_mailing_group g ON g.mailing_id = m.id
2290 WHERE g.entity_table like 'civicrm_mailing%' AND g.entity_id IN ($mailings)";
2291 $mailingDao = CRM_Core_DAO
::executeQuery($mailingQuery);
2292 while ($mailingDao->fetch()) {
2293 $mailingIDs[] = $mailingDao->id
;
2302 * Get the rows for a browse operation.
2304 * @param int $offset
2305 * The row number to start from.
2306 * @param int $rowCount
2307 * The nmber of rows to return.
2308 * @param string $sort
2309 * The sql string that describes the sort order.
2311 * @param null $additionalClause
2312 * @param array $additionalParams
2317 public function &getRows($offset, $rowCount, $sort, $additionalClause = NULL, $additionalParams = NULL) {
2318 $mailing = self
::getTableName();
2319 $job = CRM_Mailing_BAO_MailingJob
::getTableName();
2320 $group = CRM_Mailing_DAO_MailingGroup
::getTableName();
2321 $session = CRM_Core_Session
::singleton();
2323 $mailingACL = self
::mailingACL();
2325 //get all campaigns.
2326 $allCampaigns = CRM_Campaign_BAO_Campaign
::getCampaigns(NULL, NULL, FALSE, FALSE, FALSE, TRUE);
2331 "$mailing.approval_status_id",
2332 "createdContact.sort_name as created_by",
2333 "scheduledContact.sort_name as scheduled_by",
2334 "$mailing.created_id as created_id",
2335 "$mailing.scheduled_id as scheduled_id",
2336 "$mailing.is_archived as archived",
2337 "$mailing.created_date as created_date",
2339 "$mailing.sms_provider_id as sms_provider_id",
2340 "$mailing.language",
2343 // we only care about parent jobs, since that holds all the info on
2345 $selectClause = implode(', ', $select);
2346 $groupFromSelect = CRM_Contact_BAO_Query
::getGroupByFromSelectColumns($select, "$mailing.id");
2348 SELECT {$selectClause},
2349 MIN($job.scheduled_date) as scheduled_date,
2350 MIN($job.start_date) as start_date,
2351 MAX($job.end_date) as end_date
2353 LEFT JOIN $job ON ( $job.mailing_id = $mailing.id AND $job.is_test = 0 AND $job.parent_id IS NULL )
2354 LEFT JOIN civicrm_contact createdContact ON ( civicrm_mailing.created_id = createdContact.id )
2355 LEFT JOIN civicrm_contact scheduledContact ON ( civicrm_mailing.scheduled_id = scheduledContact.id )
2356 WHERE $mailingACL $additionalClause";
2358 if (!empty($groupFromSelect)) {
2359 $query .= $groupFromSelect;
2363 $orderBy = trim($sort->orderBy());
2364 if (!empty($orderBy)) {
2365 $query .= " ORDER BY $orderBy";
2370 $offset = CRM_Utils_Type
::escape($offset, 'Int');
2371 $rowCount = CRM_Utils_Type
::escape($rowCount, 'Int');
2373 $query .= " LIMIT $offset, $rowCount ";
2376 if (!$additionalParams) {
2377 $additionalParams = [];
2380 $dao = CRM_Core_DAO
::executeQuery($query, $additionalParams);
2383 while ($dao->fetch()) {
2386 'name' => $dao->name
,
2387 'status' => $dao->status ?
$dao->status
: 'Not scheduled',
2388 'created_date' => CRM_Utils_Date
::customFormat($dao->created_date
),
2389 'scheduled' => CRM_Utils_Date
::customFormat($dao->scheduled_date
),
2390 'scheduled_iso' => $dao->scheduled_date
,
2391 'start' => CRM_Utils_Date
::customFormat($dao->start_date
),
2392 'end' => CRM_Utils_Date
::customFormat($dao->end_date
),
2393 'created_by' => $dao->created_by
,
2394 'scheduled_by' => $dao->scheduled_by
,
2395 'created_id' => $dao->created_id
,
2396 'scheduled_id' => $dao->scheduled_id
,
2397 'archived' => $dao->archived
,
2398 'approval_status_id' => $dao->approval_status_id
,
2399 'campaign_id' => $dao->campaign_id
,
2400 'campaign' => empty($dao->campaign_id
) ?
NULL : $allCampaigns[$dao->campaign_id
],
2401 'sms_provider_id' => $dao->sms_provider_id
,
2402 'language' => $dao->language
,
2409 * Show detail Mailing report.
2415 public static function showEmailDetails($id) {
2416 return CRM_Utils_System
::url('civicrm/mailing/report', "mid=$id");
2420 * Delete Mails and all its associated records.
2423 * Id of the mail to delete.
2427 public static function del($id) {
2429 throw new CRM_Core_Exception(ts('No id passed to mailing del function'));
2432 CRM_Utils_Hook
::pre('delete', 'Mailing', $id);
2434 // delete all file attachments
2435 CRM_Core_BAO_File
::deleteEntityFile('civicrm_mailing',
2439 $dao = new CRM_Mailing_DAO_Mailing();
2443 CRM_Core_Session
::setStatus(ts('Selected mailing has been deleted.'), ts('Deleted'), 'success');
2445 CRM_Utils_Hook
::post('delete', 'Mailing', $id, $dao);
2450 * Use CRM_Mailing_BAO_MailingJob::del($id)
2453 * Id of the Job to delete.
2457 public static function delJob($id) {
2459 throw new CRM_Core_Exception(ts('No id passed to mailing delJob function'));
2462 CRM_Core_Error
::deprecatedWarning('This function is deprecated, use CRM_Mailing_BAO_MailingJob::del instead');
2464 CRM_Mailing_BAO_MailingJob
::del($id);
2469 * This is used by CiviMail but will be made redundant by FlexMailer/TokenProcessor.
2472 public function getReturnProperties() {
2473 $tokens = &$this->getTokens();
2476 if (isset($tokens['html']) &&
2477 isset($tokens['html']['contact'])
2479 $properties = array_merge($properties, $tokens['html']['contact']);
2482 if (isset($tokens['text']) &&
2483 isset($tokens['text']['contact'])
2485 $properties = array_merge($properties, $tokens['text']['contact']);
2488 if (isset($tokens['subject']) &&
2489 isset($tokens['subject']['contact'])
2491 $properties = array_merge($properties, $tokens['subject']['contact']);
2494 $returnProperties = [];
2495 $returnProperties['display_name'] = $returnProperties['contact_id'] = $returnProperties['preferred_mail_format'] = $returnProperties['hash'] = 1;
2497 foreach ($properties as $p) {
2498 $returnProperties[$p] = 1;
2501 return $returnProperties;
2505 * Build the compose mail form.
2507 * @param CRM_Core_Form $form
2511 public static function commonCompose(&$form) {
2515 if (method_exists($form, 'listTokens')) {
2516 $tokens = array_merge($form->listTokens(), $tokens);
2519 //sorted in ascending order tokens by ignoring word case
2520 $form->assign('tokens', CRM_Utils_Token
::formatTokensForDisplay($tokens));
2525 'text_message' => ts('HTML Format'),
2526 'sms_text_message' => ts('SMS Message'),
2528 $modePrefixes = ['Mail' => NULL, 'SMS' => 'SMS'];
2530 $className = CRM_Utils_System
::getClassName($form);
2532 if ($className != 'CRM_SMS_Form_Upload' && $className != 'CRM_Contact_Form_Task_SMS' &&
2533 $className != 'CRM_Contact_Form_Task_SMS'
2535 $form->add('wysiwyg', 'html_message',
2536 strstr($className, 'PDF') ?
ts('Document Body') : ts('HTML Format'),
2540 'onkeyup' => "return verify(this)",
2544 if ($className != 'CRM_Admin_Form_ScheduleReminders') {
2545 unset($modePrefixes['SMS']);
2549 unset($textFields['text_message']);
2550 unset($modePrefixes['Mail']);
2553 //insert message Text by selecting "Select Template option"
2554 foreach ($textFields as $id => $label) {
2556 if ($id == 'sms_text_message') {
2558 $form->assign('max_sms_length', CRM_SMS_Provider
::MAX_SMS_CHAR
);
2560 $form->add('textarea', $id, $label,
2564 'onkeyup' => "return verify(this, '{$prefix}')",
2569 foreach ($modePrefixes as $prefix) {
2570 if ($prefix == 'SMS') {
2571 $templates[$prefix] = CRM_Core_BAO_MessageTemplate
::getMessageTemplates(FALSE, TRUE);
2574 $templates[$prefix] = CRM_Core_BAO_MessageTemplate
::getMessageTemplates(FALSE);
2576 if (!empty($templates[$prefix])) {
2577 $form->assign('templates', TRUE);
2579 $form->add('select', "{$prefix}template", ts('Use Template'),
2580 ['' => ts('- select -')] +
$templates[$prefix], FALSE,
2581 ['onChange' => "selectValue( this.value, '{$prefix}');"]
2584 $form->add('checkbox', "{$prefix}updateTemplate", ts('Update Template'), NULL);
2586 $form->add('checkbox', "{$prefix}saveTemplate", ts('Save As New Template'), NULL, FALSE,
2587 ['onclick' => "showSaveDetails(this, '{$prefix}');"]
2589 $form->add('text', "{$prefix}saveTemplateName", ts('Template Title'));
2592 // I'm not sure this is ever called.
2593 $action = CRM_Utils_Request
::retrieve('action', 'String', $form, FALSE);
2594 if ((CRM_Utils_System
::getClassName($form) == 'CRM_Contact_Form_Task_PDF') &&
2595 $action == CRM_Core_Action
::VIEW
2597 $form->freeze('html_message');
2602 * Get the search based mailing Ids.
2605 * , searched base mailing ids.
2607 public function searchMailingIDs() {
2608 $group = CRM_Mailing_DAO_MailingGroup
::getTableName();
2609 $mailing = self
::getTableName();
2612 SELECT $mailing.id as mailing_id
2613 FROM $mailing, $group
2614 WHERE $group.mailing_id = $mailing.id
2615 AND $group.group_type = 'Base'";
2617 $searchDAO = CRM_Core_DAO
::executeQuery($query);
2619 while ($searchDAO->fetch()) {
2620 $mailingIDs[] = $searchDAO->mailing_id
;
2627 * Get the content/components of mailing based on mailing Id
2629 * @param array $report
2630 * of mailing report.
2633 * Reference of this.
2635 * @param bool $isSMS
2638 * array content/component.
2640 public static function getMailingContent(&$report, &$form, $isSMS = FALSE) {
2641 $htmlHeader = $textHeader = NULL;
2642 $htmlFooter = $textFooter = NULL;
2645 if ($report['mailing']['header_id']) {
2646 $header = new CRM_Mailing_BAO_MailingComponent();
2647 $header->id
= $report['mailing']['header_id'];
2648 $header->find(TRUE);
2649 $htmlHeader = $header->body_html
;
2650 $textHeader = $header->body_text
;
2653 if ($report['mailing']['footer_id']) {
2654 $footer = new CRM_Mailing_BAO_MailingComponent();
2655 $footer->id
= $report['mailing']['footer_id'];
2656 $footer->find(TRUE);
2657 $htmlFooter = $footer->body_html
;
2658 $textFooter = $footer->body_text
;
2662 $mailingKey = $form->_mailing_id
;
2664 if ($hash = CRM_Mailing_BAO_Mailing
::getMailingHash($mailingKey)) {
2665 $mailingKey = $hash;
2669 if (!empty($report['mailing']['body_text'])) {
2670 $url = CRM_Utils_System
::url('civicrm/mailing/view', 'reset=1&text=1&id=' . $mailingKey);
2671 $form->assign('textViewURL', $url);
2675 if (!empty($report['mailing']['body_html'])) {
2676 $url = CRM_Utils_System
::url('civicrm/mailing/view', 'reset=1&id=' . $mailingKey);
2677 $form->assign('htmlViewURL', $url);
2682 $report['mailing']['attachment'] = CRM_Core_BAO_File
::attachmentInfo('civicrm_mailing', $form->_mailing_id
);
2692 public static function overrideVerp($jobID) {
2693 static $_cache = [];
2695 if (!isset($_cache[$jobID])) {
2697 SELECT override_verp
2698 FROM civicrm_mailing
2699 INNER JOIN civicrm_mailing_job ON civicrm_mailing.id = civicrm_mailing_job.mailing_id
2700 WHERE civicrm_mailing_job.id = %1
2702 $params = [1 => [$jobID, 'Integer']];
2703 $_cache[$jobID] = CRM_Core_DAO
::singleValueQuery($query, $params);
2705 return $_cache[$jobID];
2714 public static function processQueue($mode = NULL) {
2715 $config = CRM_Core_Config
::singleton();
2717 if ($mode == NULL && CRM_Core_BAO_MailSettings
::defaultDomain() == "EXAMPLE.ORG") {
2718 // Using forceBackend=TRUE because WordPress sometimes fails to detect cron
2719 throw new CRM_Core_Exception(ts('The <a href="%1">default mailbox</a> has not been configured. You will find <a href="%2">more info in the online system administrator guide</a>', [
2720 1 => CRM_Utils_System
::url('civicrm/admin/mailSettings', 'reset=1', FALSE, NULL, TRUE, FALSE, TRUE),
2721 2 => "https://docs.civicrm.org/sysadmin/en/latest/setup/civimail/",
2725 // check if we are enforcing number of parallel cron jobs
2727 $gotCronLock = FALSE;
2729 $mailerJobsMax = Civi
::settings()->get('mailerJobsMax');
2730 if (is_numeric($mailerJobsMax) && $mailerJobsMax > 0) {
2731 $lockArray = range(1, $mailerJobsMax);
2733 // Shuffle the array to improve chances of quickly finding an open thread
2734 shuffle($lockArray);
2736 // Check if we are using global locks
2737 foreach ($lockArray as $lockID) {
2738 $cronLock = Civi
::lockManager()
2739 ->acquire("worker.mailing.send.{$lockID}");
2740 if ($cronLock->isAcquired()) {
2741 $gotCronLock = TRUE;
2746 // Exit here since we have enough mailing processes running
2747 if (!$gotCronLock) {
2748 CRM_Core_Error
::debug_log_message('Returning early, since the maximum number of mailing processes are running');
2752 if (getenv('CIVICRM_CRON_HOLD')) {
2753 // In testing, we may need to simulate some slow activities.
2754 sleep(getenv('CIVICRM_CRON_HOLD'));
2758 // Split up the parent jobs into multiple child jobs
2759 $mailerJobSize = Civi
::settings()->get('mailerJobSize');
2760 CRM_Mailing_BAO_MailingJob
::runJobs_pre($mailerJobSize, $mode);
2761 CRM_Mailing_BAO_MailingJob
::runJobs(NULL, $mode);
2762 CRM_Mailing_BAO_MailingJob
::runJobs_post($mode);
2764 // Release the global lock if we do have one
2766 $cronLock->release();
2773 * @param int $mailingID
2775 private static function addMultipleEmails($mailingID) {
2777 INSERT INTO civicrm_mailing_recipients
2778 (mailing_id, email_id, contact_id)
2779 SELECT %1, e.id, e.contact_id FROM civicrm_email e
2781 AND e.is_bulkmail = 1
2783 ( SELECT contact_id FROM civicrm_mailing_recipients mr WHERE mailing_id = %1 )
2784 AND e.id NOT IN ( SELECT email_id FROM civicrm_mailing_recipients mr WHERE mailing_id = %1 )
2786 $params = [1 => [$mailingID, 'Integer']];
2788 $dao = CRM_Core_DAO
::executeQuery($sql, $params);
2792 * @param bool $isSMS
2796 public static function getMailingsList($isSMS = FALSE) {
2800 $where .= " civicrm_mailing.sms_provider_id IS NULL ";
2803 $where .= " civicrm_mailing.sms_provider_id IS NOT NULL ";
2808 SELECT civicrm_mailing.id, civicrm_mailing.name, civicrm_mailing_job.end_date
2809 FROM civicrm_mailing
2810 INNER JOIN civicrm_mailing_job ON civicrm_mailing.id = civicrm_mailing_job.mailing_id {$where}
2811 ORDER BY civicrm_mailing.name";
2812 $mailing = CRM_Core_DAO
::executeQuery($query);
2814 while ($mailing->fetch()) {
2815 $list[$mailing->id
] = "{$mailing->name} :: {$mailing->end_date}";
2823 * wrapper for ajax activity selector.
2825 * @param array $params
2826 * Associated array for params record id.
2829 * associated array of contact activities
2831 public static function getContactMailingSelector(&$params) {
2832 // format the params
2833 $params['offset'] = ($params['page'] - 1) * $params['rp'];
2834 $params['rowCount'] = $params['rp'];
2835 $params['sort'] = $params['sortBy'] ??
NULL;
2836 $params['caseId'] = NULL;
2838 // get contact mailings
2839 $mailings = CRM_Mailing_BAO_Mailing
::getContactMailings($params);
2842 $params['total'] = CRM_Mailing_BAO_Mailing
::getContactMailingsCount($params);
2845 if (!empty($mailings)) {
2846 $openCounts = CRM_Mailing_Event_BAO_Opened
::getMailingContactCount(array_keys($mailings), $params['contact_id']);
2847 $clickCounts = CRM_Mailing_Event_BAO_TrackableURLOpen
::getMailingContactCount(array_keys($mailings), $params['contact_id']);
2850 // format params and add links
2851 $contactMailings = [];
2852 foreach ($mailings as $mailingId => $values) {
2854 $mailing['subject'] = $values['subject'];
2855 $mailing['creator_name'] = CRM_Utils_System
::href(
2856 $values['creator_name'],
2857 'civicrm/contact/view',
2858 "reset=1&cid={$values['creator_id']}");
2859 $mailing['recipients'] = CRM_Utils_System
::href(ts('(recipients)'), 'civicrm/mailing/report/event',
2860 "mid={$values['mailing_id']}&reset=1&cid={$params['contact_id']}&event=queue&context=mailing");
2861 $mailing['start_date'] = CRM_Utils_Date
::customFormat($values['start_date']);
2863 $mailing['openstats'] = "Opens: " .
2864 CRM_Utils_Array
::value($values['mailing_id'], $openCounts, 0) .
2866 CRM_Utils_Array
::value($values['mailing_id'], $clickCounts, 0);
2869 CRM_Core_Action
::VIEW
=> [
2870 'name' => ts('View'),
2871 'url' => 'civicrm/mailing/view',
2872 'qs' => "reset=1&id=%%mkey%%",
2873 'title' => ts('View Mailing'),
2874 'class' => 'crm-popup',
2876 CRM_Core_Action
::BROWSE
=> [
2877 'name' => ts('Mailing Report'),
2878 'url' => 'civicrm/mailing/report',
2879 'qs' => "mid=%%mid%%&reset=1&cid=%%cid%%&context=mailing",
2880 'title' => ts('View Mailing Report'),
2884 $mailingKey = $values['mailing_id'];
2885 if ($hash = CRM_Mailing_BAO_Mailing
::getMailingHash($mailingKey)) {
2886 $mailingKey = $hash;
2889 $mailing['links'] = CRM_Core_Action
::formLink(
2893 'mid' => $values['mailing_id'],
2894 'cid' => $params['contact_id'],
2895 'mkey' => $mailingKey,
2899 'mailing.contact.action',
2901 $values['mailing_id']
2904 array_push($contactMailings, $mailing);
2907 $contactMailingsDT = [];
2908 $contactMailingsDT['data'] = $contactMailings;
2909 $contactMailingsDT['recordsTotal'] = $params['total'];
2910 $contactMailingsDT['recordsFiltered'] = $params['total'];
2912 return $contactMailingsDT;
2916 * Retrieve contact mailing.
2918 * @param array $params
2921 * Array of mailings for a contact
2924 public static function getContactMailings(&$params) {
2925 $params['version'] = 3;
2926 $params['offset'] = ($params['page'] - 1) * $params['rp'];
2927 $params['limit'] = $params['rp'];
2928 $params['sort'] = $params['sortBy'] ??
NULL;
2930 $result = civicrm_api('MailingContact', 'get', $params);
2931 return $result['values'];
2935 * Retrieve contact mailing count.
2937 * @param array $params
2940 * count of mailings for a contact
2943 public static function getContactMailingsCount(&$params) {
2944 $params['version'] = 3;
2945 return civicrm_api('MailingContact', 'getcount', $params);
2949 * Get a list of permissions required for CRUD'ing each field
2950 * (when workflow is enabled).
2953 * Array (string $fieldName => string $permName)
2955 public static function getWorkflowFieldPerms() {
2956 $fieldNames = array_keys(CRM_Mailing_DAO_Mailing
::fields());
2958 foreach ($fieldNames as $fieldName) {
2959 if ($fieldName == 'id') {
2960 $fieldPerms[$fieldName] = [
2964 'schedule mailings',
2969 elseif (in_array($fieldName, ['scheduled_date', 'scheduled_id'])) {
2970 $fieldPerms[$fieldName] = [
2972 ['access CiviMail', 'schedule mailings'],
2975 elseif (in_array($fieldName, [
2978 'approval_status_id',
2981 $fieldPerms[$fieldName] = [
2983 ['access CiviMail', 'approve mailings'],
2987 $fieldPerms[$fieldName] = [
2989 ['access CiviMail', 'create mailings'],
2997 * White-list of possible values for the entity_table field.
3001 public static function mailingGroupEntityTables() {
3003 CRM_Contact_BAO_Group
::getTableName() => 'Group',
3004 CRM_Mailing_BAO_Mailing
::getTableName() => 'Mailing',
3009 * Get the public view url.
3012 * @param bool $absolute
3016 public static function getPublicViewUrl($id, $absolute = TRUE) {
3017 if ((civicrm_api3('Mailing', 'getvalue', [
3019 'return' => 'visibility',
3020 ])) === 'Public Pages') {
3022 // if hash setting is on then we change the public url into a hash
3023 $hash = CRM_Mailing_BAO_Mailing
::getMailingHash($id);
3024 if (!empty($hash)) {
3028 return CRM_Utils_System
::url('civicrm/mailing/view', ['id' => $id], $absolute, NULL, TRUE, TRUE);
3033 * Get a list of template types which can be used as `civicrm_mailing.template_type`.
3036 * A list of template-types, keyed numerically. Each defines:
3037 * - name: string, a short symbolic name
3038 * - editorUrl: string, Angular template name
3040 * Ex: $templateTypes[0] === array('name' => 'mosaico', 'editorUrl' => '~/crmMosaico/editor.html').
3042 public static function getTemplateTypes() {
3043 if (!isset(Civi
::$statics[__CLASS__
]['templateTypes'])) {
3046 'name' => 'traditional',
3047 'editorUrl' => CRM_Mailing_Info
::workflowEnabled() ?
'~/crmMailing/EditMailingCtrl/workflow.html' : '~/crmMailing/EditMailingCtrl/2step.html',
3051 CRM_Utils_Hook
::mailingTemplateTypes($types);
3053 $defaults = ['weight' => 0];
3054 foreach (array_keys($types) as $typeName) {
3055 $types[$typeName] = array_merge($defaults, $types[$typeName]);
3057 usort($types, function ($a, $b) {
3058 if ($a['weight'] === $b['weight']) {
3061 return $a['weight'] < $b['weight'] ?
-1 : 1;
3064 Civi
::$statics[__CLASS__
]['templateTypes'] = $types;
3067 return Civi
::$statics[__CLASS__
]['templateTypes'];
3071 * Get a list of template types.
3074 * Array(string $name => string $label).
3076 public static function getTemplateTypeNames() {
3078 foreach (self
::getTemplateTypes() as $type) {
3079 $r[$type['name']] = $type['name'];