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_opt_out' => CRM_Utils_SQL_Select
::fragment()->where("$contact.is_opt_out = 0"),
225 'is_deceased' => CRM_Utils_SQL_Select
::fragment()->where("$contact.is_deceased <> 1"),
226 'do_not_sms' => CRM_Utils_SQL_Select
::fragment()->where("$contact.do_not_sms = 0"),
227 'location_filter' => CRM_Utils_SQL_Select
::fragment()->where("$entityTable.phone_type_id = " . CRM_Core_PseudoConstant
::getKey('CRM_Core_DAO_Phone', 'phone_type_id', 'Mobile')),
228 'phone_not_null' => CRM_Utils_SQL_Select
::fragment()->where("$entityTable.phone IS NOT NULL"),
229 'phone_not_empty' => CRM_Utils_SQL_Select
::fragment()->where("$entityTable.phone != ''"),
230 'mailing_id' => CRM_Utils_SQL_Select
::fragment()->where("mg.mailing_id = #mailingID"),
231 'temp_contact_null' => CRM_Utils_SQL_Select
::fragment()->where('temp.contact_id IS null'),
232 'order_by' => CRM_Utils_SQL_Select
::fragment()->orderBy("$entityTable.is_primary"),
236 // Criterias to filter recipients that need to be included
238 'do_not_email' => CRM_Utils_SQL_Select
::fragment()->where("$contact.do_not_email = 0"),
239 'is_opt_out' => CRM_Utils_SQL_Select
::fragment()->where("$contact.is_opt_out = 0"),
240 'is_deceased' => CRM_Utils_SQL_Select
::fragment()->where("$contact.is_deceased <> 1"),
241 'location_filter' => CRM_Utils_SQL_Select
::fragment()->where($location_filter),
242 'email_not_null' => CRM_Utils_SQL_Select
::fragment()->where("$entityTable.email IS NOT NULL"),
243 'email_not_empty' => CRM_Utils_SQL_Select
::fragment()->where("$entityTable.email != ''"),
244 'email_not_on_hold' => CRM_Utils_SQL_Select
::fragment()->where("$entityTable.on_hold = 0"),
245 'mailing_id' => CRM_Utils_SQL_Select
::fragment()->where("mg.mailing_id = #mailingID"),
246 'temp_contact_null' => CRM_Utils_SQL_Select
::fragment()->where('temp.contact_id IS NULL'),
247 'order_by' => CRM_Utils_SQL_Select
::fragment()->orderBy($order_by),
251 // Allow user to alter query responsible to fetch mailing recipients before build,
252 // by changing the mail filters identified $params
253 CRM_Utils_Hook
::alterMailingRecipients($mailingObj, $criteria, 'pre');
255 // Get the group contacts, but only those which are not in the
256 // exclusion temp table.
257 if (!empty($recipientsGroup['Include'])) {
258 CRM_Utils_SQL_Select
::from($entityTable)
259 ->select("$contact.id as contact_id, $entityTable.id as $entityColumn")
260 ->join($contact, " INNER JOIN $contact ON $entityTable.contact_id = $contact.id ")
261 ->join('gc', " INNER JOIN civicrm_group_contact gc ON gc.contact_id = $contact.id ")
262 ->join('mg', " INNER JOIN civicrm_mailing_group mg ON gc.group_id = mg.entity_id AND mg.search_id IS NULL ")
263 ->join('temp', " LEFT JOIN $excludeTempTablename temp ON $contact.id = temp.contact_id ")
264 ->where('gc.group_id IN (#groups) AND gc.status = "Added"')
266 ->groupBy(["$contact.id", "$entityTable.id"])
267 ->replaceInto($includedTempTablename, ['contact_id', $entityColumn])
268 ->param('#groups', $recipientsGroup['Include'])
269 ->param('#mailingID', $mailingID)
273 // Get recipients selected in prior mailings
274 if (!empty($priorMailingIDs['Include'])) {
275 CRM_Utils_SQL_Select
::from('civicrm_mailing_recipients')
276 ->select("DISTINCT civicrm_mailing_recipients.contact_id, $entityColumn")
277 ->join('temp', " LEFT JOIN $excludeTempTablename temp ON civicrm_mailing_recipients.contact_id = temp.contact_id ")
278 ->where('mailing_id IN (#mailings)')
279 ->where('temp.contact_id IS NULL')
280 ->param('#mailings', $priorMailingIDs['Include'])
281 ->insertIgnoreInto($includedTempTablename, [
288 if (count($includeSmartGroupIDs)) {
289 $query = CRM_Utils_SQL_Select
::from($contact)
290 ->select("$contact.id as contact_id, $entityTable.id as $entityColumn")
291 ->join($entityTable, " INNER JOIN $entityTable ON $entityTable.contact_id = $contact.id ")
292 ->join('gc', " INNER JOIN civicrm_group_contact_cache gc ON $contact.id = gc.contact_id ")
293 ->join('gcr', " LEFT JOIN civicrm_group_contact gcr ON gc.group_id = gcr.group_id AND gc.contact_id = gcr.contact_id")
294 ->join('mg', " INNER JOIN civicrm_mailing_group mg ON gc.group_id = mg.entity_id AND mg.search_id IS NULL ")
295 ->join('temp', " LEFT JOIN $excludeTempTablename temp ON $contact.id = temp.contact_id ")
296 ->where('gc.group_id IN (#groups)')
297 ->where('gcr.status IS NULL OR gcr.status != "Removed"')
299 ->replaceInto($includedTempTablename, ['contact_id', $entityColumn])
300 ->param('#groups', $includeSmartGroupIDs)
301 ->param('#mailingID', $mailingID)
305 // Construct the filtered search queries.
306 $dao = CRM_Utils_SQL_Select
::from('civicrm_mailing_group')
307 ->select('search_id, search_args, entity_id')
308 ->where('search_id IS NOT NULL AND mailing_id = #mailingID')
309 ->param('#mailingID', $mailingID)
311 while ($dao->fetch()) {
312 $customSQL = CRM_Contact_BAO_SearchCustom
::civiMailSQL($dao->search_id
,
316 $query = "REPLACE INTO {$includedTempTablename} ($entityColumn, contact_id) {$customSQL} ";
317 $mailingGroup->query($query);
320 list($aclFrom, $aclWhere) = CRM_Contact_BAO_Contact_Permission
::cacheClause();
322 // clear all the mailing recipients before populating
323 CRM_Core_DAO
::executeQuery(" DELETE FROM civicrm_mailing_recipients WHERE mailing_id = %1 ", [
330 $selectClause = ['#mailingID', 'i.contact_id', "i.$entityColumn"];
332 $orderBy = ["i.contact_id", "i.$entityColumn"];
334 $query = CRM_Utils_SQL_Select
::from('civicrm_contact contact_a')
335 ->join('i', " INNER JOIN {$includedTempTablename} i ON contact_a.id = i.contact_id ");
336 if (!$isSMSmode && $mailingObj->dedupe_email
) {
337 $orderBy = ["MIN(i.contact_id)", "MIN(i.$entityColumn)"];
338 $query = $query->join('e', " INNER JOIN civicrm_email e ON e.id = i.email_id ")
339 ->groupBy("e.email");
340 if (CRM_Utils_SQL
::supportsFullGroupBy()) {
343 'ANY_VALUE(i.contact_id) contact_id',
344 "ANY_VALUE(i.$entityColumn) $entityColumn",
350 $query = $query->select($selectClause)->orderBy($orderBy);
351 if (!CRM_Utils_System
::isNull($aclFrom)) {
352 $query = $query->join('acl', $aclFrom);
354 if (!CRM_Utils_System
::isNull($aclWhere)) {
355 $query = $query->where($aclWhere);
358 // this mean if dedupe_email AND the mysql 5.7 supports ONLY_FULL_GROUP_BY mode then as
359 // SELECT must contain 'email' column as its used in GROUP BY, so in order to resolve This
360 // here the whole SQL code is wrapped up in FROM table i and not selecting email column for INSERT
361 if ($key = array_search('e.email', $selectClause)) {
362 unset($selectClause[$key]);
363 $sql = $query->toSQL();
364 CRM_Utils_SQL_Select
::from("( $sql ) AS i ")
365 ->select($selectClause)
366 ->insertInto('civicrm_mailing_recipients', ['mailing_id', 'contact_id', $entityColumn])
367 ->param('#mailingID', $mailingID)
371 $query->insertInto('civicrm_mailing_recipients', ['mailing_id', 'contact_id', $entityColumn])
372 ->param('#mailingID', $mailingID)
376 // if we need to add all emails marked bulk, do it as a post filter
377 // on the mailing recipients table
378 if (CRM_Core_BAO_Email
::isMultipleBulkMail()) {
379 self
::addMultipleEmails($mailingID);
382 // Delete the temp table.
383 $mailingGroup->reset();
384 $excludeTempTable->drop();
385 $includedTempTable->drop();
387 CRM_Utils_Hook
::alterMailingRecipients($mailingObj, $criteria, 'post');
391 * Function to retrieve location filter and order by clause later used by SQL query that is used to fetch and include mailing recipients
393 * @param string $email_selection_method
394 * @param int $location_type_id
398 public static function getLocationFilterAndOrderBy($email_selection_method, $location_type_id) {
399 $email = CRM_Core_DAO_Email
::getTableName();
400 // Note: When determining the ORDER that results are returned, it's
401 // the record that comes last that counts. That's because we are
402 // INSERT'ing INTO a table with a primary id so that last record
403 // over writes any previous record.
404 switch ($email_selection_method) {
405 case 'location-exclude':
406 $location_filter = "($email.location_type_id != $location_type_id)";
407 // If there is more than one email that doesn't match the location,
408 // prefer the one marked is_bulkmail, followed by is_primary.
409 $orderBy = ["$email.is_bulkmail", "$email.is_primary"];
412 case 'location-only':
413 $location_filter = "($email.location_type_id = $location_type_id)";
414 // If there is more than one email of the desired location, prefer
415 // the one marked is_bulkmail, followed by is_primary.
416 $orderBy = ["$email.is_bulkmail", "$email.is_primary"];
419 case 'location-prefer':
420 $location_filter = "($email.is_bulkmail = 1 OR $email.is_primary = 1 OR $email.location_type_id = $location_type_id)";
421 // ORDER BY is more complicated because we have to set an arbitrary
422 // order that prefers the location that we want. We do that using
423 // the FIELD function. For more info, see:
424 // https://dev.mysql.com/doc/refman/5.5/en/string-functions.html#function_field
425 // We assign the location type we want the value "1" by putting it
426 // in the first position after we name the field. All other location
427 // types are left out, so they will be assigned the value 0. That
428 // means, they will all be equally tied for first place, with our
429 // location being last.
431 "FIELD($email.location_type_id, $location_type_id)",
432 "$email.is_bulkmail",
438 // fall through to default
440 $location_filter = "($email.is_bulkmail = 1 OR $email.is_primary = 1)";
441 $orderBy = ["$email.is_bulkmail"];
444 return [$location_filter, $orderBy];
448 * @param string $type
452 private function _getMailingGroupIds($type = 'Include') {
453 $mailingGroup = new CRM_Mailing_DAO_MailingGroup();
454 $group = CRM_Contact_DAO_Group
::getTableName();
455 if (!isset($this->id
)) {
456 // we're just testing tokens, so return any group
457 $query = "SELECT id AS entity_id
463 $mg = CRM_Mailing_DAO_MailingGroup
::getTableName();
464 $query = "SELECT entity_id
466 WHERE mailing_id = {$this->id}
467 AND group_type = '$type'
468 AND entity_table = '$group'";
470 $mailingGroup->query($query);
473 while ($mailingGroup->fetch()) {
474 $groupIds[] = $mailingGroup->entity_id
;
481 * Returns the regex patterns that are used for preparing the text and html templates.
483 * @param bool $onlyHrefs
485 * @return array|string
487 private function getPatterns($onlyHrefs = FALSE) {
491 $protos = '(https?|ftp|mailto)';
493 $gunk = '\{\}/#~:.?+=&;%@!\,\-\|\(\)\*';
495 $any = "{$letters}{$gunk}{$punc}";
497 $pattern = "\\bhref[ ]*=[ ]*([\"'])?(($protos:[$any]+?(?=[$punc]*[^$any]|$)))([\"'])?";
500 $pattern = "\\b($protos:[$any]+?(?=[$punc]*[^$any]|$))";
503 $patterns[] = $pattern;
504 $patterns[] = '\\\\\{\w+\.\w+\\\\\}|\{\{\w+\.\w+\}\}';
505 $patterns[] = '\{\w+\.\w+\}';
507 $patterns = '{' . implode('|', $patterns) . '}imu';
513 * Returns an array that denotes the type of token that we are dealing with
514 * we use the type later on when we are doing a token replacement lookup
516 * @param string $token
517 * The token for which we will be doing adata lookup.
520 * An array that holds the token itself and the type.
521 * the type will tell us which function to use for the data lookup
522 * if we need to do a lookup at all
524 public function &getDataFunc($token) {
525 static $_categories = NULL;
526 static $_categoryString = NULL;
535 CRM_Utils_Hook
::tokens($_categories);
536 $_categoryString = implode('|', array_keys($_categories));
539 $funcStruct = ['type' => NULL, 'token' => $token];
541 if ((preg_match('/^href/i', $token) ||
preg_match('/^http/i', $token))) {
542 // it is a url so we need to check to see if there are any tokens embedded
543 // if so then call this function again to get the token dataFunc
544 // and assign the type 'embedded' so that the data retrieving function
545 // will know what how to handle this token.
546 if (preg_match_all('/(\{\w+\.\w+\})/', $token, $matches)) {
547 $funcStruct['type'] = 'embedded_url';
548 $funcStruct['embed_parts'] = $funcStruct['token'] = [];
549 foreach ($matches[1] as $match) {
550 $preg_token = '/' . preg_quote($match, '/') . '/';
551 $list = preg_split($preg_token, $token, 2);
552 $funcStruct['embed_parts'][] = $list[0];
554 $funcStruct['token'][] = $this->getDataFunc($match);
556 // fixed truncated url, CRM-7113
558 $funcStruct['embed_parts'][] = $token;
562 $funcStruct['type'] = 'url';
565 elseif (preg_match('/^\{(' . $_categoryString . ')\.(\w+)\}$/', $token, $matches)) {
566 $funcStruct['type'] = $matches[1];
567 $funcStruct['token'] = $matches[2];
569 elseif (preg_match('/\\\\\{(\w+\.\w+)\\\\\}|\{\{(\w+\.\w+)\}\}/', $token, $matches)) {
570 // we are an escaped token
571 // so remove the escape chars
572 $unescaped_token = preg_replace('/\{\{|\}\}|\\\\\{|\\\\\}/', '', $matches[0]);
573 $funcStruct['token'] = '{' . $unescaped_token . '}';
579 * Prepares the text and html templates
580 * for generating the emails and returns a copy of the
584 * This is used by CiviMail but will be made redundant by FlexMailer/TokenProcessor.
586 private function getPreparedTemplates() {
587 if (!$this->preparedTemplates
) {
588 $patterns['html'] = $this->getPatterns(TRUE);
589 $patterns['subject'] = $patterns['text'] = $this->getPatterns();
590 $templates = $this->getTemplates();
592 $this->preparedTemplates
= [];
599 if (!isset($templates[$key])) {
605 $split_template = [];
607 $email = $templates[$key];
608 preg_match_all($patterns[$key], $email, $matches, PREG_PATTERN_ORDER
);
609 foreach ($matches[0] as $idx => $token) {
610 $preg_token = '/' . preg_quote($token, '/') . '/im';
611 list($split_template[], $email) = preg_split($preg_token, $email, 2);
612 array_push($tokens, $this->getDataFunc($token));
615 $split_template[] = $email;
617 $this->preparedTemplates
[$key]['template'] = $split_template;
618 $this->preparedTemplates
[$key]['tokens'] = $tokens;
621 return ($this->preparedTemplates
);
625 * Retrieve a ref to an array that holds the email and text templates for this email
626 * assembles the complete template including the header and footer
627 * that the user has uploaded or declared (if they have done that)
630 * reference to an assoc array
632 public function getTemplates() {
633 if (!$this->templates
) {
634 $this->getHeaderFooter();
635 $this->templates
= [];
636 if ($this->body_text ||
!empty($this->header
)) {
638 if (!empty($this->header
->body_text
)) {
639 $template[] = $this->header
->body_text
;
641 elseif (!empty($this->header
->body_html
)) {
642 $template[] = CRM_Utils_String
::htmlToText($this->header
->body_html
);
645 if ($this->body_text
) {
646 $template[] = $this->body_text
;
649 $template[] = CRM_Utils_String
::htmlToText($this->body_html
);
652 if (!empty($this->footer
->body_text
)) {
653 $template[] = $this->footer
->body_text
;
655 elseif (!empty($this->footer
->body_html
)) {
656 $template[] = CRM_Utils_String
::htmlToText($this->footer
->body_html
);
659 $this->templates
['text'] = implode("\n", $template);
662 // To check for an html part strip tags
663 if (trim(strip_tags($this->body_html
, '<img>'))) {
667 $template[] = $this->header
->body_html
;
670 $template[] = $this->body_html
;
673 $template[] = $this->footer
->body_html
;
676 $this->templates
['html'] = implode("\n", $template);
678 // this is where we create a text template from the html template if the text template did not exist
679 // this way we ensure that every recipient will receive an email even if the pref is set to text and the
680 // user uploads an html email only
681 if (empty($this->templates
['text'])) {
682 $this->templates
['text'] = CRM_Utils_String
::htmlToText($this->templates
['html']);
686 if ($this->subject
) {
688 $template[] = $this->subject
;
689 $this->templates
['subject'] = implode("\n", $template);
692 $this->templates
['mailingID'] = $this->id
;
693 $this->templates
['campaign_id'] = $this->campaign_id
;
694 $this->templates
['template_type'] = $this->template_type
;
695 CRM_Utils_Hook
::alterMailContent($this->templates
);
697 return $this->templates
;
702 * Retrieve a ref to an array that holds all of the tokens in the email body
703 * where the keys are the type of token and the values are ordinal arrays
704 * that hold the token names (even repeated tokens) in the order in which
705 * they appear in the body of the email.
707 * note: the real work is done in the _getTokens() function
709 * this function needs to have some sort of a body assigned
710 * either text or html for this to have any meaningful impact
713 * reference to an assoc array
715 public function &getTokens() {
716 if (!$this->tokens
) {
718 $this->tokens
= ['html' => [], 'text' => [], 'subject' => []];
720 if ($this->body_html
) {
721 $this->_getTokens('html');
722 if (!$this->body_text
) {
723 // Since the text template was created from html, use the html tokens.
724 // @see CRM_Mailing_BAO_Mailing::getTemplates()
725 $this->tokens
['text'] = $this->tokens
['html'];
729 if ($this->body_text
) {
730 $this->_getTokens('text');
733 if ($this->subject
) {
734 $this->_getTokens('subject');
738 return $this->tokens
;
742 * Returns the token set for all 3 parts as one set. This allows it to be sent to the
743 * hook in one call and standardizes it across other token workflows
746 * reference to an assoc array
748 public function &getFlattenedTokens() {
749 if (!$this->flattenedTokens
) {
750 $tokens = $this->getTokens();
752 $this->flattenedTokens
= CRM_Utils_Token
::flattenTokens($tokens);
755 return $this->flattenedTokens
;
760 * _getTokens parses out all of the tokens that have been
761 * included in the html and text bodies of the email
762 * we get the tokens and then separate them into an
763 * internal structure named tokens that has the same
764 * form as the static tokens property(?) of the CRM_Utils_Token class.
765 * The difference is that there might be repeated token names as we want the
766 * structures to represent the order in which tokens were found from left to right, top to bottom.
769 * @param string $prop name of the property that holds the text that we want to scan for tokens (html, text).
770 * Name of the property that holds the text that we want to scan for tokens (html, text).
774 private function _getTokens($prop) {
775 $templates = $this->getTemplates();
777 $newTokens = CRM_Utils_Token
::getTokens($templates[$prop]);
779 foreach ($newTokens as $type => $names) {
780 if (!isset($this->tokens
[$prop][$type])) {
781 $this->tokens
[$prop][$type] = [];
783 foreach ($names as $key => $name) {
784 $this->tokens
[$prop][$type][] = $name;
790 * Generate an event queue for a test job.
792 * @param array $testParams
793 * Contains form values.
797 public function getTestRecipients($testParams) {
798 if (!empty($testParams['test_group']) && array_key_exists($testParams['test_group'], CRM_Core_PseudoConstant
::group())) {
799 $contacts = civicrm_api('contact', 'get', [
801 'group' => $testParams['test_group'],
804 'limit' => 100000000000,
808 foreach (array_keys($contacts['values']) as $groupContact) {
810 SELECT civicrm_email.id AS email_id,
811 civicrm_email.is_primary as is_primary,
812 civicrm_email.is_bulkmail as is_bulkmail
814 INNER JOIN civicrm_contact ON civicrm_email.contact_id = civicrm_contact.id
815 WHERE (civicrm_email.is_bulkmail = 1 OR civicrm_email.is_primary = 1)
816 AND civicrm_contact.id = {$groupContact}
817 AND civicrm_contact.do_not_email = 0
818 AND civicrm_contact.is_deceased <> 1
819 AND civicrm_email.on_hold = 0
820 AND civicrm_contact.is_opt_out = 0
821 GROUP BY civicrm_email.id
822 ORDER BY civicrm_email.is_bulkmail DESC
824 $dao = CRM_Core_DAO
::executeQuery($query);
827 'job_id' => $testParams['job_id'],
828 'email_id' => $dao->email_id
,
829 'contact_id' => $groupContact,
831 CRM_Mailing_Event_BAO_Queue
::create($params);
838 * Load this->header and this->footer.
840 private function getHeaderFooter() {
841 if (!$this->header
and $this->header_id
) {
842 $this->header
= new CRM_Mailing_BAO_MailingComponent();
843 $this->header
->id
= $this->header_id
;
844 $this->header
->find(TRUE);
847 if (!$this->footer
and $this->footer_id
) {
848 $this->footer
= new CRM_Mailing_BAO_MailingComponent();
849 $this->footer
->id
= $this->footer_id
;
850 $this->footer
->find(TRUE);
855 * Given and array of headers and a prefix, job ID, event queue ID, and hash,
856 * add a Message-ID header if needed.
858 * i.e. if the global includeMessageId is set and there isn't already a
859 * Message-ID in the array.
860 * The message ID is structured the same way as a verp. However no interpretation
861 * is placed on the values received, so they do not need to follow the verp
864 * @param array $headers
865 * Array of message headers to update, in-out.
866 * @param string $prefix
867 * Prefix for the message ID, use same prefixes as verp.
869 * @param string $job_id
870 * Job ID component of the generated message ID.
871 * @param string $event_queue_id
872 * Event Queue ID component of the generated message ID.
873 * @param string $hash
874 * Hash component of the generated message ID.
878 public static function addMessageIdHeader(&$headers, $prefix, $job_id, $event_queue_id, $hash) {
879 $config = CRM_Core_Config
::singleton();
880 $localpart = CRM_Core_BAO_MailSettings
::defaultLocalpart();
881 $emailDomain = CRM_Core_BAO_MailSettings
::defaultDomain();
882 $includeMessageId = CRM_Core_BAO_MailSettings
::includeMessageId();
884 $fields[] = 'Message-ID';
885 // CRM-17754 check if Resent-Message-id is set also if not add it in when re-laying reply email
886 if ($prefix == 'r') {
887 $fields[] = 'Resent-Message-ID';
889 foreach ($fields as $field) {
890 if ($includeMessageId && (!array_key_exists($field, $headers))) {
891 $headers[$field] = '<' . implode($config->verpSeparator
,
893 $localpart . $prefix,
898 ) . "@{$emailDomain}>";
905 * Static wrapper for getting verp and urls.
908 * ID of the Job associated with this message.
909 * @param int $event_queue_id
910 * ID of the EventQueue.
911 * @param string $hash
912 * Hash of the EventQueue.
913 * @param string $email
914 * Destination address.
917 * (reference) array array ref that hold array refs to the verp info and urls
919 public static function getVerpAndUrls($job_id, $event_queue_id, $hash, $email) {
920 // create a skeleton object and set its properties that are required by getVerpAndUrlsAndHeaders()
921 $config = CRM_Core_Config
::singleton();
922 $bao = new CRM_Mailing_BAO_Mailing();
923 $bao->_domain
= CRM_Core_BAO_Domain
::getDomain();
924 $bao->from_name
= $bao->from_email
= $bao->subject
= '';
926 // use $bao's instance method to get verp and urls
927 list($verp, $urls, $_) = $bao->getVerpAndUrlsAndHeaders($job_id, $event_queue_id, $hash, $email);
928 return [$verp, $urls];
932 * Get verp, urls and headers
935 * ID of the Job associated with this message.
936 * @param int $event_queue_id
937 * ID of the EventQueue.
938 * @param string $hash
939 * Hash of the EventQueue.
940 * @param string $email
941 * Destination address.
943 * @param bool $isForward
946 * array ref that hold array refs to the verp info, urls, and headers
948 public function getVerpAndUrlsAndHeaders($job_id, $event_queue_id, $hash, $email, $isForward = FALSE) {
949 $config = CRM_Core_Config
::singleton();
953 * reply: user replied to mailing
954 * bounce: email address bounced
955 * unsubscribe: contact opts out of all target lists for the mailing
956 * resubscribe: contact opts back into all target lists for the mailing
957 * optOut: contact unsubscribes from the domain
963 'unsubscribe' => 'u',
964 'resubscribe' => 'e',
968 $localpart = CRM_Core_BAO_MailSettings
::defaultLocalpart();
969 $emailDomain = CRM_Core_BAO_MailSettings
::defaultDomain();
970 // 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
971 if (empty($emailDomain)) {
972 CRM_Core_Error
::debug_log_message('Error setting verp parameters, defaultDomain is NULL. Did you configure the bounce processing account for this domain?');
975 foreach ($verpTokens as $key => $value) {
976 $verp[$key] = implode($config->verpSeparator
,
986 //handle should override VERP address.
990 self
::overrideVerp($job_id)
992 $verp['reply'] = "\"{$this->from_name}\" <{$this->from_email}>";
996 'forward' => CRM_Utils_System
::url('civicrm/mailing/forward', "reset=1&jid={$job_id}&qid={$event_queue_id}&h={$hash}", TRUE, NULL, TRUE, TRUE),
997 'unsubscribeUrl' => CRM_Utils_System
::url('civicrm/mailing/unsubscribe', "reset=1&jid={$job_id}&qid={$event_queue_id}&h={$hash}", TRUE, NULL, TRUE, TRUE),
998 'resubscribeUrl' => CRM_Utils_System
::url('civicrm/mailing/resubscribe', "reset=1&jid={$job_id}&qid={$event_queue_id}&h={$hash}", TRUE, NULL, TRUE, TRUE),
999 'optOutUrl' => CRM_Utils_System
::url('civicrm/mailing/optout', "reset=1&jid={$job_id}&qid={$event_queue_id}&h={$hash}", TRUE, NULL, TRUE, TRUE),
1000 'subscribeUrl' => CRM_Utils_System
::url('civicrm/mailing/subscribe', 'reset=1', TRUE, NULL, TRUE, TRUE),
1004 'Reply-To' => $verp['reply'],
1005 'Return-Path' => $verp['bounce'],
1006 'From' => "\"{$this->from_name}\" <{$this->from_email}>",
1007 'Subject' => $this->subject
,
1008 'List-Unsubscribe' => "<mailto:{$verp['unsubscribe']}>",
1010 self
::addMessageIdHeader($headers, 'm', $job_id, $event_queue_id, $hash);
1012 $headers['Subject'] = "[Fwd:{$this->subject}]";
1014 return [&$verp, &$urls, &$headers];
1018 * Compose a message.
1021 * This is used by CiviMail but will be made redundant by FlexMailer/TokenProcessor.
1022 * @param int $job_id
1023 * ID of the Job associated with this message.
1024 * @param int $event_queue_id
1025 * ID of the EventQueue.
1026 * @param string $hash
1027 * Hash of the EventQueue.
1028 * @param string $contactId
1029 * ID of the Contact.
1030 * @param string $email
1031 * Destination address.
1032 * @param string $recipient
1033 * To: of the recipient.
1035 * Is this mailing a test?.
1036 * @param $contactDetails
1037 * @param $attachments
1038 * @param bool $isForward
1039 * Is this mailing compose for forward?.
1040 * @param string $fromEmail
1041 * Email address of who is forwardinf it.
1043 * @param null $replyToEmail
1045 * @return Mail_mime The mail object
1047 public function compose(
1048 $job_id, $event_queue_id, $hash, $contactId,
1049 $email, &$recipient, $test,
1050 $contactDetails, &$attachments, $isForward = FALSE,
1051 $fromEmail = NULL, $replyToEmail = NULL
1053 $config = CRM_Core_Config
::singleton();
1056 if ($this->_domain
== NULL) {
1057 $this->_domain
= CRM_Core_BAO_Domain
::getDomain();
1060 list($verp, $urls, $headers) = $this->getVerpAndUrlsAndHeaders(
1068 //set from email who is forwarding it and not original one.
1070 unset($headers['From']);
1071 $headers['From'] = "<{$fromEmail}>";
1074 if ($replyToEmail && ($fromEmail != $replyToEmail)) {
1075 $headers['Reply-To'] = "{$replyToEmail}";
1078 if ($contactDetails) {
1079 $contact = $contactDetails;
1081 elseif ($contactId === 0) {
1084 CRM_Utils_Hook
::tokenValues($contact, [$contactId], $job_id);
1087 $params = [['contact_id', '=', $contactId, 0, 0]];
1088 list($contact) = CRM_Contact_BAO_Query
::apiQuery($params);
1089 // $contact is an array of [ contactID => contactDetails ]
1091 // also call the hook to get contact details
1092 CRM_Utils_Hook
::tokenValues($contact, [$contactId], $job_id);
1094 // Don't send if contact doesn't exist
1095 $contact = reset($contact);
1096 if (!$contact ||
is_a($contact, 'CRM_Core_Error')) {
1097 CRM_Core_Error
::debug_log_message(ts('CiviMail will not send email to a non-existent contact: %1',
1100 // setting this because function is called by reference
1101 //@todo test not calling function by reference
1107 $pTemplates = $this->getPreparedTemplates();
1110 foreach ($pTemplates as $type => $pTemplate) {
1111 $html = $type == 'html';
1112 $pEmails[$type] = [];
1113 $pEmail = &$pEmails[$type];
1114 $template = &$pTemplates[$type]['template'];
1115 $tokens = &$pTemplates[$type]['tokens'];
1117 if (!empty($tokens)) {
1118 foreach ($tokens as $idx => $token) {
1119 $token_data = $this->getTokenData($token, $html, $contact, $verp, $urls, $event_queue_id);
1120 array_push($pEmail, $template[$idx]);
1121 array_push($pEmail, $token_data);
1125 array_push($pEmail, $template[$idx]);
1128 if (isset($template[($idx +
1)])) {
1129 array_push($pEmail, $template[($idx +
1)]);
1134 if (isset($pEmails['html']) && is_array($pEmails['html']) && count($pEmails['html'])) {
1135 $html = &$pEmails['html'];
1139 if (isset($pEmails['text']) && is_array($pEmails['text']) && count($pEmails['text'])) {
1140 $text = &$pEmails['text'];
1143 // push the tracking url on to the html email if necessary
1144 if ($this->open_tracking
&& $html) {
1145 array_push($html, "\n" . '<img src="' . CRM_Utils_System
::externUrl('extern/open', "q=$event_queue_id")
1146 . '" width="1" height="1" alt="" border="0">'
1150 $message = new Mail_mime("\n");
1152 $useSmarty = defined('CIVICRM_MAIL_SMARTY') && CIVICRM_MAIL_SMARTY
;
1154 $smarty = CRM_Core_Smarty
::singleton();
1155 // also add the contact tokens to the template
1156 $smarty->assign_by_ref('contact', $contact);
1159 $mailParams = $headers;
1160 if ($text && ($test ||
$contact['preferred_mail_format'] == 'Text' ||
1161 $contact['preferred_mail_format'] == 'Both' ||
1162 ($contact['preferred_mail_format'] == 'HTML' && !array_key_exists('html', $pEmails))
1165 $textBody = implode('', $text);
1167 $textBody = $smarty->fetch("string:$textBody");
1169 $mailParams['text'] = $textBody;
1172 if ($html && ($test ||
($contact['preferred_mail_format'] == 'HTML' ||
1173 $contact['preferred_mail_format'] == 'Both'
1176 $htmlBody = implode('', $html);
1178 $htmlBody = $smarty->fetch("string:$htmlBody");
1180 $mailParams['html'] = $htmlBody;
1183 if (empty($mailParams['text']) && empty($mailParams['html'])) {
1185 // something went wrong, lets log it and return null (by reference)
1186 CRM_Core_Error
::debug_log_message(ts('CiviMail will not send an empty mail body, Skipping: %1',
1193 $mailParams['attachments'] = $attachments;
1195 $mailParams['Subject'] = $pEmails['subject'] ??
NULL;
1196 if (is_array($mailParams['Subject'])) {
1197 $mailParams['Subject'] = implode('', $mailParams['Subject']);
1200 $mailParams['toName'] = CRM_Utils_Array
::value('display_name',
1203 $mailParams['toEmail'] = $email;
1205 // Add job ID to mailParams for external email delivery service to utilise
1206 $mailParams['job_id'] = $job_id;
1208 CRM_Utils_Hook
::alterMailParams($mailParams, 'civimail');
1210 // CRM-10699 support custom email headers
1211 if (!empty($mailParams['headers'])) {
1212 $headers = array_merge($headers, $mailParams['headers']);
1214 //cycle through mailParams and set headers array
1215 foreach ($mailParams as $paramKey => $paramValue) {
1216 //exclude values not intended for the header
1217 if (!in_array($paramKey, [
1225 $headers[$paramKey] = $paramValue;
1229 if (!empty($mailParams['text'])) {
1230 $message->setTxtBody($mailParams['text']);
1233 if (!empty($mailParams['html'])) {
1234 $message->setHTMLBody($mailParams['html']);
1237 if (!empty($mailParams['attachments'])) {
1238 foreach ($mailParams['attachments'] as $fileID => $attach) {
1239 $message->addAttachment($attach['fullPath'],
1240 $attach['mime_type'],
1241 $attach['cleanName']
1246 //pickup both params from mail params.
1247 $toName = trim($mailParams['toName']);
1248 $toEmail = trim($mailParams['toEmail']);
1249 if ($toName == $toEmail ||
1250 strpos($toName, '@') !== FALSE
1255 $toName = CRM_Utils_Mail
::formatRFC2822Name($toName);
1258 $headers['To'] = "$toName <$toEmail>";
1260 $headers['Precedence'] = 'bulk';
1261 // Will test in the mail processor if the X-VERP is set in the bounced email.
1262 // (As an option to replace real VERP for those that can't set it up)
1263 $headers['X-CiviMail-Bounce'] = $verp['bounce'];
1266 //token replacement of subject
1267 $headers['Subject'] = $mailParams['Subject'];
1269 CRM_Utils_Mail
::setMimeParams($message);
1270 $headers = $message->headers($headers);
1272 //get formatted recipient
1273 $recipient = $headers['To'];
1275 // make sure we unset a lot of stuff
1288 * Get mailing object and replaces subscribeInvite, domain and mailing tokens.
1291 * This is used by CiviMail but will be made redundant by FlexMailer/TokenProcessor.
1292 * @param CRM_Mailing_BAO_Mailing $mailing
1294 public static function tokenReplace(&$mailing) {
1295 $domain = CRM_Core_BAO_Domain
::getDomain();
1297 foreach (['text', 'html'] as $type) {
1298 $tokens = $mailing->getTokens();
1299 if (isset($mailing->templates
[$type])) {
1300 $mailing->templates
[$type] = CRM_Utils_Token
::replaceSubscribeInviteTokens($mailing->templates
[$type]);
1301 $mailing->templates
[$type] = CRM_Utils_Token
::replaceDomainTokens(
1302 $mailing->templates
[$type],
1307 $mailing->templates
[$type] = CRM_Utils_Token
::replaceMailingTokens($mailing->templates
[$type], $mailing, NULL, $tokens[$type]);
1313 * Get data to resolve tokens.
1316 * This is used by CiviMail but will be made redundant by FlexMailer/TokenProcessor.
1318 * @param array $token_a
1320 * Whether to encode the token result for use in HTML email
1321 * @param array $contact
1322 * @param string $verp
1323 * @param array $urls
1324 * @param int $event_queue_id
1326 * @return bool|mixed|null|string
1328 private function getTokenData(&$token_a, $html = FALSE, &$contact, &$verp, &$urls, $event_queue_id) {
1329 $type = $token_a['type'];
1330 $token = $token_a['token'];
1333 $useSmarty = defined('CIVICRM_MAIL_SMARTY') && CIVICRM_MAIL_SMARTY
;
1335 if ($type == 'embedded_url') {
1337 foreach ($token as $t) {
1338 $embed_data[] = $this->getTokenData($t, $html, $contact, $verp, $urls, $event_queue_id);
1340 $numSlices = count($embed_data);
1342 for ($i = 0; $i < $numSlices; $i++
) {
1343 $embed_url_data = parse_url($embed_data[$i]);
1344 if (!empty($embed_url_data['scheme'])) {
1345 $token_a['embed_parts'][$i] = preg_replace("/href=\"(https*:\/\/)/", "href=\"", $token_a['embed_parts'][$i]);
1347 $url .= "{$token_a['embed_parts'][$i]}{$embed_data[$i]}";
1349 if (isset($token_a['embed_parts'][$numSlices])) {
1350 $url .= $token_a['embed_parts'][$numSlices];
1352 // add trailing quote since we've gobbled it up in a previous regex
1353 // function getPatterns, line 431
1354 if (preg_match("/^href[ ]*=[ ]*'.*[^']$/", $url)) {
1357 elseif (preg_match('/^href[ ]*=[ ]*".*[^"]$/', $url)) {
1361 // CRM-20206 Fix ampersand encoding in plain text emails
1363 $data = CRM_Utils_String
::unstupifyUrl($data);
1366 elseif ($type == 'url') {
1367 if ($this->url_tracking
&& !empty($this->id
)) {
1368 // ensure that Google CSS and any .css files are not tracked.
1369 if (!(strpos($token, 'css?family') ||
strpos($token, '.css'))) {
1370 $data = CRM_Mailing_BAO_TrackableURL
::getTrackerURL($token, $this->id
, $event_queue_id);
1371 if (!empty($html)) {
1372 $data = htmlentities($data, ENT_NOQUOTES
);
1380 elseif ($type == 'contact') {
1381 $data = CRM_Utils_Token
::getContactTokenReplacement($token, $contact, FALSE, FALSE, $useSmarty);
1383 elseif ($type == 'action') {
1384 $data = CRM_Utils_Token
::getActionTokenReplacement($token, $verp, $urls, $html);
1386 elseif ($type == 'domain') {
1387 $domain = CRM_Core_BAO_Domain
::getDomain();
1388 $data = CRM_Utils_Token
::getDomainTokenReplacement($token, $domain, $html);
1390 elseif ($type == 'mailing') {
1391 if ($token == 'name') {
1392 $data = $this->name
;
1394 elseif ($token == 'group') {
1395 $groups = $this->getGroupNames();
1396 $data = implode(', ', $groups);
1400 $data = $contact["{$type}.{$token}"] ??
NULL;
1406 * Return a list of group names for this mailing. Does not work with
1407 * prior-mailing targets.
1410 * Names of groups receiving this mailing
1412 public function &getGroupNames() {
1413 if (!isset($this->id
)) {
1416 $mg = new CRM_Mailing_DAO_MailingGroup();
1417 $mgtable = CRM_Mailing_DAO_MailingGroup
::getTableName();
1418 $group = CRM_Contact_BAO_Group
::getTableName();
1420 $mg->query("SELECT $group.title as name FROM $mgtable
1421 INNER JOIN $group ON $mgtable.entity_id = $group.id
1422 WHERE $mgtable.mailing_id = {$this->id}
1423 AND $mgtable.entity_table = '$group'
1424 AND $mgtable.group_type = 'Include'
1425 ORDER BY $group.name");
1428 while ($mg->fetch()) {
1429 $groups[] = $mg->name
;
1437 * @param array $params
1438 * Reference array contains the values submitted by the form.
1440 * Reference array contains the id.
1443 * @return CRM_Mailing_DAO_Mailing
1445 public static function add(&$params, $ids = []) {
1446 $id = $params['id'] ??
$ids['mailing_id'] ??
NULL;
1448 if (empty($params['id']) && !empty($ids)) {
1449 CRM_Core_Error
::deprecatedWarning('Parameter $ids is no longer used by Mailing::add. Use the api or just pass $params');
1453 CRM_Utils_Hook
::pre('edit', 'Mailing', $id, $params);
1456 CRM_Utils_Hook
::pre('create', 'Mailing', NULL, $params);
1459 $mailing = new static();
1462 $mailing->find(TRUE);
1464 $mailing->domain_id
= CRM_Utils_Array
::value('domain_id', $params, CRM_Core_Config
::domainID());
1466 if (((!$id && empty($params['replyto_email'])) ||
!isset($params['replyto_email'])) &&
1467 isset($params['from_email'])
1469 $params['replyto_email'] = $params['from_email'];
1471 $mailing->copyValues($params);
1473 // CRM-20892 Unset Modifed Date here so that MySQL can correctly set an updated modfied date.
1474 unset($mailing->modified_date
);
1475 $result = $mailing->save();
1477 // CRM-20892 Re find record after saing so we can set the updated modified date in the result.
1478 $mailing->find(TRUE);
1480 if (isset($mailing->modified_date
)) {
1481 $result->modified_date
= $mailing->modified_date
;
1485 CRM_Utils_Hook
::post('edit', 'Mailing', $mailing->id
, $mailing);
1488 CRM_Utils_Hook
::post('create', 'Mailing', $mailing->id
, $mailing);
1495 * Construct a new mailing object, along with job and mailing_group
1496 * objects, from the form values of the create mailing wizard.
1498 * This function is a bit evil. It not only merges $params and saves
1499 * the mailing -- it also schedules the mailing and chooses the recipients.
1500 * Since it merges $params, it's also the only place to correctly trigger
1501 * multi-field validation. It should be broken up.
1503 * In the mean time, use-cases which break under the weight of this
1504 * evil may find reprieve in these extra evil params:
1506 * - _skip_evil_bao_auto_recipients_: bool
1507 * - _skip_evil_bao_auto_schedule_: bool
1508 * - _evil_bao_validator_: string|callable
1510 * </twowrongsmakesaright>
1512 * @params array $params
1515 * @param array $params
1519 * $mailing The new mailing object
1521 * @throws \CRM_Core_Exception
1522 * @throws \CiviCRM_API3_Exception
1524 public static function create(&$params, $ids = []) {
1526 if (empty($params['id']) && (array_filter($ids) !== [])) {
1527 $params['id'] = $ids['mailing_id'] ??
$ids['id'];
1528 CRM_Core_Error
::deprecatedWarning('Parameter $ids is no longer used by Mailing::create. Use the api or just pass $params');
1532 // If it is a mass sms, set url_tracking to false
1533 if (!empty($params['sms_provider_id'])) {
1534 $params['url_tracking'] = 0;
1538 // Do the below only for an insert
1539 // for an update, we should not set the defaults
1540 if (!isset($params['id'])) {
1541 // Retrieve domain email and name for default sender
1542 $domain = civicrm_api(
1547 'current_domain' => 1,
1551 if (isset($domain['from_email'])) {
1552 $domain_email = $domain['from_email'];
1553 $domain_name = $domain['from_name'];
1556 $domain_email = 'info@EXAMPLE.ORG';
1557 $domain_name = 'EXAMPLE.ORG';
1559 if (!isset($params['created_id'])) {
1560 $params['created_id'] = CRM_Core_Session
::getLoggedInContactID();
1563 // load the default config settings for each
1564 // eg reply_id, unsubscribe_id need to use
1565 // correct template IDs here
1566 'override_verp' => TRUE,
1567 'forward_replies' => FALSE,
1568 'open_tracking' => Civi
::settings()->get('open_tracking_default'),
1569 'url_tracking' => Civi
::settings()->get('url_tracking_default'),
1570 'visibility' => 'Public Pages',
1571 'replyto_email' => $domain_email,
1572 'header_id' => CRM_Mailing_PseudoConstant
::defaultComponent('header_id', ''),
1573 'footer_id' => CRM_Mailing_PseudoConstant
::defaultComponent('footer_id', ''),
1574 'from_email' => $domain_email,
1575 'from_name' => $domain_name,
1576 'msg_template_id' => NULL,
1577 'created_id' => $params['created_id'],
1578 'approver_id' => NULL,
1579 'auto_responder' => 0,
1580 'created_date' => date('YmdHis'),
1581 'scheduled_date' => NULL,
1582 'approval_date' => NULL,
1585 // Get the default from email address, if not provided.
1586 if (empty($defaults['from_email'])) {
1587 $defaultAddress = CRM_Core_BAO_Domain
::getNameAndEmail(TRUE, TRUE);
1588 foreach ($defaultAddress as $id => $value) {
1589 if (preg_match('/"(.*)" <(.*)>/', $value, $match)) {
1590 $defaults['from_email'] = $match[2];
1591 $defaults['from_name'] = $match[1];
1596 $params = array_merge($defaults, $params);
1600 * Could check and warn for the following cases:
1602 * - groups OR mailings should be populated.
1603 * - body html OR body text should be populated.
1606 $transaction = new CRM_Core_Transaction();
1608 $mailing = self
::add($params);
1610 if (is_a($mailing, 'CRM_Core_Error')) {
1611 $transaction->rollback();
1614 // update mailings with hash values
1615 CRM_Contact_BAO_Contact_Utils
::generateChecksum($mailing->id
, NULL, NULL, NULL, 'mailing', 16);
1617 $groupTableName = CRM_Contact_BAO_Group
::getTableName();
1619 /* Create the mailing group record */
1620 $mg = new CRM_Mailing_DAO_MailingGroup();
1622 'include' => 'Include',
1623 'exclude' => 'Exclude',
1626 foreach (['groups', 'mailings'] as $entity) {
1627 foreach (['include', 'exclude', 'base'] as $type) {
1628 if (isset($params[$entity][$type])) {
1629 self
::replaceGroups($mailing->id
, $groupTypes[$type], $entity, $params[$entity][$type]);
1634 if (!empty($params['search_id']) && !empty($params['group_id'])) {
1636 $mg->mailing_id
= $mailing->id
;
1637 $mg->entity_table
= $groupTableName;
1638 $mg->entity_id
= $params['group_id'];
1639 $mg->search_id
= $params['search_id'];
1640 $mg->search_args
= $params['search_args'];
1641 $mg->group_type
= 'Include';
1645 // check and attach and files as needed
1646 CRM_Core_BAO_File
::processAttachment($params, 'civicrm_mailing', $mailing->id
);
1648 // If we're going to autosend, then check validity before saving.
1649 if (empty($params['is_completed']) && !empty($params['scheduled_date']) && $params['scheduled_date'] != 'null' && !empty($params['_evil_bao_validator_'])) {
1650 $cb = Civi\Core\Resolver
::singleton()
1651 ->get($params['_evil_bao_validator_']);
1652 $errors = call_user_func($cb, $mailing);
1653 if (!empty($errors)) {
1654 $fields = implode(',', array_keys($errors));
1655 throw new CRM_Core_Exception("Mailing cannot be sent. There are missing or invalid fields ($fields).", 'cannot-send', $errors);
1659 $transaction->commit();
1661 // Create parent job if not yet created.
1662 // Condition on the existence of a scheduled date.
1663 if (!empty($params['scheduled_date']) && $params['scheduled_date'] != 'null' && empty($params['_skip_evil_bao_auto_schedule_'])) {
1664 $job = new CRM_Mailing_BAO_MailingJob();
1665 $job->mailing_id
= $mailing->id
;
1666 // If we are creating a new Completed mailing (e.g. import from another system) set the job to completed.
1667 // Keeping former behaviour when an id is present is precautionary and may warrant reconsideration later.
1668 $job->status
= ((empty($params['is_completed']) ||
!empty($params['id'])) ?
'Scheduled' : 'Complete');
1671 if (!$job->find(TRUE)) {
1672 // Don't schedule job until we populate the recipients.
1673 $job->scheduled_date
= NULL;
1676 // Schedule the job now that it has recipients.
1677 $job->scheduled_date
= $params['scheduled_date'];
1681 // Populate the recipients.
1682 if (empty($params['_skip_evil_bao_auto_recipients_'])) {
1683 self
::getRecipients($mailing->id
);
1691 * This is used by CiviMail but will be made redundant by FlexMailer.
1692 * @param CRM_Mailing_DAO_Mailing $mailing
1693 * The mailing which may or may not be sendable.
1695 * List of error messages.
1697 public static function checkSendable($mailing) {
1699 foreach (['subject', 'name', 'from_name', 'from_email'] as $field) {
1700 if (empty($mailing->{$field})) {
1701 $errors[$field] = ts('Field "%1" is required.', [
1706 if (empty($mailing->body_html
) && empty($mailing->body_text
)) {
1707 $errors['body'] = ts('Field "body_html" or "body_text" is required.');
1710 if (!Civi
::settings()->get('disable_mandatory_tokens_check')) {
1711 $header = $mailing->header_id
&& $mailing->header_id
!= 'null' ? CRM_Mailing_BAO_MailingComponent
::findById($mailing->header_id
) : NULL;
1712 $footer = $mailing->footer_id
&& $mailing->footer_id
!= 'null' ? CRM_Mailing_BAO_MailingComponent
::findById($mailing->footer_id
) : NULL;
1713 foreach (['body_html', 'body_text'] as $field) {
1714 if (empty($mailing->{$field})) {
1717 $str = ($header ?
$header->{$field} : '') . $mailing->{$field} . ($footer ?
$footer->{$field} : '');
1718 $err = CRM_Utils_Token
::requiredTokens($str);
1719 if ($err !== TRUE) {
1720 foreach ($err as $token => $desc) {
1721 $errors["{$field}:{$token}"] = ts('This message is missing a required token - {%1}: %2',
1722 [1 => $token, 2 => $desc]
1733 * Replace the list of recipients on a given mailing.
1735 * @param int $mailingId
1736 * @param string $type
1737 * 'include' or 'exclude'.
1738 * @param string $entity
1739 * 'groups' or 'mailings'.
1740 * @param array $entityIds
1741 * @throws CiviCRM_API3_Exception
1743 public static function replaceGroups($mailingId, $type, $entity, $entityIds) {
1745 foreach ($entityIds as $entityId) {
1746 $values[] = ['entity_id' => $entityId];
1748 civicrm_api3('mailing_group', 'replace', [
1749 'mailing_id' => $mailingId,
1750 'group_type' => $type,
1751 'entity_table' => ($entity == 'groups') ? CRM_Contact_BAO_Group
::getTableName() : CRM_Mailing_BAO_Mailing
::getTableName(),
1752 'values' => $values,
1757 * Get hash value of the mailing.
1761 * @return null|string
1763 public static function getMailingHash($id) {
1765 if (Civi
::settings()->get('hash_mailing_url') && !empty($id)) {
1766 $hash = CRM_Core_DAO
::getFieldValue('CRM_Mailing_BAO_Mailing', $id, 'hash', 'id');
1772 * Generate a report. Fetch event count information, mailing data, and job
1776 * The mailing id to report.
1777 * @param bool $skipDetails
1778 * Whether return all detailed report.
1780 * @param bool $isSMS
1783 * Associative array of reporting data
1785 public static function &report($id, $skipDetails = FALSE, $isSMS = FALSE) {
1786 $mailing_id = CRM_Utils_Type
::escape($id, 'Integer');
1788 $mailing = new CRM_Mailing_BAO_Mailing();
1791 'mailing' => self
::getTableName(),
1792 'mailing_group' => CRM_Mailing_DAO_MailingGroup
::getTableName(),
1793 'group' => CRM_Contact_BAO_Group
::getTableName(),
1794 'job' => CRM_Mailing_BAO_MailingJob
::getTableName(),
1795 'queue' => CRM_Mailing_Event_BAO_Queue
::getTableName(),
1796 'delivered' => CRM_Mailing_Event_BAO_Delivered
::getTableName(),
1797 'opened' => CRM_Mailing_Event_BAO_Opened
::getTableName(),
1798 'reply' => CRM_Mailing_Event_BAO_Reply
::getTableName(),
1799 'unsubscribe' => CRM_Mailing_Event_BAO_Unsubscribe
::getTableName(),
1800 'bounce' => CRM_Mailing_Event_BAO_Bounce
::getTableName(),
1801 'forward' => CRM_Mailing_Event_BAO_Forward
::getTableName(),
1802 'url' => CRM_Mailing_BAO_TrackableURL
::getTableName(),
1803 'urlopen' => CRM_Mailing_Event_BAO_TrackableURLOpen
::getTableName(),
1804 'component' => CRM_Mailing_BAO_MailingComponent
::getTableName(),
1805 'spool' => CRM_Mailing_BAO_Spool
::getTableName(),
1809 $additionalWhereClause = " AND ";
1811 $additionalWhereClause .= " {$t['mailing']}.sms_provider_id IS NULL ";
1814 $additionalWhereClause .= " {$t['mailing']}.sms_provider_id IS NOT NULL ";
1817 /* Get the mailing info */
1820 SELECT {$t['mailing']}.*
1821 FROM {$t['mailing']}
1822 WHERE {$t['mailing']}.id = $mailing_id {$additionalWhereClause}");
1826 $report['mailing'] = [];
1827 foreach (array_keys(self
::fields()) as $field) {
1828 $field = self
::fields()[$field]['name'];
1829 $report['mailing'][$field] = $mailing->$field;
1833 if ($campaignId = CRM_Utils_Array
::value('campaign_id', $report['mailing'])) {
1834 $campaigns = CRM_Campaign_BAO_Campaign
::getCampaigns($campaignId);
1835 $report['mailing']['campaign'] = $campaigns[$campaignId];
1838 //mailing report is called by activity
1839 //we dont need all detail report
1844 /* Get the component info */
1849 'header' => ts('Header'),
1850 'footer' => ts('Footer'),
1851 'reply' => ts('Reply'),
1852 'optout' => ts('Opt-Out'),
1853 'resubscribe' => ts('Resubscribe'),
1854 'unsubscribe' => ts('Unsubscribe'),
1856 foreach (array_keys($components) as $type) {
1857 $query[] = "SELECT {$t['component']}.name as name,
1859 {$t['component']}.id as id
1860 FROM {$t['component']}
1861 INNER JOIN {$t['mailing']}
1862 ON {$t['mailing']}.{$type}_id =
1863 {$t['component']}.id
1864 WHERE {$t['mailing']}.id = $mailing_id";
1866 $q = '(' . implode(') UNION (', $query) . ')';
1867 $mailing->query($q);
1869 $report['component'] = [];
1870 while ($mailing->fetch()) {
1871 $report['component'][] = [
1872 'type' => $components[$mailing->type
],
1873 'name' => $mailing->name
,
1874 'link' => CRM_Utils_System
::url('civicrm/mailing/component', "reset=1&action=update&id={$mailing->id}"),
1878 /* Get the recipient group info */
1881 SELECT {$t['mailing_group']}.group_type as group_type,
1882 {$t['group']}.id as group_id,
1883 {$t['group']}.title as group_title,
1884 {$t['group']}.is_hidden as group_hidden,
1885 {$t['mailing']}.id as mailing_id,
1886 {$t['mailing']}.name as mailing_name
1887 FROM {$t['mailing_group']}
1888 LEFT JOIN {$t['group']}
1889 ON {$t['mailing_group']}.entity_id = {$t['group']}.id
1890 AND {$t['mailing_group']}.entity_table =
1892 LEFT JOIN {$t['mailing']}
1893 ON {$t['mailing_group']}.entity_id =
1895 AND {$t['mailing_group']}.entity_table =
1898 WHERE {$t['mailing_group']}.mailing_id = $mailing_id
1901 $report['group'] = ['include' => [], 'exclude' => [], 'base' => []];
1902 while ($mailing->fetch()) {
1904 if (isset($mailing->group_id
)) {
1905 $row['id'] = $mailing->group_id
;
1906 $row['name'] = $mailing->group_title
;
1907 $row['link'] = CRM_Utils_System
::url('civicrm/group/search',
1908 "reset=1&force=1&context=smog&gid={$row['id']}"
1912 $row['id'] = $mailing->mailing_id
;
1913 $row['name'] = $mailing->mailing_name
;
1914 $row['mailing'] = TRUE;
1915 $row['link'] = CRM_Utils_System
::url('civicrm/mailing/report',
1920 /* Rename hidden groups */
1922 if ($mailing->group_hidden
== 1) {
1923 $row['name'] = "Search Results";
1926 if ($mailing->group_type
== 'Include') {
1927 $report['group']['include'][] = $row;
1929 elseif ($mailing->group_type
== 'Base') {
1930 $report['group']['base'][] = $row;
1933 $report['group']['exclude'][] = $row;
1937 /* Get the event totals, grouped by job (retries) */
1940 SELECT {$t['job']}.*,
1941 COUNT(DISTINCT {$t['queue']}.id) as queue,
1942 COUNT(DISTINCT {$t['delivered']}.id) as delivered,
1943 COUNT(DISTINCT {$t['reply']}.id) as reply,
1944 COUNT(DISTINCT {$t['forward']}.id) as forward,
1945 COUNT(DISTINCT {$t['bounce']}.id) as bounce,
1946 COUNT(DISTINCT {$t['urlopen']}.id) as url,
1947 COUNT(DISTINCT {$t['spool']}.id) as spool
1949 LEFT JOIN {$t['queue']}
1950 ON {$t['queue']}.job_id = {$t['job']}.id
1951 LEFT JOIN {$t['reply']}
1952 ON {$t['reply']}.event_queue_id = {$t['queue']}.id
1953 LEFT JOIN {$t['forward']}
1954 ON {$t['forward']}.event_queue_id = {$t['queue']}.id
1955 LEFT JOIN {$t['bounce']}
1956 ON {$t['bounce']}.event_queue_id = {$t['queue']}.id
1957 LEFT JOIN {$t['delivered']}
1958 ON {$t['delivered']}.event_queue_id = {$t['queue']}.id
1959 AND {$t['bounce']}.id IS null
1960 LEFT JOIN {$t['urlopen']}
1961 ON {$t['urlopen']}.event_queue_id = {$t['queue']}.id
1962 LEFT JOIN {$t['spool']}
1963 ON {$t['spool']}.job_id = {$t['job']}.id
1964 WHERE {$t['job']}.mailing_id = $mailing_id
1965 AND {$t['job']}.is_test = 0
1966 GROUP BY {$t['job']}.id");
1968 $report['jobs'] = [];
1969 $report['event_totals'] = [];
1970 $path = 'civicrm/mailing/report/event';
1985 // initialize various counters
1986 foreach ($elements as $field) {
1987 $report['event_totals'][$field] = 0;
1990 while ($mailing->fetch()) {
1992 foreach ($elements as $field) {
1993 if (isset($mailing->$field)) {
1994 $row[$field] = $mailing->$field;
1995 $report['event_totals'][$field] +
= $mailing->$field;
1999 // compute open total separately to discount duplicates
2001 $row['opened'] = CRM_Mailing_Event_BAO_Opened
::getTotalCount($mailing_id, $mailing->id
, TRUE);
2002 $report['event_totals']['opened'] +
= $row['opened'];
2003 $row['total_opened'] = CRM_Mailing_Event_BAO_Opened
::getTotalCount($mailing_id, $mailing->id
);
2004 $report['event_totals']['total_opened'] +
= $row['total_opened'];
2006 // compute unsub total separately to discount duplicates
2008 $row['unsubscribe'] = CRM_Mailing_Event_BAO_Unsubscribe
::getTotalCount($mailing_id, $mailing->id
, TRUE, TRUE);
2009 $report['event_totals']['unsubscribe'] +
= $row['unsubscribe'];
2011 $row['optout'] = CRM_Mailing_Event_BAO_Unsubscribe
::getTotalCount($mailing_id, $mailing->id
, TRUE, FALSE);
2012 $report['event_totals']['optout'] +
= $row['optout'];
2014 foreach (array_keys(CRM_Mailing_BAO_MailingJob
::fields()) as $field) {
2015 // Get the field name from the MailingJob fields as that will not have any prefixing.
2017 $field = CRM_Mailing_BAO_MailingJob
::fields()[$field]['name'];
2018 $row[$field] = $mailing->$field;
2021 if ($mailing->queue
) {
2022 $row['delivered_rate'] = (100.0 * $mailing->delivered
) / $mailing->queue
;
2023 $row['bounce_rate'] = (100.0 * $mailing->bounce
) / $mailing->queue
;
2024 $row['unsubscribe_rate'] = (100.0 * $row['unsubscribe']) / $mailing->queue
;
2025 $row['optout_rate'] = (100.0 * $row['optout']) / $mailing->queue
;
2026 $row['opened_rate'] = $mailing->delivered ?
(($row['opened'] / $mailing->delivered
) * 100.0) : 0;
2027 $row['clickthrough_rate'] = $mailing->delivered ?
(($mailing->url
/ $mailing->delivered
) * 100.0) : 0;
2030 $row['delivered_rate'] = 0;
2031 $row['bounce_rate'] = 0;
2032 $row['unsubscribe_rate'] = 0;
2033 $row['optout_rate'] = 0;
2034 $row['opened_rate'] = 0;
2035 $row['clickthrough_rate'] = 0;
2038 $arg = "reset=1&mid=$mailing_id&jid={$mailing->id}";
2040 'clicks' => CRM_Utils_System
::url($path, "$arg&event=click"),
2041 'queue' => CRM_Utils_System
::url($path, "$arg&event=queue"),
2042 'delivered' => CRM_Utils_System
::url($path, "$arg&event=delivered"),
2043 'bounce' => CRM_Utils_System
::url($path, "$arg&event=bounce"),
2044 'unsubscribe' => CRM_Utils_System
::url($path, "$arg&event=unsubscribe"),
2045 'forward' => CRM_Utils_System
::url($path, "$arg&event=forward"),
2046 'reply' => CRM_Utils_System
::url($path, "$arg&event=reply"),
2047 'opened' => CRM_Utils_System
::url($path, "$arg&event=opened"),
2050 foreach (['scheduled_date', 'start_date', 'end_date'] as $key) {
2051 $row[$key] = CRM_Utils_Date
::customFormat($row[$key]);
2053 $report['jobs'][] = $row;
2056 $newTableSize = CRM_Mailing_BAO_Recipients
::mailingSize($mailing_id);
2058 // we need to do this for backward compatibility, since old mailings did not
2059 // use the mailing_recipients table
2060 if ($newTableSize > 0) {
2061 $report['event_totals']['queue'] = $newTableSize;
2064 $report['event_totals']['queue'] = self
::getRecipientsCount($mailing_id);
2067 if (!empty($report['event_totals']['queue'])) {
2068 $report['event_totals']['delivered_rate'] = (100.0 * $report['event_totals']['delivered']) / $report['event_totals']['queue'];
2069 $report['event_totals']['bounce_rate'] = (100.0 * $report['event_totals']['bounce']) / $report['event_totals']['queue'];
2070 $report['event_totals']['unsubscribe_rate'] = (100.0 * $report['event_totals']['unsubscribe']) / $report['event_totals']['queue'];
2071 $report['event_totals']['optout_rate'] = (100.0 * $report['event_totals']['optout']) / $report['event_totals']['queue'];
2072 $report['event_totals']['opened_rate'] = !empty($report['event_totals']['delivered']) ?
(($report['event_totals']['opened'] / $report['event_totals']['delivered']) * 100.0) : 0;
2073 $report['event_totals']['clickthrough_rate'] = !empty($report['event_totals']['delivered']) ?
(($report['event_totals']['url'] / $report['event_totals']['delivered']) * 100.0) : 0;
2076 $report['event_totals']['delivered_rate'] = 0;
2077 $report['event_totals']['bounce_rate'] = 0;
2078 $report['event_totals']['unsubscribe_rate'] = 0;
2079 $report['event_totals']['optout_rate'] = 0;
2080 $report['event_totals']['opened_rate'] = 0;
2081 $report['event_totals']['clickthrough_rate'] = 0;
2084 /* Get the click-through totals, grouped by URL */
2087 SELECT {$t['url']}.url,
2089 COUNT({$t['urlopen']}.id) as clicks,
2090 COUNT(DISTINCT {$t['queue']}.id) as unique_clicks
2092 LEFT JOIN {$t['urlopen']}
2093 ON {$t['urlopen']}.trackable_url_id = {$t['url']}.id
2094 LEFT JOIN {$t['queue']}
2095 ON {$t['urlopen']}.event_queue_id = {$t['queue']}.id
2096 LEFT JOIN {$t['job']}
2097 ON {$t['queue']}.job_id = {$t['job']}.id
2098 WHERE {$t['url']}.mailing_id = $mailing_id
2099 AND {$t['job']}.is_test = 0
2100 GROUP BY {$t['url']}.id
2101 ORDER BY unique_clicks DESC");
2103 $report['click_through'] = [];
2105 while ($mailing->fetch()) {
2106 $report['click_through'][] = [
2107 'url' => $mailing->url
,
2108 'link' => CRM_Utils_System
::url($path, "reset=1&event=click&mid=$mailing_id&uid={$mailing->id}"),
2109 'link_unique' => CRM_Utils_System
::url($path, "reset=1&event=click&mid=$mailing_id&uid={$mailing->id}&distinct=1"),
2110 'clicks' => $mailing->clicks
,
2111 'unique' => $mailing->unique_clicks
,
2112 'rate' => !empty($report['event_totals']['delivered']) ?
(100.0 * $mailing->unique_clicks
) / $report['event_totals']['delivered'] : 0,
2113 'report' => CRM_Report_Utils_Report
::getNextUrl('mailing/clicks', "reset=1&mailing_id_value={$mailing_id}&url_value={$mailing->url}", FALSE, TRUE),
2117 $arg = "reset=1&mid=$mailing_id";
2118 $report['event_totals']['links'] = [
2119 'clicks' => CRM_Utils_System
::url($path, "$arg&event=click"),
2120 'clicks_unique' => CRM_Utils_System
::url($path, "$arg&event=click&distinct=1"),
2121 'queue' => CRM_Utils_System
::url($path, "$arg&event=queue"),
2122 'delivered' => CRM_Utils_System
::url($path, "$arg&event=delivered"),
2123 'bounce' => CRM_Utils_System
::url($path, "$arg&event=bounce"),
2124 'unsubscribe' => CRM_Utils_System
::url($path, "$arg&event=unsubscribe"),
2125 'optout' => CRM_Utils_System
::url($path, "$arg&event=optout"),
2126 'forward' => CRM_Utils_System
::url($path, "$arg&event=forward"),
2127 'reply' => CRM_Utils_System
::url($path, "$arg&event=reply"),
2128 'opened' => CRM_Utils_System
::url($path, "$arg&event=opened"),
2131 $actionLinks = [CRM_Core_Action
::VIEW
=> ['name' => ts('Report')]];
2132 $actionLinks[CRM_Core_Action
::ADVANCED
] = [
2133 'name' => ts('Advanced Search'),
2134 'url' => 'civicrm/contact/search/advanced',
2136 $action = array_sum(array_keys($actionLinks));
2138 $report['event_totals']['actionlinks'] = [];
2139 foreach (['clicks', 'clicks_unique', 'queue', 'delivered', 'bounce', 'unsubscribe', 'forward', 'reply', 'opened', 'opened_unique', 'optout'] as $key) {
2140 $url = 'mailing/detail';
2141 $reportFilter = "reset=1&mailing_id_value={$mailing_id}";
2142 $searchFilter = "force=1&mailing_id=%%mid%%";
2145 $reportFilter .= "&delivery_status_value=successful";
2146 $searchFilter .= "&mailing_delivery_status=Y";
2150 $url = "mailing/bounce";
2151 $searchFilter .= "&mailing_delivery_status=N";
2155 $reportFilter .= "&is_forwarded_value=1";
2156 $searchFilter .= "&mailing_forward=1";
2160 $reportFilter .= "&is_replied_value=1";
2161 $searchFilter .= "&mailing_reply_status=Y";
2165 $reportFilter .= "&is_unsubscribed_value=1";
2166 $searchFilter .= "&mailing_unsubscribe=1";
2170 $reportFilter .= "&is_optout_value=1";
2171 $searchFilter .= "&mailing_optout=1";
2175 // do not use group by clause in report, because same report used for total and unique open
2176 $reportFilter .= "&distinct=0";
2177 case 'opened_unique':
2178 $url = "mailing/opened";
2179 $searchFilter .= "&mailing_open_status=Y";
2183 case 'clicks_unique':
2184 $url = "mailing/clicks";
2185 $searchFilter .= "&mailing_click_status=Y";
2188 $actionLinks[CRM_Core_Action
::VIEW
]['url'] = CRM_Report_Utils_Report
::getNextUrl($url, $reportFilter, FALSE, TRUE);
2189 if (array_key_exists(CRM_Core_Action
::ADVANCED
, $actionLinks)) {
2190 $actionLinks[CRM_Core_Action
::ADVANCED
]['qs'] = $searchFilter;
2192 $report['event_totals']['actionlinks'][$key] = CRM_Core_Action
::formLink(
2195 ['mid' => $mailing_id],
2198 'mailing.report.action',
2208 * Get the count of mailings.
2215 public function getCount() {
2217 $this->selectAdd('COUNT(id) as count');
2219 $session = CRM_Core_Session
::singleton();
2222 return $this->count
;
2230 public static function checkPermission($id) {
2235 $mailingIDs = self
::mailingACLIDs();
2236 if ($mailingIDs === TRUE) {
2240 if (!in_array($id, $mailingIDs)) {
2241 throw new CRM_Core_Exception(ts('You do not have permission to access this mailing report'));
2246 * @param null $alias
2250 public static function mailingACL($alias = NULL) {
2251 $mailingACL = " ( 0 ) ";
2253 $mailingIDs = self
::mailingACLIDs();
2254 if ($mailingIDs === TRUE) {
2258 if (!empty($mailingIDs)) {
2259 $mailingIDs = implode(',', $mailingIDs);
2260 $tableName = !$alias ? self
::getTableName() : $alias;
2261 $mailingACL = " $tableName.id IN ( $mailingIDs ) ";
2267 * Returns all the mailings that this user can access. This is dependent on
2268 * all the groups that the user has access to.
2269 * However since most civi installs dont use ACL's we special case the condition
2270 * where the user has access to ALL groups, and hence ALL mailings and return a
2271 * value of TRUE (to avoid the downstream where clause with a list of mailing list IDs
2273 * @return bool|array
2274 * TRUE if the user has access to all mailings, else array of mailing IDs (possibly empty).
2276 public static function mailingACLIDs() {
2278 // optimize common case where admin has access
2281 CRM_Core_Permission
::check('view all contacts') ||
2282 CRM_Core_Permission
::check('edit all contacts')
2289 // get all the groups that this user can access
2290 // if they dont have universal access
2291 $groupNames = civicrm_api3('Group', 'get', [
2292 'check_permissions' => TRUE,
2293 'return' => ['title', 'id'],
2294 'options' => ['limit' => 0],
2296 foreach ($groupNames['values'] as $group) {
2297 $groups[$group['id']] = $group['title'];
2299 if (!empty($groups)) {
2300 $groupIDs = implode(',', array_keys($groups));
2301 $domain_id = CRM_Core_Config
::domainID();
2303 // get all the mailings that are in this subset of groups
2305 SELECT DISTINCT( m.id ) as id
2306 FROM civicrm_mailing m
2307 LEFT JOIN civicrm_mailing_group g ON g.mailing_id = m.id
2308 WHERE ( ( g.entity_table like 'civicrm_group%' AND g.entity_id IN ( $groupIDs ) )
2309 OR ( g.entity_table IS NULL AND g.entity_id IS NULL AND m.domain_id = $domain_id ) )
2311 $dao = CRM_Core_DAO
::executeQuery($query);
2314 while ($dao->fetch()) {
2315 $mailingIDs[] = $dao->id
;
2317 //CRM-18181 Get all mailings that use the mailings found earlier as receipients
2318 if (!empty($mailingIDs)) {
2319 $mailings = implode(',', $mailingIDs);
2321 SELECT DISTINCT ( m.id ) as id
2322 FROM civicrm_mailing m
2323 LEFT JOIN civicrm_mailing_group g ON g.mailing_id = m.id
2324 WHERE g.entity_table like 'civicrm_mailing%' AND g.entity_id IN ($mailings)";
2325 $mailingDao = CRM_Core_DAO
::executeQuery($mailingQuery);
2326 while ($mailingDao->fetch()) {
2327 $mailingIDs[] = $mailingDao->id
;
2336 * Get the rows for a browse operation.
2338 * @param int $offset
2339 * The row number to start from.
2340 * @param int $rowCount
2341 * The nmber of rows to return.
2342 * @param string $sort
2343 * The sql string that describes the sort order.
2345 * @param null $additionalClause
2346 * @param array $additionalParams
2351 public function &getRows($offset, $rowCount, $sort, $additionalClause = NULL, $additionalParams = NULL) {
2352 $mailing = self
::getTableName();
2353 $job = CRM_Mailing_BAO_MailingJob
::getTableName();
2354 $group = CRM_Mailing_DAO_MailingGroup
::getTableName();
2355 $session = CRM_Core_Session
::singleton();
2357 $mailingACL = self
::mailingACL();
2359 //get all campaigns.
2360 $allCampaigns = CRM_Campaign_BAO_Campaign
::getCampaigns(NULL, NULL, FALSE, FALSE, FALSE, TRUE);
2365 "$mailing.approval_status_id",
2366 "createdContact.sort_name as created_by",
2367 "scheduledContact.sort_name as scheduled_by",
2368 "$mailing.created_id as created_id",
2369 "$mailing.scheduled_id as scheduled_id",
2370 "$mailing.is_archived as archived",
2371 "$mailing.created_date as created_date",
2373 "$mailing.sms_provider_id as sms_provider_id",
2374 "$mailing.language",
2377 // we only care about parent jobs, since that holds all the info on
2379 $selectClause = implode(', ', $select);
2380 $groupFromSelect = CRM_Contact_BAO_Query
::getGroupByFromSelectColumns($select, "$mailing.id");
2382 SELECT {$selectClause},
2383 MIN($job.scheduled_date) as scheduled_date,
2384 MIN($job.start_date) as start_date,
2385 MAX($job.end_date) as end_date
2387 LEFT JOIN $job ON ( $job.mailing_id = $mailing.id AND $job.is_test = 0 AND $job.parent_id IS NULL )
2388 LEFT JOIN civicrm_contact createdContact ON ( civicrm_mailing.created_id = createdContact.id )
2389 LEFT JOIN civicrm_contact scheduledContact ON ( civicrm_mailing.scheduled_id = scheduledContact.id )
2390 WHERE $mailingACL $additionalClause";
2392 if (!empty($groupFromSelect)) {
2393 $query .= $groupFromSelect;
2397 $orderBy = trim($sort->orderBy());
2398 if (!empty($orderBy)) {
2399 $query .= " ORDER BY $orderBy";
2404 $offset = CRM_Utils_Type
::escape($offset, 'Int');
2405 $rowCount = CRM_Utils_Type
::escape($rowCount, 'Int');
2407 $query .= " LIMIT $offset, $rowCount ";
2410 if (!$additionalParams) {
2411 $additionalParams = [];
2414 $dao = CRM_Core_DAO
::executeQuery($query, $additionalParams);
2417 while ($dao->fetch()) {
2420 'name' => $dao->name
,
2421 'status' => $dao->status ?
$dao->status
: 'Not scheduled',
2422 'created_date' => CRM_Utils_Date
::customFormat($dao->created_date
),
2423 'scheduled' => CRM_Utils_Date
::customFormat($dao->scheduled_date
),
2424 'scheduled_iso' => $dao->scheduled_date
,
2425 'start' => CRM_Utils_Date
::customFormat($dao->start_date
),
2426 'end' => CRM_Utils_Date
::customFormat($dao->end_date
),
2427 'created_by' => $dao->created_by
,
2428 'scheduled_by' => $dao->scheduled_by
,
2429 'created_id' => $dao->created_id
,
2430 'scheduled_id' => $dao->scheduled_id
,
2431 'archived' => $dao->archived
,
2432 'approval_status_id' => $dao->approval_status_id
,
2433 'campaign_id' => $dao->campaign_id
,
2434 'campaign' => empty($dao->campaign_id
) ?
NULL : $allCampaigns[$dao->campaign_id
],
2435 'sms_provider_id' => $dao->sms_provider_id
,
2436 'language' => $dao->language
,
2443 * Show detail Mailing report.
2449 public static function showEmailDetails($id) {
2450 return CRM_Utils_System
::url('civicrm/mailing/report', "mid=$id");
2454 * Delete Mails and all its associated records.
2457 * Id of the mail to delete.
2461 public static function del($id) {
2463 throw new CRM_Core_Exception(ts('No id passed to mailing del function'));
2466 CRM_Utils_Hook
::pre('delete', 'Mailing', $id);
2468 // delete all file attachments
2469 CRM_Core_BAO_File
::deleteEntityFile('civicrm_mailing',
2473 $dao = new CRM_Mailing_DAO_Mailing();
2477 CRM_Core_Session
::setStatus(ts('Selected mailing has been deleted.'), ts('Deleted'), 'success');
2479 CRM_Utils_Hook
::post('delete', 'Mailing', $id, $dao);
2484 * Use CRM_Mailing_BAO_MailingJob::del($id)
2487 * Id of the Job to delete.
2491 public static function delJob($id) {
2493 throw new CRM_Core_Exception(ts('No id passed to mailing delJob function'));
2496 CRM_Core_Error
::deprecatedWarning('This function is deprecated, use CRM_Mailing_BAO_MailingJob::del instead');
2498 CRM_Mailing_BAO_MailingJob
::del($id);
2503 * This is used by CiviMail but will be made redundant by FlexMailer/TokenProcessor.
2506 public function getReturnProperties() {
2507 $tokens = &$this->getTokens();
2510 if (isset($tokens['html']) &&
2511 isset($tokens['html']['contact'])
2513 $properties = array_merge($properties, $tokens['html']['contact']);
2516 if (isset($tokens['text']) &&
2517 isset($tokens['text']['contact'])
2519 $properties = array_merge($properties, $tokens['text']['contact']);
2522 if (isset($tokens['subject']) &&
2523 isset($tokens['subject']['contact'])
2525 $properties = array_merge($properties, $tokens['subject']['contact']);
2528 $returnProperties = [];
2529 $returnProperties['display_name'] = $returnProperties['contact_id'] = $returnProperties['preferred_mail_format'] = $returnProperties['hash'] = 1;
2531 foreach ($properties as $p) {
2532 $returnProperties[$p] = 1;
2535 return $returnProperties;
2539 * Build the compose mail form.
2541 * @param CRM_Core_Form $form
2545 public static function commonCompose(&$form) {
2549 if (method_exists($form, 'listTokens')) {
2550 $tokens = array_merge($form->listTokens(), $tokens);
2553 //sorted in ascending order tokens by ignoring word case
2554 $form->assign('tokens', CRM_Utils_Token
::formatTokensForDisplay($tokens));
2559 'text_message' => ts('HTML Format'),
2560 'sms_text_message' => ts('SMS Message'),
2562 $modePrefixes = ['Mail' => NULL, 'SMS' => 'SMS'];
2564 $className = CRM_Utils_System
::getClassName($form);
2566 if ($className != 'CRM_SMS_Form_Upload' && $className != 'CRM_Contact_Form_Task_SMS' &&
2567 $className != 'CRM_Contact_Form_Task_SMS'
2569 $form->add('wysiwyg', 'html_message',
2570 strstr($className, 'PDF') ?
ts('Document Body') : ts('HTML Format'),
2574 'onkeyup' => "return verify(this)",
2578 if ($className != 'CRM_Admin_Form_ScheduleReminders') {
2579 unset($modePrefixes['SMS']);
2583 unset($textFields['text_message']);
2584 unset($modePrefixes['Mail']);
2587 //insert message Text by selecting "Select Template option"
2588 foreach ($textFields as $id => $label) {
2590 if ($id == 'sms_text_message') {
2592 $form->assign('max_sms_length', CRM_SMS_Provider
::MAX_SMS_CHAR
);
2594 $form->add('textarea', $id, $label,
2598 'onkeyup' => "return verify(this, '{$prefix}')",
2603 foreach ($modePrefixes as $prefix) {
2604 if ($prefix == 'SMS') {
2605 $templates[$prefix] = CRM_Core_BAO_MessageTemplate
::getMessageTemplates(FALSE, TRUE);
2608 $templates[$prefix] = CRM_Core_BAO_MessageTemplate
::getMessageTemplates(FALSE);
2610 if (!empty($templates[$prefix])) {
2611 $form->assign('templates', TRUE);
2613 $form->add('select', "{$prefix}template", ts('Use Template'),
2614 ['' => ts('- select -')] +
$templates[$prefix], FALSE,
2615 ['onChange' => "selectValue( this.value, '{$prefix}');"]
2618 $form->add('checkbox', "{$prefix}updateTemplate", ts('Update Template'), NULL);
2620 $form->add('checkbox', "{$prefix}saveTemplate", ts('Save As New Template'), NULL, FALSE,
2621 ['onclick' => "showSaveDetails(this, '{$prefix}');"]
2623 $form->add('text', "{$prefix}saveTemplateName", ts('Template Title'));
2626 // I'm not sure this is ever called.
2627 $action = CRM_Utils_Request
::retrieve('action', 'String', $form, FALSE);
2628 if ((CRM_Utils_System
::getClassName($form) == 'CRM_Contact_Form_Task_PDF') &&
2629 $action == CRM_Core_Action
::VIEW
2631 $form->freeze('html_message');
2636 * Get the search based mailing Ids.
2639 * , searched base mailing ids.
2641 public function searchMailingIDs() {
2642 $group = CRM_Mailing_DAO_MailingGroup
::getTableName();
2643 $mailing = self
::getTableName();
2646 SELECT $mailing.id as mailing_id
2647 FROM $mailing, $group
2648 WHERE $group.mailing_id = $mailing.id
2649 AND $group.group_type = 'Base'";
2651 $searchDAO = CRM_Core_DAO
::executeQuery($query);
2653 while ($searchDAO->fetch()) {
2654 $mailingIDs[] = $searchDAO->mailing_id
;
2661 * Get the content/components of mailing based on mailing Id
2663 * @param array $report
2664 * of mailing report.
2667 * Reference of this.
2669 * @param bool $isSMS
2672 * array content/component.
2674 public static function getMailingContent(&$report, &$form, $isSMS = FALSE) {
2675 $htmlHeader = $textHeader = NULL;
2676 $htmlFooter = $textFooter = NULL;
2679 if ($report['mailing']['header_id']) {
2680 $header = new CRM_Mailing_BAO_MailingComponent();
2681 $header->id
= $report['mailing']['header_id'];
2682 $header->find(TRUE);
2683 $htmlHeader = $header->body_html
;
2684 $textHeader = $header->body_text
;
2687 if ($report['mailing']['footer_id']) {
2688 $footer = new CRM_Mailing_BAO_MailingComponent();
2689 $footer->id
= $report['mailing']['footer_id'];
2690 $footer->find(TRUE);
2691 $htmlFooter = $footer->body_html
;
2692 $textFooter = $footer->body_text
;
2696 $mailingKey = $form->_mailing_id
;
2698 if ($hash = CRM_Mailing_BAO_Mailing
::getMailingHash($mailingKey)) {
2699 $mailingKey = $hash;
2703 if (!empty($report['mailing']['body_text'])) {
2704 $url = CRM_Utils_System
::url('civicrm/mailing/view', 'reset=1&text=1&id=' . $mailingKey);
2705 $form->assign('textViewURL', $url);
2709 if (!empty($report['mailing']['body_html'])) {
2710 $url = CRM_Utils_System
::url('civicrm/mailing/view', 'reset=1&id=' . $mailingKey);
2711 $form->assign('htmlViewURL', $url);
2716 $report['mailing']['attachment'] = CRM_Core_BAO_File
::attachmentInfo('civicrm_mailing', $form->_mailing_id
);
2726 public static function overrideVerp($jobID) {
2727 static $_cache = [];
2729 if (!isset($_cache[$jobID])) {
2731 SELECT override_verp
2732 FROM civicrm_mailing
2733 INNER JOIN civicrm_mailing_job ON civicrm_mailing.id = civicrm_mailing_job.mailing_id
2734 WHERE civicrm_mailing_job.id = %1
2736 $params = [1 => [$jobID, 'Integer']];
2737 $_cache[$jobID] = CRM_Core_DAO
::singleValueQuery($query, $params);
2739 return $_cache[$jobID];
2748 public static function processQueue($mode = NULL) {
2749 $config = CRM_Core_Config
::singleton();
2751 if ($mode == NULL && CRM_Core_BAO_MailSettings
::defaultDomain() == "EXAMPLE.ORG") {
2752 // Using forceBackend=TRUE because WordPress sometimes fails to detect cron
2753 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>', [
2754 1 => CRM_Utils_System
::url('civicrm/admin/mailSettings', 'reset=1', FALSE, NULL, TRUE, FALSE, TRUE),
2755 2 => "https://docs.civicrm.org/sysadmin/en/latest/setup/civimail/",
2759 // check if we are enforcing number of parallel cron jobs
2761 $gotCronLock = FALSE;
2763 $mailerJobsMax = Civi
::settings()->get('mailerJobsMax');
2764 if (is_numeric($mailerJobsMax) && $mailerJobsMax > 0) {
2765 $lockArray = range(1, $mailerJobsMax);
2767 // Shuffle the array to improve chances of quickly finding an open thread
2768 shuffle($lockArray);
2770 // Check if we are using global locks
2771 foreach ($lockArray as $lockID) {
2772 $cronLock = Civi
::lockManager()
2773 ->acquire("worker.mailing.send.{$lockID}");
2774 if ($cronLock->isAcquired()) {
2775 $gotCronLock = TRUE;
2780 // Exit here since we have enough mailing processes running
2781 if (!$gotCronLock) {
2782 CRM_Core_Error
::debug_log_message('Returning early, since the maximum number of mailing processes are running');
2786 if (getenv('CIVICRM_CRON_HOLD')) {
2787 // In testing, we may need to simulate some slow activities.
2788 sleep(getenv('CIVICRM_CRON_HOLD'));
2792 // Split up the parent jobs into multiple child jobs
2793 $mailerJobSize = Civi
::settings()->get('mailerJobSize');
2794 CRM_Mailing_BAO_MailingJob
::runJobs_pre($mailerJobSize, $mode);
2795 CRM_Mailing_BAO_MailingJob
::runJobs(NULL, $mode);
2796 CRM_Mailing_BAO_MailingJob
::runJobs_post($mode);
2798 // Release the global lock if we do have one
2800 $cronLock->release();
2807 * @param int $mailingID
2809 private static function addMultipleEmails($mailingID) {
2811 INSERT INTO civicrm_mailing_recipients
2812 (mailing_id, email_id, contact_id)
2813 SELECT %1, e.id, e.contact_id FROM civicrm_email e
2815 AND e.is_bulkmail = 1
2817 ( SELECT contact_id FROM civicrm_mailing_recipients mr WHERE mailing_id = %1 )
2818 AND e.id NOT IN ( SELECT email_id FROM civicrm_mailing_recipients mr WHERE mailing_id = %1 )
2820 $params = [1 => [$mailingID, 'Integer']];
2822 $dao = CRM_Core_DAO
::executeQuery($sql, $params);
2826 * @param bool $isSMS
2830 public static function getMailingsList($isSMS = FALSE) {
2834 $where .= " civicrm_mailing.sms_provider_id IS NULL ";
2837 $where .= " civicrm_mailing.sms_provider_id IS NOT NULL ";
2842 SELECT civicrm_mailing.id, civicrm_mailing.name, civicrm_mailing_job.end_date
2843 FROM civicrm_mailing
2844 INNER JOIN civicrm_mailing_job ON civicrm_mailing.id = civicrm_mailing_job.mailing_id {$where}
2845 ORDER BY civicrm_mailing.name";
2846 $mailing = CRM_Core_DAO
::executeQuery($query);
2848 while ($mailing->fetch()) {
2849 $list[$mailing->id
] = "{$mailing->name} :: {$mailing->end_date}";
2857 * wrapper for ajax activity selector.
2859 * @param array $params
2860 * Associated array for params record id.
2863 * associated array of contact activities
2865 public static function getContactMailingSelector(&$params) {
2866 // format the params
2867 $params['offset'] = ($params['page'] - 1) * $params['rp'];
2868 $params['rowCount'] = $params['rp'];
2869 $params['sort'] = $params['sortBy'] ??
NULL;
2870 $params['caseId'] = NULL;
2872 // get contact mailings
2873 $mailings = CRM_Mailing_BAO_Mailing
::getContactMailings($params);
2876 $params['total'] = CRM_Mailing_BAO_Mailing
::getContactMailingsCount($params);
2879 if (!empty($mailings)) {
2880 $openCounts = CRM_Mailing_Event_BAO_Opened
::getMailingContactCount(array_keys($mailings), $params['contact_id']);
2881 $clickCounts = CRM_Mailing_Event_BAO_TrackableURLOpen
::getMailingContactCount(array_keys($mailings), $params['contact_id']);
2884 // format params and add links
2885 $contactMailings = [];
2886 foreach ($mailings as $mailingId => $values) {
2888 $mailing['subject'] = $values['subject'];
2889 $mailing['creator_name'] = CRM_Utils_System
::href(
2890 $values['creator_name'],
2891 'civicrm/contact/view',
2892 "reset=1&cid={$values['creator_id']}");
2893 $mailing['recipients'] = CRM_Utils_System
::href(ts('(recipients)'), 'civicrm/mailing/report/event',
2894 "mid={$values['mailing_id']}&reset=1&cid={$params['contact_id']}&event=queue&context=mailing");
2895 $mailing['start_date'] = CRM_Utils_Date
::customFormat($values['start_date']);
2897 $mailing['openstats'] = "Opens: " .
2898 CRM_Utils_Array
::value($values['mailing_id'], $openCounts, 0) .
2900 CRM_Utils_Array
::value($values['mailing_id'], $clickCounts, 0);
2903 CRM_Core_Action
::VIEW
=> [
2904 'name' => ts('View'),
2905 'url' => 'civicrm/mailing/view',
2906 'qs' => "reset=1&id=%%mkey%%",
2907 'title' => ts('View Mailing'),
2908 'class' => 'crm-popup',
2910 CRM_Core_Action
::BROWSE
=> [
2911 'name' => ts('Mailing Report'),
2912 'url' => 'civicrm/mailing/report',
2913 'qs' => "mid=%%mid%%&reset=1&cid=%%cid%%&context=mailing",
2914 'title' => ts('View Mailing Report'),
2918 $mailingKey = $values['mailing_id'];
2919 if ($hash = CRM_Mailing_BAO_Mailing
::getMailingHash($mailingKey)) {
2920 $mailingKey = $hash;
2923 $mailing['links'] = CRM_Core_Action
::formLink(
2927 'mid' => $values['mailing_id'],
2928 'cid' => $params['contact_id'],
2929 'mkey' => $mailingKey,
2933 'mailing.contact.action',
2935 $values['mailing_id']
2938 array_push($contactMailings, $mailing);
2941 $contactMailingsDT = [];
2942 $contactMailingsDT['data'] = $contactMailings;
2943 $contactMailingsDT['recordsTotal'] = $params['total'];
2944 $contactMailingsDT['recordsFiltered'] = $params['total'];
2946 return $contactMailingsDT;
2950 * Retrieve contact mailing.
2952 * @param array $params
2955 * Array of mailings for a contact
2958 public static function getContactMailings(&$params) {
2959 $params['version'] = 3;
2960 $params['offset'] = ($params['page'] - 1) * $params['rp'];
2961 $params['limit'] = $params['rp'];
2962 $params['sort'] = $params['sortBy'] ??
NULL;
2964 $result = civicrm_api('MailingContact', 'get', $params);
2965 return $result['values'];
2969 * Retrieve contact mailing count.
2971 * @param array $params
2974 * count of mailings for a contact
2977 public static function getContactMailingsCount(&$params) {
2978 $params['version'] = 3;
2979 return civicrm_api('MailingContact', 'getcount', $params);
2983 * Get a list of permissions required for CRUD'ing each field
2984 * (when workflow is enabled).
2987 * Array (string $fieldName => string $permName)
2989 public static function getWorkflowFieldPerms() {
2990 $fieldNames = array_keys(CRM_Mailing_DAO_Mailing
::fields());
2992 foreach ($fieldNames as $fieldName) {
2993 if ($fieldName == 'id') {
2994 $fieldPerms[$fieldName] = [
2998 'schedule mailings',
3003 elseif (in_array($fieldName, ['scheduled_date', 'scheduled_id'])) {
3004 $fieldPerms[$fieldName] = [
3006 ['access CiviMail', 'schedule mailings'],
3009 elseif (in_array($fieldName, [
3012 'approval_status_id',
3015 $fieldPerms[$fieldName] = [
3017 ['access CiviMail', 'approve mailings'],
3021 $fieldPerms[$fieldName] = [
3023 ['access CiviMail', 'create mailings'],
3031 * White-list of possible values for the entity_table field.
3035 public static function mailingGroupEntityTables() {
3037 CRM_Contact_BAO_Group
::getTableName() => 'Group',
3038 CRM_Mailing_BAO_Mailing
::getTableName() => 'Mailing',
3043 * Get the public view url.
3046 * @param bool $absolute
3050 public static function getPublicViewUrl($id, $absolute = TRUE) {
3051 if ((civicrm_api3('Mailing', 'getvalue', [
3053 'return' => 'visibility',
3054 ])) === 'Public Pages') {
3056 // if hash setting is on then we change the public url into a hash
3057 $hash = CRM_Mailing_BAO_Mailing
::getMailingHash($id);
3058 if (!empty($hash)) {
3062 return CRM_Utils_System
::url('civicrm/mailing/view', ['id' => $id], $absolute, NULL, TRUE, TRUE);
3067 * Get a list of template types which can be used as `civicrm_mailing.template_type`.
3070 * A list of template-types, keyed numerically. Each defines:
3071 * - name: string, a short symbolic name
3072 * - editorUrl: string, Angular template name
3074 * Ex: $templateTypes[0] === array('name' => 'mosaico', 'editorUrl' => '~/crmMosaico/editor.html').
3076 public static function getTemplateTypes() {
3077 if (!isset(Civi
::$statics[__CLASS__
]['templateTypes'])) {
3080 'name' => 'traditional',
3081 'editorUrl' => CRM_Mailing_Info
::workflowEnabled() ?
'~/crmMailing/EditMailingCtrl/workflow.html' : '~/crmMailing/EditMailingCtrl/2step.html',
3085 CRM_Utils_Hook
::mailingTemplateTypes($types);
3087 $defaults = ['weight' => 0];
3088 foreach (array_keys($types) as $typeName) {
3089 $types[$typeName] = array_merge($defaults, $types[$typeName]);
3091 usort($types, function ($a, $b) {
3092 if ($a['weight'] === $b['weight']) {
3095 return $a['weight'] < $b['weight'] ?
-1 : 1;
3098 Civi
::$statics[__CLASS__
]['templateTypes'] = $types;
3101 return Civi
::$statics[__CLASS__
]['templateTypes'];
3105 * Get a list of template types.
3108 * Array(string $name => string $label).
3110 public static function getTemplateTypeNames() {
3112 foreach (self
::getTemplateTypes() as $type) {
3113 $r[$type['name']] = $type['name'];