3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
33 require_once 'Mail/mime.php';
36 * Class CRM_Mailing_BAO_Mailing
38 class CRM_Mailing_BAO_Mailing
extends CRM_Mailing_DAO_Mailing
{
41 * An array that holds the complete templates
42 * including any headers or footers that need to be prepended
43 * or appended to the body.
46 private $preparedTemplates = NULL;
49 * An array that holds the complete templates
50 * including any headers or footers that need to be prepended
51 * or appended to the body.
54 private $templates = NULL;
57 * An array that holds the tokens that are specifically found in our text and html bodies.
60 private $tokens = NULL;
63 * An array that holds the tokens that are specifically found in our text and html bodies.
66 private $flattenedTokens = NULL;
69 * The header associated with this mailing.
72 private $header = NULL;
75 * The footer associated with this mailing.
78 private $footer = NULL;
81 * The HTML content of the message.
87 * The text content of the message.
93 * Cached BAO for the domain.
96 private $_domain = NULL;
101 public function __construct() {
102 parent
::__construct();
108 * @param int $mailingID
112 public static function getRecipientsCount($mailingID) {
113 //rebuild the recipients
114 self
::getRecipients($mailingID);
116 return civicrm_api3('MailingRecipients', 'getcount', ['mailing_id' => $mailingID]);
120 * This function retrieve recipients of selected mailing groups.
122 * @param int $mailingID
126 public static function getRecipients($mailingID) {
127 // load mailing object
128 $mailingObj = new self();
129 $mailingObj->id
= $mailingID;
130 $mailingObj->find(TRUE);
132 $mailing = CRM_Mailing_BAO_Mailing
::getTableName();
133 $contact = CRM_Contact_DAO_Contact
::getTableName();
134 $isSMSmode = (!CRM_Utils_System
::isNull($mailingObj->sms_provider_id
));
136 $mailingGroup = new CRM_Mailing_DAO_MailingGroup();
137 $recipientsGroup = $excludeSmartGroupIDs = $includeSmartGroupIDs = $priorMailingIDs = [];
138 $dao = CRM_Utils_SQL_Select
::from('civicrm_mailing_group')
139 ->select('GROUP_CONCAT(entity_id SEPARATOR ",") as group_ids, group_type, entity_table')
140 ->where('mailing_id = #mailing_id AND entity_table RLIKE "^civicrm_(group.*|mailing)$" ')
141 ->groupBy(['group_type', 'entity_table'])
142 ->param('!groupTableName', CRM_Contact_BAO_Group
::getTableName())
143 ->param('#mailing_id', $mailingID)
145 while ($dao->fetch()) {
146 if ($dao->entity_table
== 'civicrm_mailing') {
147 $priorMailingIDs[$dao->group_type
] = explode(',', $dao->group_ids
);
150 $recipientsGroup[$dao->group_type
] = empty($recipientsGroup[$dao->group_type
]) ?
explode(',', $dao->group_ids
) : array_merge($recipientsGroup[$dao->group_type
], explode(',', $dao->group_ids
));
154 // there is no need to proceed further if no mailing group is selected to include recipients,
155 // but before return clear the mailing recipients populated earlier since as per current params no group is selected
156 if (empty($recipientsGroup['Include']) && empty($priorMailingIDs['Include'])) {
157 CRM_Core_DAO
::executeQuery(" DELETE FROM civicrm_mailing_recipients WHERE mailing_id = %1 ", [
166 list($location_filter, $order_by) = self
::getLocationFilterAndOrderBy($mailingObj->email_selection_method
, $mailingObj->location_type_id
);
168 // get all the saved searches AND hierarchical groups
169 // and load them in the cache
170 foreach ($recipientsGroup as $groupType => $groupIDs) {
171 $groupDAO = CRM_Utils_SQL_Select
::from('civicrm_group')
172 ->where('id IN (#groupIDs)')
173 ->where('saved_search_id != 0 OR saved_search_id IS NOT NULL OR children IS NOT NULL')
174 ->param('#groupIDs', $groupIDs)
176 while ($groupDAO->fetch()) {
177 // hidden smart groups always have a cache date and there is no other way
178 // we can rebuilt the contact list from UI so consider such smart group
179 if ($groupDAO->cache_date
== NULL ||
$groupDAO->is_hidden
) {
180 CRM_Contact_BAO_GroupContactCache
::load($groupDAO);
182 if ($groupType == 'Include') {
183 $includeSmartGroupIDs[] = $groupDAO->id
;
185 elseif ($groupType == 'Exclude') {
186 $excludeSmartGroupIDs[] = $groupDAO->id
;
188 //NOTE: Do nothing for base
192 // Create a temp table for contact exclusion.
193 $excludeTempTablename = "excluded_recipients_temp" . substr(sha1(rand()), 0, 4);
194 $includedTempTablename = "included_recipients_temp" . substr(sha1(rand()), 0, 4);
195 $mailingGroup->query(
196 "CREATE TEMPORARY TABLE $excludeTempTablename
197 (contact_id int primary key)
200 // populate exclude temp-table with recipients to be excluded from the list
201 // on basis of selected recipients groups and/or previous mailing
202 if (!empty($recipientsGroup['Exclude'])) {
203 CRM_Utils_SQL_Select
::from('civicrm_group_contact')
204 ->select('DISTINCT contact_id')
205 ->where('status = "Added" AND group_id IN (#groups)')
206 ->param('#groups', $recipientsGroup['Exclude'])
207 ->insertInto($excludeTempTablename, ['contact_id'])
210 if (count($excludeSmartGroupIDs)) {
211 CRM_Utils_SQL_Select
::from('civicrm_group_contact_cache')
212 ->select('contact_id')
213 ->where('group_id IN (#groups)')
214 ->param('#groups', $excludeSmartGroupIDs)
215 ->insertIgnoreInto($excludeTempTablename, ['contact_id'])
219 if (!empty($priorMailingIDs['Exclude'])) {
220 CRM_Utils_SQL_Select
::from('civicrm_mailing_recipients')
221 ->select('DISTINCT contact_id')
222 ->where('mailing_id IN (#mailings)')
223 ->param('#mailings', $priorMailingIDs['Exclude'])
224 ->insertIgnoreInto($excludeTempTablename, ['contact_id'])
228 if (!empty($recipientsGroup['Base'])) {
229 CRM_Utils_SQL_Select
::from('civicrm_group_contact')
230 ->select('DISTINCT contact_id')
231 ->where('status = "Removed" AND group_id IN (#groups)')
232 ->param('#groups', $recipientsGroup['Base'])
233 ->insertIgnoreInto($excludeTempTablename, ['contact_id'])
237 $entityColumn = $isSMSmode ?
'phone_id' : 'email_id';
238 $entityTable = $isSMSmode ? CRM_Core_DAO_Phone
::getTableName() : CRM_Core_DAO_Email
::getTableName();
239 // Get all the group contacts we want to include.
240 $mailingGroup->query(
241 "CREATE TEMPORARY TABLE $includedTempTablename
242 (contact_id int primary key, $entityColumn int)
248 'is_opt_out' => CRM_Utils_SQL_Select
::fragment()->where("$contact.is_opt_out = 0"),
249 'is_deceased' => CRM_Utils_SQL_Select
::fragment()->where("$contact.is_deceased <> 1"),
250 'do_not_sms' => CRM_Utils_SQL_Select
::fragment()->where("$contact.do_not_sms = 0"),
251 'location_filter' => CRM_Utils_SQL_Select
::fragment()->where("$entityTable.phone_type_id = " . CRM_Core_PseudoConstant
::getKey('CRM_Core_DAO_Phone', 'phone_type_id', 'Mobile')),
252 'phone_not_null' => CRM_Utils_SQL_Select
::fragment()->where("$entityTable.phone IS NOT NULL"),
253 'phone_not_empty' => CRM_Utils_SQL_Select
::fragment()->where("$entityTable.phone != ''"),
254 'mailing_id' => CRM_Utils_SQL_Select
::fragment()->where("mg.mailing_id = #mailingID"),
255 'temp_contact_null' => CRM_Utils_SQL_Select
::fragment()->where('temp.contact_id IS null'),
256 'order_by' => CRM_Utils_SQL_Select
::fragment()->orderBy("$entityTable.is_primary"),
260 // Criterias to filter recipients that need to be included
262 'do_not_email' => CRM_Utils_SQL_Select
::fragment()->where("$contact.do_not_email = 0"),
263 'is_opt_out' => CRM_Utils_SQL_Select
::fragment()->where("$contact.is_opt_out = 0"),
264 'is_deceased' => CRM_Utils_SQL_Select
::fragment()->where("$contact.is_deceased <> 1"),
265 'location_filter' => CRM_Utils_SQL_Select
::fragment()->where($location_filter),
266 'email_not_null' => CRM_Utils_SQL_Select
::fragment()->where("$entityTable.email IS NOT NULL"),
267 'email_not_empty' => CRM_Utils_SQL_Select
::fragment()->where("$entityTable.email != ''"),
268 'email_not_on_hold' => CRM_Utils_SQL_Select
::fragment()->where("$entityTable.on_hold = 0"),
269 'mailing_id' => CRM_Utils_SQL_Select
::fragment()->where("mg.mailing_id = #mailingID"),
270 'temp_contact_null' => CRM_Utils_SQL_Select
::fragment()->where('temp.contact_id IS NULL'),
271 'order_by' => CRM_Utils_SQL_Select
::fragment()->orderBy($order_by),
275 // Allow user to alter query responsible to fetch mailing recipients before build,
276 // by changing the mail filters identified $params
277 CRM_Utils_Hook
::alterMailingRecipients($mailingObj, $criteria, 'pre');
279 // Get the group contacts, but only those which are not in the
280 // exclusion temp table.
281 if (!empty($recipientsGroup['Include'])) {
282 CRM_Utils_SQL_Select
::from($entityTable)
283 ->select("$contact.id as contact_id, $entityTable.id as $entityColumn")
284 ->join($contact, " INNER JOIN $contact ON $entityTable.contact_id = $contact.id ")
285 ->join('gc', " INNER JOIN civicrm_group_contact gc ON gc.contact_id = $contact.id ")
286 ->join('mg', " INNER JOIN civicrm_mailing_group mg ON gc.group_id = mg.entity_id AND mg.search_id IS NULL ")
287 ->join('temp', " LEFT JOIN $excludeTempTablename temp ON $contact.id = temp.contact_id ")
288 ->where('gc.group_id IN (#groups) AND gc.status = "Added"')
290 ->groupBy(["$contact.id", "$entityTable.id"])
291 ->replaceInto($includedTempTablename, ['contact_id', $entityColumn])
292 ->param('#groups', $recipientsGroup['Include'])
293 ->param('#mailingID', $mailingID)
297 // Get recipients selected in prior mailings
298 if (!empty($priorMailingIDs['Include'])) {
299 CRM_Utils_SQL_Select
::from('civicrm_mailing_recipients')
300 ->select("DISTINCT civicrm_mailing_recipients.contact_id, $entityColumn")
301 ->join('temp', " LEFT JOIN $excludeTempTablename temp ON civicrm_mailing_recipients.contact_id = temp.contact_id ")
302 ->where('mailing_id IN (#mailings)')
303 ->where('temp.contact_id IS NULL')
304 ->param('#mailings', $priorMailingIDs['Include'])
305 ->insertIgnoreInto($includedTempTablename, [
312 if (count($includeSmartGroupIDs)) {
313 $query = CRM_Utils_SQL_Select
::from($contact)
314 ->select("$contact.id as contact_id, $entityTable.id as $entityColumn")
315 ->join($entityTable, " INNER JOIN $entityTable ON $entityTable.contact_id = $contact.id ")
316 ->join('gc', " INNER JOIN civicrm_group_contact_cache gc ON $contact.id = gc.contact_id ")
317 ->join('gcr', " LEFT JOIN civicrm_group_contact gcr ON gc.group_id = gcr.group_id AND gc.contact_id = gcr.contact_id")
318 ->join('mg', " INNER JOIN civicrm_mailing_group mg ON gc.group_id = mg.entity_id AND mg.search_id IS NULL ")
319 ->join('temp', " LEFT JOIN $excludeTempTablename temp ON $contact.id = temp.contact_id ")
320 ->where('gc.group_id IN (#groups)')
321 ->where('gcr.status IS NULL OR gcr.status != "Removed"')
323 ->replaceInto($includedTempTablename, ['contact_id', $entityColumn])
324 ->param('#groups', $includeSmartGroupIDs)
325 ->param('#mailingID', $mailingID)
329 // Construct the filtered search queries.
330 $dao = CRM_Utils_SQL_Select
::from('civicrm_mailing_group')
331 ->select('search_id, search_args, entity_id')
332 ->where('search_id IS NOT NULL AND mailing_id = #mailingID')
333 ->param('#mailingID', $mailingID)
335 while ($dao->fetch()) {
336 $customSQL = CRM_Contact_BAO_SearchCustom
::civiMailSQL($dao->search_id
,
340 $query = "REPLACE INTO {$includedTempTablename} ($entityColumn, contact_id) {$customSQL} ";
341 $mailingGroup->query($query);
344 list($aclFrom, $aclWhere) = CRM_Contact_BAO_Contact_Permission
::cacheClause();
346 // clear all the mailing recipients before populating
347 CRM_Core_DAO
::executeQuery(" DELETE FROM civicrm_mailing_recipients WHERE mailing_id = %1 ", [
354 $selectClause = ['#mailingID', 'i.contact_id', "i.$entityColumn"];
356 $orderBy = ["i.contact_id", "i.$entityColumn"];
358 $query = CRM_Utils_SQL_Select
::from('civicrm_contact contact_a')
359 ->join('i', " INNER JOIN {$includedTempTablename} i ON contact_a.id = i.contact_id ");
360 if (!$isSMSmode && $mailingObj->dedupe_email
) {
361 $orderBy = ["MIN(i.contact_id)", "MIN(i.$entityColumn)"];
362 $query = $query->join('e', " INNER JOIN civicrm_email e ON e.id = i.email_id ")
363 ->groupBy("e.email");
364 if (CRM_Utils_SQL
::supportsFullGroupBy()) {
367 'ANY_VALUE(i.contact_id) contact_id',
368 "ANY_VALUE(i.$entityColumn) $entityColumn",
374 $query = $query->select($selectClause)->orderBy($orderBy);
375 if (!CRM_Utils_System
::isNull($aclFrom)) {
376 $query = $query->join('acl', $aclFrom);
378 if (!CRM_Utils_System
::isNull($aclWhere)) {
379 $query = $query->where($aclWhere);
382 // this mean if dedupe_email AND the mysql 5.7 supports ONLY_FULL_GROUP_BY mode then as
383 // SELECT must contain 'email' column as its used in GROUP BY, so in order to resolve This
384 // here the whole SQL code is wrapped up in FROM table i and not selecting email column for INSERT
385 if ($key = array_search('e.email', $selectClause)) {
386 unset($selectClause[$key]);
387 $sql = $query->toSQL();
388 CRM_Utils_SQL_Select
::from("( $sql ) AS i ")
389 ->select($selectClause)
390 ->insertInto('civicrm_mailing_recipients', ['mailing_id', 'contact_id', $entityColumn])
391 ->param('#mailingID', $mailingID)
395 $query->insertInto('civicrm_mailing_recipients', ['mailing_id', 'contact_id', $entityColumn])
396 ->param('#mailingID', $mailingID)
400 // if we need to add all emails marked bulk, do it as a post filter
401 // on the mailing recipients table
402 if (CRM_Core_BAO_Email
::isMultipleBulkMail()) {
403 self
::addMultipleEmails($mailingID);
406 // Delete the temp table.
407 $mailingGroup->reset();
408 $mailingGroup->query(" DROP TEMPORARY TABLE $excludeTempTablename ");
409 $mailingGroup->query(" DROP TEMPORARY TABLE $includedTempTablename ");
411 CRM_Utils_Hook
::alterMailingRecipients($mailingObj, $criteria, 'post');
415 * Function to retrieve location filter and order by clause later used by SQL query that is used to fetch and include mailing recipients
417 * @param string $email_selection_method
418 * @param int $location_type_id
422 public static function getLocationFilterAndOrderBy($email_selection_method, $location_type_id) {
423 $email = CRM_Core_DAO_Email
::getTableName();
424 // Note: When determining the ORDER that results are returned, it's
425 // the record that comes last that counts. That's because we are
426 // INSERT'ing INTO a table with a primary id so that last record
427 // over writes any previous record.
428 switch ($email_selection_method) {
429 case 'location-exclude':
430 $location_filter = "($email.location_type_id != $location_type_id)";
431 // If there is more than one email that doesn't match the location,
432 // prefer the one marked is_bulkmail, followed by is_primary.
433 $orderBy = ["$email.is_bulkmail", "$email.is_primary"];
436 case 'location-only':
437 $location_filter = "($email.location_type_id = $location_type_id)";
438 // If there is more than one email of the desired location, prefer
439 // the one marked is_bulkmail, followed by is_primary.
440 $orderBy = ["$email.is_bulkmail", "$email.is_primary"];
443 case 'location-prefer':
444 $location_filter = "($email.is_bulkmail = 1 OR $email.is_primary = 1 OR $email.location_type_id = $location_type_id)";
445 // ORDER BY is more complicated because we have to set an arbitrary
446 // order that prefers the location that we want. We do that using
447 // the FIELD function. For more info, see:
448 // https://dev.mysql.com/doc/refman/5.5/en/string-functions.html#function_field
449 // We assign the location type we want the value "1" by putting it
450 // in the first position after we name the field. All other location
451 // types are left out, so they will be assigned the value 0. That
452 // means, they will all be equally tied for first place, with our
453 // location being last.
455 "FIELD($email.location_type_id, $location_type_id)",
456 "$email.is_bulkmail",
462 // fall through to default
464 $location_filter = "($email.is_bulkmail = 1 OR $email.is_primary = 1)";
465 $orderBy = ["$email.is_bulkmail"];
468 return [$location_filter, $orderBy];
472 * @param string $type
476 private function _getMailingGroupIds($type = 'Include') {
477 $mailingGroup = new CRM_Mailing_DAO_MailingGroup();
478 $group = CRM_Contact_DAO_Group
::getTableName();
479 if (!isset($this->id
)) {
480 // we're just testing tokens, so return any group
481 $query = "SELECT id AS entity_id
487 $mg = CRM_Mailing_DAO_MailingGroup
::getTableName();
488 $query = "SELECT entity_id
490 WHERE mailing_id = {$this->id}
491 AND group_type = '$type'
492 AND entity_table = '$group'";
494 $mailingGroup->query($query);
497 while ($mailingGroup->fetch()) {
498 $groupIds[] = $mailingGroup->entity_id
;
505 * Returns the regex patterns that are used for preparing the text and html templates.
507 * @param bool $onlyHrefs
509 * @return array|string
511 private function getPatterns($onlyHrefs = FALSE) {
515 $protos = '(https?|ftp|mailto)';
517 $gunk = '\{\}/#~:.?+=&;%@!\,\-\|\(\)\*';
519 $any = "{$letters}{$gunk}{$punc}";
521 $pattern = "\\bhref[ ]*=[ ]*([\"'])?(($protos:[$any]+?(?=[$punc]*[^$any]|$)))([\"'])?";
524 $pattern = "\\b($protos:[$any]+?(?=[$punc]*[^$any]|$))";
527 $patterns[] = $pattern;
528 $patterns[] = '\\\\\{\w+\.\w+\\\\\}|\{\{\w+\.\w+\}\}';
529 $patterns[] = '\{\w+\.\w+\}';
531 $patterns = '{' . implode('|', $patterns) . '}imu';
537 * Returns an array that denotes the type of token that we are dealing with
538 * we use the type later on when we are doing a token replacement lookup
540 * @param string $token
541 * The token for which we will be doing adata lookup.
544 * An array that holds the token itself and the type.
545 * the type will tell us which function to use for the data lookup
546 * if we need to do a lookup at all
548 public function &getDataFunc($token) {
549 static $_categories = NULL;
550 static $_categoryString = NULL;
559 CRM_Utils_Hook
::tokens($_categories);
560 $_categoryString = implode('|', array_keys($_categories));
563 $funcStruct = ['type' => NULL, 'token' => $token];
565 if ((preg_match('/^href/i', $token) ||
preg_match('/^http/i', $token))) {
566 // it is a url so we need to check to see if there are any tokens embedded
567 // if so then call this function again to get the token dataFunc
568 // and assign the type 'embedded' so that the data retrieving function
569 // will know what how to handle this token.
570 if (preg_match_all('/(\{\w+\.\w+\})/', $token, $matches)) {
571 $funcStruct['type'] = 'embedded_url';
572 $funcStruct['embed_parts'] = $funcStruct['token'] = [];
573 foreach ($matches[1] as $match) {
574 $preg_token = '/' . preg_quote($match, '/') . '/';
575 $list = preg_split($preg_token, $token, 2);
576 $funcStruct['embed_parts'][] = $list[0];
578 $funcStruct['token'][] = $this->getDataFunc($match);
580 // fixed truncated url, CRM-7113
582 $funcStruct['embed_parts'][] = $token;
586 $funcStruct['type'] = 'url';
589 elseif (preg_match('/^\{(' . $_categoryString . ')\.(\w+)\}$/', $token, $matches)) {
590 $funcStruct['type'] = $matches[1];
591 $funcStruct['token'] = $matches[2];
593 elseif (preg_match('/\\\\\{(\w+\.\w+)\\\\\}|\{\{(\w+\.\w+)\}\}/', $token, $matches)) {
594 // we are an escaped token
595 // so remove the escape chars
596 $unescaped_token = preg_replace('/\{\{|\}\}|\\\\\{|\\\\\}/', '', $matches[0]);
597 $funcStruct['token'] = '{' . $unescaped_token . '}';
603 * Prepares the text and html templates
604 * for generating the emails and returns a copy of the
608 * This is used by CiviMail but will be made redundant by FlexMailer/TokenProcessor.
610 private function getPreparedTemplates() {
611 if (!$this->preparedTemplates
) {
612 $patterns['html'] = $this->getPatterns(TRUE);
613 $patterns['subject'] = $patterns['text'] = $this->getPatterns();
614 $templates = $this->getTemplates();
616 $this->preparedTemplates
= [];
623 if (!isset($templates[$key])) {
629 $split_template = [];
631 $email = $templates[$key];
632 preg_match_all($patterns[$key], $email, $matches, PREG_PATTERN_ORDER
);
633 foreach ($matches[0] as $idx => $token) {
634 $preg_token = '/' . preg_quote($token, '/') . '/im';
635 list($split_template[], $email) = preg_split($preg_token, $email, 2);
636 array_push($tokens, $this->getDataFunc($token));
639 $split_template[] = $email;
641 $this->preparedTemplates
[$key]['template'] = $split_template;
642 $this->preparedTemplates
[$key]['tokens'] = $tokens;
645 return ($this->preparedTemplates
);
649 * Retrieve a ref to an array that holds the email and text templates for this email
650 * assembles the complete template including the header and footer
651 * that the user has uploaded or declared (if they have done that)
654 * reference to an assoc array
656 public function getTemplates() {
657 if (!$this->templates
) {
658 $this->getHeaderFooter();
659 $this->templates
= [];
660 if ($this->body_text ||
!empty($this->header
)) {
662 if (!empty($this->header
->body_text
)) {
663 $template[] = $this->header
->body_text
;
665 elseif (!empty($this->header
->body_html
)) {
666 $template[] = CRM_Utils_String
::htmlToText($this->header
->body_html
);
669 if ($this->body_text
) {
670 $template[] = $this->body_text
;
673 $template[] = CRM_Utils_String
::htmlToText($this->body_html
);
676 if (!empty($this->footer
->body_text
)) {
677 $template[] = $this->footer
->body_text
;
679 elseif (!empty($this->footer
->body_html
)) {
680 $template[] = CRM_Utils_String
::htmlToText($this->footer
->body_html
);
683 $this->templates
['text'] = implode("\n", $template);
686 // To check for an html part strip tags
687 if (trim(strip_tags($this->body_html
, '<img>'))) {
691 $template[] = $this->header
->body_html
;
694 $template[] = $this->body_html
;
697 $template[] = $this->footer
->body_html
;
700 $this->templates
['html'] = implode("\n", $template);
702 // this is where we create a text template from the html template if the text template did not exist
703 // this way we ensure that every recipient will receive an email even if the pref is set to text and the
704 // user uploads an html email only
705 if (empty($this->templates
['text'])) {
706 $this->templates
['text'] = CRM_Utils_String
::htmlToText($this->templates
['html']);
710 if ($this->subject
) {
712 $template[] = $this->subject
;
713 $this->templates
['subject'] = implode("\n", $template);
716 CRM_Utils_Hook
::alterMailContent($this->templates
);
718 return $this->templates
;
723 * Retrieve a ref to an array that holds all of the tokens in the email body
724 * where the keys are the type of token and the values are ordinal arrays
725 * that hold the token names (even repeated tokens) in the order in which
726 * they appear in the body of the email.
728 * note: the real work is done in the _getTokens() function
730 * this function needs to have some sort of a body assigned
731 * either text or html for this to have any meaningful impact
734 * reference to an assoc array
736 public function &getTokens() {
737 if (!$this->tokens
) {
739 $this->tokens
= ['html' => [], 'text' => [], 'subject' => []];
741 if ($this->body_html
) {
742 $this->_getTokens('html');
743 if (!$this->body_text
) {
744 // Since the text template was created from html, use the html tokens.
745 // @see CRM_Mailing_BAO_Mailing::getTemplates()
746 $this->tokens
['text'] = $this->tokens
['html'];
750 if ($this->body_text
) {
751 $this->_getTokens('text');
754 if ($this->subject
) {
755 $this->_getTokens('subject');
759 return $this->tokens
;
763 * Returns the token set for all 3 parts as one set. This allows it to be sent to the
764 * hook in one call and standardizes it across other token workflows
767 * reference to an assoc array
769 public function &getFlattenedTokens() {
770 if (!$this->flattenedTokens
) {
771 $tokens = $this->getTokens();
773 $this->flattenedTokens
= CRM_Utils_Token
::flattenTokens($tokens);
776 return $this->flattenedTokens
;
781 * _getTokens parses out all of the tokens that have been
782 * included in the html and text bodies of the email
783 * we get the tokens and then separate them into an
784 * internal structure named tokens that has the same
785 * form as the static tokens property(?) of the CRM_Utils_Token class.
786 * The difference is that there might be repeated token names as we want the
787 * structures to represent the order in which tokens were found from left to right, top to bottom.
790 * @param string $prop name of the property that holds the text that we want to scan for tokens (html, text).
791 * Name of the property that holds the text that we want to scan for tokens (html, text).
795 private function _getTokens($prop) {
796 $templates = $this->getTemplates();
798 $newTokens = CRM_Utils_Token
::getTokens($templates[$prop]);
800 foreach ($newTokens as $type => $names) {
801 if (!isset($this->tokens
[$prop][$type])) {
802 $this->tokens
[$prop][$type] = [];
804 foreach ($names as $key => $name) {
805 $this->tokens
[$prop][$type][] = $name;
811 * Generate an event queue for a test job.
813 * @param array $testParams
814 * Contains form values.
818 public function getTestRecipients($testParams) {
819 if (!empty($testParams['test_group']) && array_key_exists($testParams['test_group'], CRM_Core_PseudoConstant
::group())) {
820 $contacts = civicrm_api('contact', 'get', [
822 'group' => $testParams['test_group'],
825 'limit' => 100000000000,
829 foreach (array_keys($contacts['values']) as $groupContact) {
831 SELECT civicrm_email.id AS email_id,
832 civicrm_email.is_primary as is_primary,
833 civicrm_email.is_bulkmail as is_bulkmail
835 INNER JOIN civicrm_contact ON civicrm_email.contact_id = civicrm_contact.id
836 WHERE (civicrm_email.is_bulkmail = 1 OR civicrm_email.is_primary = 1)
837 AND civicrm_contact.id = {$groupContact}
838 AND civicrm_contact.do_not_email = 0
839 AND civicrm_contact.is_deceased <> 1
840 AND civicrm_email.on_hold = 0
841 AND civicrm_contact.is_opt_out = 0
842 GROUP BY civicrm_email.id
843 ORDER BY civicrm_email.is_bulkmail DESC
845 $dao = CRM_Core_DAO
::executeQuery($query);
848 'job_id' => $testParams['job_id'],
849 'email_id' => $dao->email_id
,
850 'contact_id' => $groupContact,
852 CRM_Mailing_Event_BAO_Queue
::create($params);
859 * Load this->header and this->footer.
861 private function getHeaderFooter() {
862 if (!$this->header
and $this->header_id
) {
863 $this->header
= new CRM_Mailing_BAO_MailingComponent();
864 $this->header
->id
= $this->header_id
;
865 $this->header
->find(TRUE);
868 if (!$this->footer
and $this->footer_id
) {
869 $this->footer
= new CRM_Mailing_BAO_MailingComponent();
870 $this->footer
->id
= $this->footer_id
;
871 $this->footer
->find(TRUE);
876 * Given and array of headers and a prefix, job ID, event queue ID, and hash,
877 * add a Message-ID header if needed.
879 * i.e. if the global includeMessageId is set and there isn't already a
880 * Message-ID in the array.
881 * The message ID is structured the same way as a verp. However no interpretation
882 * is placed on the values received, so they do not need to follow the verp
885 * @param array $headers
886 * Array of message headers to update, in-out.
887 * @param string $prefix
888 * Prefix for the message ID, use same prefixes as verp.
890 * @param string $job_id
891 * Job ID component of the generated message ID.
892 * @param string $event_queue_id
893 * Event Queue ID component of the generated message ID.
894 * @param string $hash
895 * Hash component of the generated message ID.
899 public static function addMessageIdHeader(&$headers, $prefix, $job_id, $event_queue_id, $hash) {
900 $config = CRM_Core_Config
::singleton();
901 $localpart = CRM_Core_BAO_MailSettings
::defaultLocalpart();
902 $emailDomain = CRM_Core_BAO_MailSettings
::defaultDomain();
903 $includeMessageId = CRM_Core_BAO_MailSettings
::includeMessageId();
905 $fields[] = 'Message-ID';
906 // CRM-17754 check if Resent-Message-id is set also if not add it in when re-laying reply email
907 if ($prefix == 'r') {
908 $fields[] = 'Resent-Message-ID';
910 foreach ($fields as $field) {
911 if ($includeMessageId && (!array_key_exists($field, $headers))) {
912 $headers[$field] = '<' . implode($config->verpSeparator
,
914 $localpart . $prefix,
919 ) . "@{$emailDomain}>";
926 * Static wrapper for getting verp and urls.
929 * ID of the Job associated with this message.
930 * @param int $event_queue_id
931 * ID of the EventQueue.
932 * @param string $hash
933 * Hash of the EventQueue.
934 * @param string $email
935 * Destination address.
938 * (reference) array array ref that hold array refs to the verp info and urls
940 public static function getVerpAndUrls($job_id, $event_queue_id, $hash, $email) {
941 // create a skeleton object and set its properties that are required by getVerpAndUrlsAndHeaders()
942 $config = CRM_Core_Config
::singleton();
943 $bao = new CRM_Mailing_BAO_Mailing();
944 $bao->_domain
= CRM_Core_BAO_Domain
::getDomain();
945 $bao->from_name
= $bao->from_email
= $bao->subject
= '';
947 // use $bao's instance method to get verp and urls
948 list($verp, $urls, $_) = $bao->getVerpAndUrlsAndHeaders($job_id, $event_queue_id, $hash, $email);
949 return [$verp, $urls];
953 * Get verp, urls and headers
956 * ID of the Job associated with this message.
957 * @param int $event_queue_id
958 * ID of the EventQueue.
959 * @param string $hash
960 * Hash of the EventQueue.
961 * @param string $email
962 * Destination address.
964 * @param bool $isForward
967 * array ref that hold array refs to the verp info, urls, and headers
969 public function getVerpAndUrlsAndHeaders($job_id, $event_queue_id, $hash, $email, $isForward = FALSE) {
970 $config = CRM_Core_Config
::singleton();
974 * reply: user replied to mailing
975 * bounce: email address bounced
976 * unsubscribe: contact opts out of all target lists for the mailing
977 * resubscribe: contact opts back into all target lists for the mailing
978 * optOut: contact unsubscribes from the domain
984 'unsubscribe' => 'u',
985 'resubscribe' => 'e',
989 $localpart = CRM_Core_BAO_MailSettings
::defaultLocalpart();
990 $emailDomain = CRM_Core_BAO_MailSettings
::defaultDomain();
991 // 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
992 if (empty($emailDomain)) {
993 CRM_Core_Error
::debug_log_message('Error setting verp parameters, defaultDomain is NULL. Did you configure the bounce processing account for this domain?');
996 foreach ($verpTokens as $key => $value) {
997 $verp[$key] = implode($config->verpSeparator
,
1004 ) . "@$emailDomain";
1007 //handle should override VERP address.
1008 $skipEncode = FALSE;
1011 self
::overrideVerp($job_id)
1013 $verp['reply'] = "\"{$this->from_name}\" <{$this->from_email}>";
1017 'forward' => CRM_Utils_System
::url('civicrm/mailing/forward', "reset=1&jid={$job_id}&qid={$event_queue_id}&h={$hash}", TRUE, NULL, TRUE, TRUE),
1018 'unsubscribeUrl' => CRM_Utils_System
::url('civicrm/mailing/unsubscribe', "reset=1&jid={$job_id}&qid={$event_queue_id}&h={$hash}", TRUE, NULL, TRUE, TRUE),
1019 'resubscribeUrl' => CRM_Utils_System
::url('civicrm/mailing/resubscribe', "reset=1&jid={$job_id}&qid={$event_queue_id}&h={$hash}", TRUE, NULL, TRUE, TRUE),
1020 'optOutUrl' => CRM_Utils_System
::url('civicrm/mailing/optout', "reset=1&jid={$job_id}&qid={$event_queue_id}&h={$hash}", TRUE, NULL, TRUE, TRUE),
1021 'subscribeUrl' => CRM_Utils_System
::url('civicrm/mailing/subscribe', 'reset=1', TRUE, NULL, TRUE, TRUE),
1025 'Reply-To' => $verp['reply'],
1026 'Return-Path' => $verp['bounce'],
1027 'From' => "\"{$this->from_name}\" <{$this->from_email}>",
1028 'Subject' => $this->subject
,
1029 'List-Unsubscribe' => "<mailto:{$verp['unsubscribe']}>",
1031 self
::addMessageIdHeader($headers, 'm', $job_id, $event_queue_id, $hash);
1033 $headers['Subject'] = "[Fwd:{$this->subject}]";
1035 return [&$verp, &$urls, &$headers];
1039 * Compose a message.
1042 * This is used by CiviMail but will be made redundant by FlexMailer/TokenProcessor.
1043 * @param int $job_id
1044 * ID of the Job associated with this message.
1045 * @param int $event_queue_id
1046 * ID of the EventQueue.
1047 * @param string $hash
1048 * Hash of the EventQueue.
1049 * @param string $contactId
1050 * ID of the Contact.
1051 * @param string $email
1052 * Destination address.
1053 * @param string $recipient
1054 * To: of the recipient.
1056 * Is this mailing a test?.
1057 * @param $contactDetails
1058 * @param $attachments
1059 * @param bool $isForward
1060 * Is this mailing compose for forward?.
1061 * @param string $fromEmail
1062 * Email address of who is forwardinf it.
1064 * @param null $replyToEmail
1066 * @return Mail_mime The mail object
1068 public function compose(
1069 $job_id, $event_queue_id, $hash, $contactId,
1070 $email, &$recipient, $test,
1071 $contactDetails, &$attachments, $isForward = FALSE,
1072 $fromEmail = NULL, $replyToEmail = NULL
1074 $config = CRM_Core_Config
::singleton();
1077 if ($this->_domain
== NULL) {
1078 $this->_domain
= CRM_Core_BAO_Domain
::getDomain();
1081 list($verp, $urls, $headers) = $this->getVerpAndUrlsAndHeaders(
1089 //set from email who is forwarding it and not original one.
1091 unset($headers['From']);
1092 $headers['From'] = "<{$fromEmail}>";
1095 if ($replyToEmail && ($fromEmail != $replyToEmail)) {
1096 $headers['Reply-To'] = "{$replyToEmail}";
1099 if ($contactDetails) {
1100 $contact = $contactDetails;
1102 elseif ($contactId === 0) {
1105 CRM_Utils_Hook
::tokenValues($contact, $contactId, $job_id);
1108 $params = [['contact_id', '=', $contactId, 0, 0]];
1109 list($contact) = CRM_Contact_BAO_Query
::apiQuery($params);
1112 $contact = reset($contact);
1114 if (!$contact ||
is_a($contact, 'CRM_Core_Error')) {
1115 CRM_Core_Error
::debug_log_message(ts('CiviMail will not send email to a non-existent contact: %1',
1118 // setting this because function is called by reference
1119 //@todo test not calling function by reference
1124 // also call the hook to get contact details
1125 CRM_Utils_Hook
::tokenValues($contact, $contactId, $job_id);
1128 $pTemplates = $this->getPreparedTemplates();
1131 foreach ($pTemplates as $type => $pTemplate) {
1132 $html = ($type == 'html') ?
TRUE : FALSE;
1133 $pEmails[$type] = [];
1134 $pEmail = &$pEmails[$type];
1135 $template = &$pTemplates[$type]['template'];
1136 $tokens = &$pTemplates[$type]['tokens'];
1138 if (!empty($tokens)) {
1139 foreach ($tokens as $idx => $token) {
1140 $token_data = $this->getTokenData($token, $html, $contact, $verp, $urls, $event_queue_id);
1141 array_push($pEmail, $template[$idx]);
1142 array_push($pEmail, $token_data);
1146 array_push($pEmail, $template[$idx]);
1149 if (isset($template[($idx +
1)])) {
1150 array_push($pEmail, $template[($idx +
1)]);
1155 if (isset($pEmails['html']) && is_array($pEmails['html']) && count($pEmails['html'])) {
1156 $html = &$pEmails['html'];
1160 if (isset($pEmails['text']) && is_array($pEmails['text']) && count($pEmails['text'])) {
1161 $text = &$pEmails['text'];
1164 // push the tracking url on to the html email if necessary
1165 if ($this->open_tracking
&& $html) {
1166 array_push($html, "\n" . '<img src="' . $config->userFrameworkResourceURL
.
1167 "extern/open.php?q=$event_queue_id\" width='1' height='1' alt='' border='0'>"
1171 $message = new Mail_mime("\n");
1173 $useSmarty = defined('CIVICRM_MAIL_SMARTY') && CIVICRM_MAIL_SMARTY ?
TRUE : FALSE;
1175 $smarty = CRM_Core_Smarty
::singleton();
1176 // also add the contact tokens to the template
1177 $smarty->assign_by_ref('contact', $contact);
1180 $mailParams = $headers;
1181 if ($text && ($test ||
$contact['preferred_mail_format'] == 'Text' ||
1182 $contact['preferred_mail_format'] == 'Both' ||
1183 ($contact['preferred_mail_format'] == 'HTML' && !array_key_exists('html', $pEmails))
1186 $textBody = implode('', $text);
1188 $textBody = $smarty->fetch("string:$textBody");
1190 $mailParams['text'] = $textBody;
1193 if ($html && ($test ||
($contact['preferred_mail_format'] == 'HTML' ||
1194 $contact['preferred_mail_format'] == 'Both'
1197 $htmlBody = implode('', $html);
1199 $htmlBody = $smarty->fetch("string:$htmlBody");
1201 $mailParams['html'] = $htmlBody;
1204 if (empty($mailParams['text']) && empty($mailParams['html'])) {
1206 // something went wrong, lets log it and return null (by reference)
1207 CRM_Core_Error
::debug_log_message(ts('CiviMail will not send an empty mail body, Skipping: %1',
1214 $mailParams['attachments'] = $attachments;
1216 $mailParams['Subject'] = CRM_Utils_Array
::value('subject', $pEmails);
1217 if (is_array($mailParams['Subject'])) {
1218 $mailParams['Subject'] = implode('', $mailParams['Subject']);
1221 $mailParams['toName'] = CRM_Utils_Array
::value('display_name',
1224 $mailParams['toEmail'] = $email;
1226 // Add job ID to mailParams for external email delivery service to utilise
1227 $mailParams['job_id'] = $job_id;
1229 CRM_Utils_Hook
::alterMailParams($mailParams, 'civimail');
1231 // CRM-10699 support custom email headers
1232 if (!empty($mailParams['headers'])) {
1233 $headers = array_merge($headers, $mailParams['headers']);
1235 //cycle through mailParams and set headers array
1236 foreach ($mailParams as $paramKey => $paramValue) {
1237 //exclude values not intended for the header
1238 if (!in_array($paramKey, [
1246 $headers[$paramKey] = $paramValue;
1250 if (!empty($mailParams['text'])) {
1251 $message->setTxtBody($mailParams['text']);
1254 if (!empty($mailParams['html'])) {
1255 $message->setHTMLBody($mailParams['html']);
1258 if (!empty($mailParams['attachments'])) {
1259 foreach ($mailParams['attachments'] as $fileID => $attach) {
1260 $message->addAttachment($attach['fullPath'],
1261 $attach['mime_type'],
1262 $attach['cleanName']
1267 //pickup both params from mail params.
1268 $toName = trim($mailParams['toName']);
1269 $toEmail = trim($mailParams['toEmail']);
1270 if ($toName == $toEmail ||
1271 strpos($toName, '@') !== FALSE
1276 $toName = CRM_Utils_Mail
::formatRFC2822Name($toName);
1279 $headers['To'] = "$toName <$toEmail>";
1281 $headers['Precedence'] = 'bulk';
1282 // Will test in the mail processor if the X-VERP is set in the bounced email.
1283 // (As an option to replace real VERP for those that can't set it up)
1284 $headers['X-CiviMail-Bounce'] = $verp['bounce'];
1287 //token replacement of subject
1288 $headers['Subject'] = $mailParams['Subject'];
1290 CRM_Utils_Mail
::setMimeParams($message);
1291 $headers = $message->headers($headers);
1293 //get formatted recipient
1294 $recipient = $headers['To'];
1296 // make sure we unset a lot of stuff
1309 * Get mailing object and replaces subscribeInvite, domain and mailing tokens.
1312 * This is used by CiviMail but will be made redundant by FlexMailer/TokenProcessor.
1313 * @param CRM_Mailing_BAO_Mailing $mailing
1315 public static function tokenReplace(&$mailing) {
1316 $domain = CRM_Core_BAO_Domain
::getDomain();
1318 foreach (['text', 'html'] as $type) {
1319 $tokens = $mailing->getTokens();
1320 if (isset($mailing->templates
[$type])) {
1321 $mailing->templates
[$type] = CRM_Utils_Token
::replaceSubscribeInviteTokens($mailing->templates
[$type]);
1322 $mailing->templates
[$type] = CRM_Utils_Token
::replaceDomainTokens(
1323 $mailing->templates
[$type],
1325 $type == 'html' ?
TRUE : FALSE,
1328 $mailing->templates
[$type] = CRM_Utils_Token
::replaceMailingTokens($mailing->templates
[$type], $mailing, NULL, $tokens[$type]);
1334 * Get data to resolve tokens.
1337 * This is used by CiviMail but will be made redundant by FlexMailer/TokenProcessor.
1339 * @param array $token_a
1341 * Whether to encode the token result for use in HTML email
1342 * @param array $contact
1343 * @param string $verp
1344 * @param array $urls
1345 * @param int $event_queue_id
1347 * @return bool|mixed|null|string
1349 private function getTokenData(&$token_a, $html = FALSE, &$contact, &$verp, &$urls, $event_queue_id) {
1350 $type = $token_a['type'];
1351 $token = $token_a['token'];
1354 $useSmarty = defined('CIVICRM_MAIL_SMARTY') && CIVICRM_MAIL_SMARTY ?
TRUE : FALSE;
1356 if ($type == 'embedded_url') {
1358 foreach ($token as $t) {
1359 $embed_data[] = $this->getTokenData($t, $html, $contact, $verp, $urls, $event_queue_id);
1361 $numSlices = count($embed_data);
1363 for ($i = 0; $i < $numSlices; $i++
) {
1364 $embed_url_data = parse_url($embed_data[$i]);
1365 if (!empty($embed_url_data['scheme'])) {
1366 $token_a['embed_parts'][$i] = preg_replace("/href=\"(https*:\/\/)/", "href=\"", $token_a['embed_parts'][$i]);
1368 $url .= "{$token_a['embed_parts'][$i]}{$embed_data[$i]}";
1370 if (isset($token_a['embed_parts'][$numSlices])) {
1371 $url .= $token_a['embed_parts'][$numSlices];
1373 // add trailing quote since we've gobbled it up in a previous regex
1374 // function getPatterns, line 431
1375 if (preg_match("/^href[ ]*=[ ]*'.*[^']$/", $url)) {
1378 elseif (preg_match('/^href[ ]*=[ ]*".*[^"]$/', $url)) {
1382 // CRM-20206 Fix ampersand encoding in plain text emails
1384 $data = CRM_Utils_String
::unstupifyUrl($data);
1387 elseif ($type == 'url') {
1388 if ($this->url_tracking
&& !empty($this->id
)) {
1389 // ensure that Google CSS and any .css files are not tracked.
1390 if (!(strpos($token, 'css?family') ||
strpos($token, '.css'))) {
1391 $data = CRM_Mailing_BAO_TrackableURL
::getTrackerURL($token, $this->id
, $event_queue_id);
1392 if (!empty($html)) {
1393 $data = htmlentities($data, ENT_NOQUOTES
);
1401 elseif ($type == 'contact') {
1402 $data = CRM_Utils_Token
::getContactTokenReplacement($token, $contact, FALSE, FALSE, $useSmarty);
1404 elseif ($type == 'action') {
1405 $data = CRM_Utils_Token
::getActionTokenReplacement($token, $verp, $urls, $html);
1407 elseif ($type == 'domain') {
1408 $domain = CRM_Core_BAO_Domain
::getDomain();
1409 $data = CRM_Utils_Token
::getDomainTokenReplacement($token, $domain, $html);
1411 elseif ($type == 'mailing') {
1412 if ($token == 'name') {
1413 $data = $this->name
;
1415 elseif ($token == 'group') {
1416 $groups = $this->getGroupNames();
1417 $data = implode(', ', $groups);
1421 $data = CRM_Utils_Array
::value("{$type}.{$token}", $contact);
1427 * Return a list of group names for this mailing. Does not work with
1428 * prior-mailing targets.
1431 * Names of groups receiving this mailing
1433 public function &getGroupNames() {
1434 if (!isset($this->id
)) {
1437 $mg = new CRM_Mailing_DAO_MailingGroup();
1438 $mgtable = CRM_Mailing_DAO_MailingGroup
::getTableName();
1439 $group = CRM_Contact_BAO_Group
::getTableName();
1441 $mg->query("SELECT $group.title as name FROM $mgtable
1442 INNER JOIN $group ON $mgtable.entity_id = $group.id
1443 WHERE $mgtable.mailing_id = {$this->id}
1444 AND $mgtable.entity_table = '$group'
1445 AND $mgtable.group_type = 'Include'
1446 ORDER BY $group.name");
1449 while ($mg->fetch()) {
1450 $groups[] = $mg->name
;
1458 * @param array $params
1459 * Reference array contains the values submitted by the form.
1461 * Reference array contains the id.
1464 * @return CRM_Mailing_DAO_Mailing
1466 public static function add(&$params, $ids = []) {
1467 $id = CRM_Utils_Array
::value('id', $params, CRM_Utils_Array
::value('mailing_id', $ids));
1469 if (empty($params['id']) && !empty($ids)) {
1470 \Civi
::log('Parameter $ids is no longer used by Mailing::add. Use the api or just pass $params', ['civi.tag' => 'deprecated']);
1474 CRM_Utils_Hook
::pre('edit', 'Mailing', $id, $params);
1477 CRM_Utils_Hook
::pre('create', 'Mailing', NULL, $params);
1480 $mailing = new static();
1483 $mailing->find(TRUE);
1485 $mailing->domain_id
= CRM_Utils_Array
::value('domain_id', $params, CRM_Core_Config
::domainID());
1487 if (((!$id && empty($params['replyto_email'])) ||
!isset($params['replyto_email'])) &&
1488 isset($params['from_email'])
1490 $params['replyto_email'] = $params['from_email'];
1492 $mailing->copyValues($params);
1494 // CRM-20892 Unset Modifed Date here so that MySQL can correctly set an updated modfied date.
1495 unset($mailing->modified_date
);
1496 $result = $mailing->save();
1498 // CRM-20892 Re find record after saing so we can set the updated modified date in the result.
1499 $mailing->find(TRUE);
1501 if (isset($mailing->modified_date
)) {
1502 $result->modified_date
= $mailing->modified_date
;
1506 CRM_Utils_Hook
::post('edit', 'Mailing', $mailing->id
, $mailing);
1509 CRM_Utils_Hook
::post('create', 'Mailing', $mailing->id
, $mailing);
1516 * Construct a new mailing object, along with job and mailing_group
1517 * objects, from the form values of the create mailing wizard.
1519 * This function is a bit evil. It not only merges $params and saves
1520 * the mailing -- it also schedules the mailing and chooses the recipients.
1521 * Since it merges $params, it's also the only place to correctly trigger
1522 * multi-field validation. It should be broken up.
1524 * In the mean time, use-cases which break under the weight of this
1525 * evil may find reprieve in these extra evil params:
1527 * - _skip_evil_bao_auto_recipients_: bool
1528 * - _skip_evil_bao_auto_schedule_: bool
1529 * - _evil_bao_validator_: string|callable
1531 * </twowrongsmakesaright>
1533 * @params array $params
1536 * @param array $params
1540 * $mailing The new mailing object
1541 * @throws \Exception
1543 public static function create(&$params, $ids = []) {
1545 if (empty($params['id']) && (array_filter($ids) !== [])) {
1546 $params['id'] = isset($ids['mailing_id']) ?
$ids['mailing_id'] : $ids['id'];
1547 \Civi
::log('Parameter $ids is no longer used by Mailing::create. Use the api or just pass $params', ['civi.tag' => 'deprecated']);
1551 // Do the below only for an insert
1552 // for an update, we should not set the defaults
1553 if (!isset($params['id'])) {
1554 // Retrieve domain email and name for default sender
1555 $domain = civicrm_api(
1560 'current_domain' => 1,
1564 if (isset($domain['from_email'])) {
1565 $domain_email = $domain['from_email'];
1566 $domain_name = $domain['from_name'];
1569 $domain_email = 'info@EXAMPLE.ORG';
1570 $domain_name = 'EXAMPLE.ORG';
1572 if (!isset($params['created_id'])) {
1573 $session =& CRM_Core_Session
::singleton();
1574 $params['created_id'] = $session->get('userID');
1577 // load the default config settings for each
1578 // eg reply_id, unsubscribe_id need to use
1579 // correct template IDs here
1580 'override_verp' => TRUE,
1581 'forward_replies' => FALSE,
1582 'open_tracking' => TRUE,
1583 'url_tracking' => TRUE,
1584 'visibility' => 'Public Pages',
1585 'replyto_email' => $domain_email,
1586 'header_id' => CRM_Mailing_PseudoConstant
::defaultComponent('header_id', ''),
1587 'footer_id' => CRM_Mailing_PseudoConstant
::defaultComponent('footer_id', ''),
1588 'from_email' => $domain_email,
1589 'from_name' => $domain_name,
1590 'msg_template_id' => NULL,
1591 'created_id' => $params['created_id'],
1592 'approver_id' => NULL,
1593 'auto_responder' => 0,
1594 'created_date' => date('YmdHis'),
1595 'scheduled_date' => NULL,
1596 'approval_date' => NULL,
1599 // Get the default from email address, if not provided.
1600 if (empty($defaults['from_email'])) {
1601 $defaultAddress = CRM_Core_BAO_Domain
::getNameAndEmail(TRUE, TRUE);
1602 foreach ($defaultAddress as $id => $value) {
1603 if (preg_match('/"(.*)" <(.*)>/', $value, $match)) {
1604 $defaults['from_email'] = $match[2];
1605 $defaults['from_name'] = $match[1];
1610 $params = array_merge($defaults, $params);
1614 * Could check and warn for the following cases:
1616 * - groups OR mailings should be populated.
1617 * - body html OR body text should be populated.
1620 $transaction = new CRM_Core_Transaction();
1622 $mailing = self
::add($params);
1624 if (is_a($mailing, 'CRM_Core_Error')) {
1625 $transaction->rollback();
1628 // update mailings with hash values
1629 CRM_Contact_BAO_Contact_Utils
::generateChecksum($mailing->id
, NULL, NULL, NULL, 'mailing', 16);
1631 $groupTableName = CRM_Contact_BAO_Group
::getTableName();
1633 /* Create the mailing group record */
1634 $mg = new CRM_Mailing_DAO_MailingGroup();
1636 'include' => 'Include',
1637 'exclude' => 'Exclude',
1640 foreach (['groups', 'mailings'] as $entity) {
1641 foreach (['include', 'exclude', 'base'] as $type) {
1642 if (isset($params[$entity][$type])) {
1643 self
::replaceGroups($mailing->id
, $groupTypes[$type], $entity, $params[$entity][$type]);
1648 if (!empty($params['search_id']) && !empty($params['group_id'])) {
1650 $mg->mailing_id
= $mailing->id
;
1651 $mg->entity_table
= $groupTableName;
1652 $mg->entity_id
= $params['group_id'];
1653 $mg->search_id
= $params['search_id'];
1654 $mg->search_args
= $params['search_args'];
1655 $mg->group_type
= 'Include';
1659 // check and attach and files as needed
1660 CRM_Core_BAO_File
::processAttachment($params, 'civicrm_mailing', $mailing->id
);
1662 // If we're going to autosend, then check validity before saving.
1663 if (empty($params['is_completed']) && !empty($params['scheduled_date']) && $params['scheduled_date'] != 'null' && !empty($params['_evil_bao_validator_'])) {
1664 $cb = Civi\Core\Resolver
::singleton()
1665 ->get($params['_evil_bao_validator_']);
1666 $errors = call_user_func($cb, $mailing);
1667 if (!empty($errors)) {
1668 $fields = implode(',', array_keys($errors));
1669 throw new CRM_Core_Exception("Mailing cannot be sent. There are missing or invalid fields ($fields).", 'cannot-send', $errors);
1673 $transaction->commit();
1675 // Create parent job if not yet created.
1676 // Condition on the existence of a scheduled date.
1677 if (!empty($params['scheduled_date']) && $params['scheduled_date'] != 'null' && empty($params['_skip_evil_bao_auto_schedule_'])) {
1678 $job = new CRM_Mailing_BAO_MailingJob();
1679 $job->mailing_id
= $mailing->id
;
1680 // If we are creating a new Completed mailing (e.g. import from another system) set the job to completed.
1681 // Keeping former behaviour when an id is present is precautionary and may warrant reconsideration later.
1682 $job->status
= ((empty($params['is_completed']) ||
!empty($params['id'])) ?
'Scheduled' : 'Complete');
1685 if (!$job->find(TRUE)) {
1686 // Don't schedule job until we populate the recipients.
1687 $job->scheduled_date
= NULL;
1690 // Schedule the job now that it has recipients.
1691 $job->scheduled_date
= $params['scheduled_date'];
1695 // Populate the recipients.
1696 if (empty($params['_skip_evil_bao_auto_recipients_'])) {
1697 self
::getRecipients($mailing->id
);
1705 * This is used by CiviMail but will be made redundant by FlexMailer.
1706 * @param CRM_Mailing_DAO_Mailing $mailing
1707 * The mailing which may or may not be sendable.
1709 * List of error messages.
1711 public static function checkSendable($mailing) {
1713 foreach (['subject', 'name', 'from_name', 'from_email'] as $field) {
1714 if (empty($mailing->{$field})) {
1715 $errors[$field] = ts('Field "%1" is required.', [
1720 if (empty($mailing->body_html
) && empty($mailing->body_text
)) {
1721 $errors['body'] = ts('Field "body_html" or "body_text" is required.');
1724 if (!Civi
::settings()->get('disable_mandatory_tokens_check')) {
1725 $header = $mailing->header_id
&& $mailing->header_id
!= 'null' ? CRM_Mailing_BAO_MailingComponent
::findById($mailing->header_id
) : NULL;
1726 $footer = $mailing->footer_id
&& $mailing->footer_id
!= 'null' ? CRM_Mailing_BAO_MailingComponent
::findById($mailing->footer_id
) : NULL;
1727 foreach (['body_html', 'body_text'] as $field) {
1728 if (empty($mailing->{$field})) {
1731 $str = ($header ?
$header->{$field} : '') . $mailing->{$field} . ($footer ?
$footer->{$field} : '');
1732 $err = CRM_Utils_Token
::requiredTokens($str);
1733 if ($err !== TRUE) {
1734 foreach ($err as $token => $desc) {
1735 $errors["{$field}:{$token}"] = ts('This message is missing a required token - {%1}: %2',
1736 [1 => $token, 2 => $desc]
1747 * Replace the list of recipients on a given mailing.
1749 * @param int $mailingId
1750 * @param string $type
1751 * 'include' or 'exclude'.
1752 * @param string $entity
1753 * 'groups' or 'mailings'.
1754 * @param array $entityIds
1755 * @throws CiviCRM_API3_Exception
1757 public static function replaceGroups($mailingId, $type, $entity, $entityIds) {
1759 foreach ($entityIds as $entityId) {
1760 $values[] = ['entity_id' => $entityId];
1762 civicrm_api3('mailing_group', 'replace', [
1763 'mailing_id' => $mailingId,
1764 'group_type' => $type,
1765 'entity_table' => ($entity == 'groups') ? CRM_Contact_BAO_Group
::getTableName() : CRM_Mailing_BAO_Mailing
::getTableName(),
1766 'values' => $values,
1771 * Get hash value of the mailing.
1775 * @return null|string
1777 public static function getMailingHash($id) {
1779 if (Civi
::settings()->get('hash_mailing_url') && !empty($id)) {
1780 $hash = CRM_Core_DAO
::getFieldValue('CRM_Mailing_BAO_Mailing', $id, 'hash', 'id');
1786 * Generate a report. Fetch event count information, mailing data, and job
1790 * The mailing id to report.
1791 * @param bool $skipDetails
1792 * Whether return all detailed report.
1794 * @param bool $isSMS
1797 * Associative array of reporting data
1799 public static function &report($id, $skipDetails = FALSE, $isSMS = FALSE) {
1800 $mailing_id = CRM_Utils_Type
::escape($id, 'Integer');
1802 $mailing = new CRM_Mailing_BAO_Mailing();
1805 'mailing' => self
::getTableName(),
1806 'mailing_group' => CRM_Mailing_DAO_MailingGroup
::getTableName(),
1807 'group' => CRM_Contact_BAO_Group
::getTableName(),
1808 'job' => CRM_Mailing_BAO_MailingJob
::getTableName(),
1809 'queue' => CRM_Mailing_Event_BAO_Queue
::getTableName(),
1810 'delivered' => CRM_Mailing_Event_BAO_Delivered
::getTableName(),
1811 'opened' => CRM_Mailing_Event_BAO_Opened
::getTableName(),
1812 'reply' => CRM_Mailing_Event_BAO_Reply
::getTableName(),
1813 'unsubscribe' => CRM_Mailing_Event_BAO_Unsubscribe
::getTableName(),
1814 'bounce' => CRM_Mailing_Event_BAO_Bounce
::getTableName(),
1815 'forward' => CRM_Mailing_Event_BAO_Forward
::getTableName(),
1816 'url' => CRM_Mailing_BAO_TrackableURL
::getTableName(),
1817 'urlopen' => CRM_Mailing_Event_BAO_TrackableURLOpen
::getTableName(),
1818 'component' => CRM_Mailing_BAO_MailingComponent
::getTableName(),
1819 'spool' => CRM_Mailing_BAO_Spool
::getTableName(),
1823 $additionalWhereClause = " AND ";
1825 $additionalWhereClause .= " {$t['mailing']}.sms_provider_id IS NULL ";
1828 $additionalWhereClause .= " {$t['mailing']}.sms_provider_id IS NOT NULL ";
1831 /* Get the mailing info */
1834 SELECT {$t['mailing']}.*
1835 FROM {$t['mailing']}
1836 WHERE {$t['mailing']}.id = $mailing_id {$additionalWhereClause}");
1840 $report['mailing'] = [];
1841 foreach (array_keys(self
::fields()) as $field) {
1842 $field = self
::fields()[$field]['name'];
1843 $report['mailing'][$field] = $mailing->$field;
1847 if ($campaignId = CRM_Utils_Array
::value('campaign_id', $report['mailing'])) {
1848 $campaigns = CRM_Campaign_BAO_Campaign
::getCampaigns($campaignId);
1849 $report['mailing']['campaign'] = $campaigns[$campaignId];
1852 //mailing report is called by activity
1853 //we dont need all detail report
1858 /* Get the component info */
1863 'header' => ts('Header'),
1864 'footer' => ts('Footer'),
1865 'reply' => ts('Reply'),
1866 'optout' => ts('Opt-Out'),
1867 'resubscribe' => ts('Resubscribe'),
1868 'unsubscribe' => ts('Unsubscribe'),
1870 foreach (array_keys($components) as $type) {
1871 $query[] = "SELECT {$t['component']}.name as name,
1873 {$t['component']}.id as id
1874 FROM {$t['component']}
1875 INNER JOIN {$t['mailing']}
1876 ON {$t['mailing']}.{$type}_id =
1877 {$t['component']}.id
1878 WHERE {$t['mailing']}.id = $mailing_id";
1880 $q = '(' . implode(') UNION (', $query) . ')';
1881 $mailing->query($q);
1883 $report['component'] = [];
1884 while ($mailing->fetch()) {
1885 $report['component'][] = [
1886 'type' => $components[$mailing->type
],
1887 'name' => $mailing->name
,
1888 'link' => CRM_Utils_System
::url('civicrm/mailing/component', "reset=1&action=update&id={$mailing->id}"),
1892 /* Get the recipient group info */
1895 SELECT {$t['mailing_group']}.group_type as group_type,
1896 {$t['group']}.id as group_id,
1897 {$t['group']}.title as group_title,
1898 {$t['group']}.is_hidden as group_hidden,
1899 {$t['mailing']}.id as mailing_id,
1900 {$t['mailing']}.name as mailing_name
1901 FROM {$t['mailing_group']}
1902 LEFT JOIN {$t['group']}
1903 ON {$t['mailing_group']}.entity_id = {$t['group']}.id
1904 AND {$t['mailing_group']}.entity_table =
1906 LEFT JOIN {$t['mailing']}
1907 ON {$t['mailing_group']}.entity_id =
1909 AND {$t['mailing_group']}.entity_table =
1912 WHERE {$t['mailing_group']}.mailing_id = $mailing_id
1915 $report['group'] = ['include' => [], 'exclude' => [], 'base' => []];
1916 while ($mailing->fetch()) {
1918 if (isset($mailing->group_id
)) {
1919 $row['id'] = $mailing->group_id
;
1920 $row['name'] = $mailing->group_title
;
1921 $row['link'] = CRM_Utils_System
::url('civicrm/group/search',
1922 "reset=1&force=1&context=smog&gid={$row['id']}"
1926 $row['id'] = $mailing->mailing_id
;
1927 $row['name'] = $mailing->mailing_name
;
1928 $row['mailing'] = TRUE;
1929 $row['link'] = CRM_Utils_System
::url('civicrm/mailing/report',
1934 /* Rename hidden groups */
1936 if ($mailing->group_hidden
== 1) {
1937 $row['name'] = "Search Results";
1940 if ($mailing->group_type
== 'Include') {
1941 $report['group']['include'][] = $row;
1943 elseif ($mailing->group_type
== 'Base') {
1944 $report['group']['base'][] = $row;
1947 $report['group']['exclude'][] = $row;
1951 /* Get the event totals, grouped by job (retries) */
1954 SELECT {$t['job']}.*,
1955 COUNT(DISTINCT {$t['queue']}.id) as queue,
1956 COUNT(DISTINCT {$t['delivered']}.id) as delivered,
1957 COUNT(DISTINCT {$t['reply']}.id) as reply,
1958 COUNT(DISTINCT {$t['forward']}.id) as forward,
1959 COUNT(DISTINCT {$t['bounce']}.id) as bounce,
1960 COUNT(DISTINCT {$t['urlopen']}.id) as url,
1961 COUNT(DISTINCT {$t['spool']}.id) as spool
1963 LEFT JOIN {$t['queue']}
1964 ON {$t['queue']}.job_id = {$t['job']}.id
1965 LEFT JOIN {$t['reply']}
1966 ON {$t['reply']}.event_queue_id = {$t['queue']}.id
1967 LEFT JOIN {$t['forward']}
1968 ON {$t['forward']}.event_queue_id = {$t['queue']}.id
1969 LEFT JOIN {$t['bounce']}
1970 ON {$t['bounce']}.event_queue_id = {$t['queue']}.id
1971 LEFT JOIN {$t['delivered']}
1972 ON {$t['delivered']}.event_queue_id = {$t['queue']}.id
1973 AND {$t['bounce']}.id IS null
1974 LEFT JOIN {$t['urlopen']}
1975 ON {$t['urlopen']}.event_queue_id = {$t['queue']}.id
1976 LEFT JOIN {$t['spool']}
1977 ON {$t['spool']}.job_id = {$t['job']}.id
1978 WHERE {$t['job']}.mailing_id = $mailing_id
1979 AND {$t['job']}.is_test = 0
1980 GROUP BY {$t['job']}.id");
1982 $report['jobs'] = [];
1983 $report['event_totals'] = [];
1984 $path = 'civicrm/mailing/report/event';
1999 // initialize various counters
2000 foreach ($elements as $field) {
2001 $report['event_totals'][$field] = 0;
2004 while ($mailing->fetch()) {
2006 foreach ($elements as $field) {
2007 if (isset($mailing->$field)) {
2008 $row[$field] = $mailing->$field;
2009 $report['event_totals'][$field] +
= $mailing->$field;
2013 // compute open total separately to discount duplicates
2015 $row['opened'] = CRM_Mailing_Event_BAO_Opened
::getTotalCount($mailing_id, $mailing->id
, TRUE);
2016 $report['event_totals']['opened'] +
= $row['opened'];
2017 $row['total_opened'] = CRM_Mailing_Event_BAO_Opened
::getTotalCount($mailing_id, $mailing->id
);
2018 $report['event_totals']['total_opened'] +
= $row['total_opened'];
2020 // compute unsub total separately to discount duplicates
2022 $row['unsubscribe'] = CRM_Mailing_Event_BAO_Unsubscribe
::getTotalCount($mailing_id, $mailing->id
, TRUE, TRUE);
2023 $report['event_totals']['unsubscribe'] +
= $row['unsubscribe'];
2025 $row['optout'] = CRM_Mailing_Event_BAO_Unsubscribe
::getTotalCount($mailing_id, $mailing->id
, TRUE, FALSE);
2026 $report['event_totals']['optout'] +
= $row['optout'];
2028 foreach (array_keys(CRM_Mailing_BAO_MailingJob
::fields()) as $field) {
2029 // Get the field name from the MailingJob fields as that will not have any prefixing.
2031 $field = CRM_Mailing_BAO_MailingJob
::fields()[$field]['name'];
2032 $row[$field] = $mailing->$field;
2035 if ($mailing->queue
) {
2036 $row['delivered_rate'] = (100.0 * $mailing->delivered
) / $mailing->queue
;
2037 $row['bounce_rate'] = (100.0 * $mailing->bounce
) / $mailing->queue
;
2038 $row['unsubscribe_rate'] = (100.0 * $row['unsubscribe']) / $mailing->queue
;
2039 $row['optout_rate'] = (100.0 * $row['optout']) / $mailing->queue
;
2040 $row['opened_rate'] = $mailing->delivered ?
(($row['opened'] / $mailing->delivered
) * 100.0) : 0;
2041 $row['clickthrough_rate'] = $mailing->delivered ?
(($mailing->url
/ $mailing->delivered
) * 100.0) : 0;
2044 $row['delivered_rate'] = 0;
2045 $row['bounce_rate'] = 0;
2046 $row['unsubscribe_rate'] = 0;
2047 $row['optout_rate'] = 0;
2048 $row['opened_rate'] = 0;
2049 $row['clickthrough_rate'] = 0;
2052 $arg = "reset=1&mid=$mailing_id&jid={$mailing->id}";
2054 'clicks' => CRM_Utils_System
::url($path, "$arg&event=click"),
2055 'queue' => CRM_Utils_System
::url($path, "$arg&event=queue"),
2056 'delivered' => CRM_Utils_System
::url($path, "$arg&event=delivered"),
2057 'bounce' => CRM_Utils_System
::url($path, "$arg&event=bounce"),
2058 'unsubscribe' => CRM_Utils_System
::url($path, "$arg&event=unsubscribe"),
2059 'forward' => CRM_Utils_System
::url($path, "$arg&event=forward"),
2060 'reply' => CRM_Utils_System
::url($path, "$arg&event=reply"),
2061 'opened' => CRM_Utils_System
::url($path, "$arg&event=opened"),
2064 foreach (['scheduled_date', 'start_date', 'end_date'] as $key) {
2065 $row[$key] = CRM_Utils_Date
::customFormat($row[$key]);
2067 $report['jobs'][] = $row;
2070 $newTableSize = CRM_Mailing_BAO_Recipients
::mailingSize($mailing_id);
2072 // we need to do this for backward compatibility, since old mailings did not
2073 // use the mailing_recipients table
2074 if ($newTableSize > 0) {
2075 $report['event_totals']['queue'] = $newTableSize;
2078 $report['event_totals']['queue'] = self
::getRecipientsCount($mailing_id);
2081 if (!empty($report['event_totals']['queue'])) {
2082 $report['event_totals']['delivered_rate'] = (100.0 * $report['event_totals']['delivered']) / $report['event_totals']['queue'];
2083 $report['event_totals']['bounce_rate'] = (100.0 * $report['event_totals']['bounce']) / $report['event_totals']['queue'];
2084 $report['event_totals']['unsubscribe_rate'] = (100.0 * $report['event_totals']['unsubscribe']) / $report['event_totals']['queue'];
2085 $report['event_totals']['optout_rate'] = (100.0 * $report['event_totals']['optout']) / $report['event_totals']['queue'];
2086 $report['event_totals']['opened_rate'] = !empty($report['event_totals']['delivered']) ?
(($report['event_totals']['opened'] / $report['event_totals']['delivered']) * 100.0) : 0;
2087 $report['event_totals']['clickthrough_rate'] = !empty($report['event_totals']['delivered']) ?
(($report['event_totals']['url'] / $report['event_totals']['delivered']) * 100.0) : 0;
2090 $report['event_totals']['delivered_rate'] = 0;
2091 $report['event_totals']['bounce_rate'] = 0;
2092 $report['event_totals']['unsubscribe_rate'] = 0;
2093 $report['event_totals']['optout_rate'] = 0;
2094 $report['event_totals']['opened_rate'] = 0;
2095 $report['event_totals']['clickthrough_rate'] = 0;
2098 /* Get the click-through totals, grouped by URL */
2101 SELECT {$t['url']}.url,
2103 COUNT({$t['urlopen']}.id) as clicks,
2104 COUNT(DISTINCT {$t['queue']}.id) as unique_clicks
2106 LEFT JOIN {$t['urlopen']}
2107 ON {$t['urlopen']}.trackable_url_id = {$t['url']}.id
2108 LEFT JOIN {$t['queue']}
2109 ON {$t['urlopen']}.event_queue_id = {$t['queue']}.id
2110 LEFT JOIN {$t['job']}
2111 ON {$t['queue']}.job_id = {$t['job']}.id
2112 WHERE {$t['url']}.mailing_id = $mailing_id
2113 AND {$t['job']}.is_test = 0
2114 GROUP BY {$t['url']}.id
2115 ORDER BY unique_clicks DESC");
2117 $report['click_through'] = [];
2119 while ($mailing->fetch()) {
2120 $report['click_through'][] = [
2121 'url' => $mailing->url
,
2122 'link' => CRM_Utils_System
::url($path, "reset=1&event=click&mid=$mailing_id&uid={$mailing->id}"),
2123 'link_unique' => CRM_Utils_System
::url($path, "reset=1&event=click&mid=$mailing_id&uid={$mailing->id}&distinct=1"),
2124 'clicks' => $mailing->clicks
,
2125 'unique' => $mailing->unique_clicks
,
2126 'rate' => CRM_Utils_Array
::value('delivered', $report['event_totals']) ?
(100.0 * $mailing->unique_clicks
) / $report['event_totals']['delivered'] : 0,
2127 'report' => CRM_Report_Utils_Report
::getNextUrl('mailing/clicks', "reset=1&mailing_id_value={$mailing_id}&url_value={$mailing->url}", FALSE, TRUE),
2131 $arg = "reset=1&mid=$mailing_id";
2132 $report['event_totals']['links'] = [
2133 'clicks' => CRM_Utils_System
::url($path, "$arg&event=click"),
2134 'clicks_unique' => CRM_Utils_System
::url($path, "$arg&event=click&distinct=1"),
2135 'queue' => CRM_Utils_System
::url($path, "$arg&event=queue"),
2136 'delivered' => CRM_Utils_System
::url($path, "$arg&event=delivered"),
2137 'bounce' => CRM_Utils_System
::url($path, "$arg&event=bounce"),
2138 'unsubscribe' => CRM_Utils_System
::url($path, "$arg&event=unsubscribe"),
2139 'optout' => CRM_Utils_System
::url($path, "$arg&event=optout"),
2140 'forward' => CRM_Utils_System
::url($path, "$arg&event=forward"),
2141 'reply' => CRM_Utils_System
::url($path, "$arg&event=reply"),
2142 'opened' => CRM_Utils_System
::url($path, "$arg&event=opened"),
2145 $actionLinks = [CRM_Core_Action
::VIEW
=> ['name' => ts('Report')]];
2146 $actionLinks[CRM_Core_Action
::ADVANCED
] = [
2147 'name' => ts('Advanced Search'),
2148 'url' => 'civicrm/contact/search/advanced',
2150 $action = array_sum(array_keys($actionLinks));
2152 $report['event_totals']['actionlinks'] = [];
2153 foreach (['clicks', 'clicks_unique', 'queue', 'delivered', 'bounce', 'unsubscribe', 'forward', 'reply', 'opened', 'opened_unique', 'optout'] as $key) {
2154 $url = 'mailing/detail';
2155 $reportFilter = "reset=1&mailing_id_value={$mailing_id}";
2156 $searchFilter = "force=1&mailing_id=%%mid%%";
2159 $reportFilter .= "&delivery_status_value=successful";
2160 $searchFilter .= "&mailing_delivery_status=Y";
2164 $url = "mailing/bounce";
2165 $searchFilter .= "&mailing_delivery_status=N";
2169 $reportFilter .= "&is_forwarded_value=1";
2170 $searchFilter .= "&mailing_forward=1";
2174 $reportFilter .= "&is_replied_value=1";
2175 $searchFilter .= "&mailing_reply_status=Y";
2179 $reportFilter .= "&is_unsubscribed_value=1";
2180 $searchFilter .= "&mailing_unsubscribe=1";
2184 $reportFilter .= "&is_optout_value=1";
2185 $searchFilter .= "&mailing_optout=1";
2189 // do not use group by clause in report, because same report used for total and unique open
2190 $reportFilter .= "&distinct=0";
2191 case 'opened_unique':
2192 $url = "mailing/opened";
2193 $searchFilter .= "&mailing_open_status=Y";
2197 case 'clicks_unique':
2198 $url = "mailing/clicks";
2199 $searchFilter .= "&mailing_click_status=Y";
2202 $actionLinks[CRM_Core_Action
::VIEW
]['url'] = CRM_Report_Utils_Report
::getNextUrl($url, $reportFilter, FALSE, TRUE);
2203 if (array_key_exists(CRM_Core_Action
::ADVANCED
, $actionLinks)) {
2204 $actionLinks[CRM_Core_Action
::ADVANCED
]['qs'] = $searchFilter;
2206 $report['event_totals']['actionlinks'][$key] = CRM_Core_Action
::formLink(
2209 ['mid' => $mailing_id],
2212 'mailing.report.action',
2222 * Get the count of mailings.
2229 public function getCount() {
2231 $this->selectAdd('COUNT(id) as count');
2233 $session = CRM_Core_Session
::singleton();
2236 return $this->count
;
2244 public static function checkPermission($id) {
2249 $mailingIDs = self
::mailingACLIDs();
2250 if ($mailingIDs === TRUE) {
2254 if (!in_array($id, $mailingIDs)) {
2255 throw new CRM_Core_Exception(ts('You do not have permission to access this mailing report'));
2260 * @param null $alias
2264 public static function mailingACL($alias = NULL) {
2265 $mailingACL = " ( 0 ) ";
2267 $mailingIDs = self
::mailingACLIDs();
2268 if ($mailingIDs === TRUE) {
2272 if (!empty($mailingIDs)) {
2273 $mailingIDs = implode(',', $mailingIDs);
2274 $tableName = !$alias ? self
::getTableName() : $alias;
2275 $mailingACL = " $tableName.id IN ( $mailingIDs ) ";
2281 * Returns all the mailings that this user can access. This is dependent on
2282 * all the groups that the user has access to.
2283 * However since most civi installs dont use ACL's we special case the condition
2284 * where the user has access to ALL groups, and hence ALL mailings and return a
2285 * value of TRUE (to avoid the downstream where clause with a list of mailing list IDs
2287 * @return bool|array
2288 * TRUE if the user has access to all mailings, else array of mailing IDs (possibly empty).
2290 public static function mailingACLIDs() {
2292 // optimize common case where admin has access
2295 CRM_Core_Permission
::check('view all contacts') ||
2296 CRM_Core_Permission
::check('edit all contacts')
2303 // get all the groups that this user can access
2304 // if they dont have universal access
2305 $groupNames = civicrm_api3('Group', 'get', [
2306 'check_permissions' => TRUE,
2307 'return' => ['title', 'id'],
2308 'options' => ['limit' => 0],
2310 foreach ($groupNames['values'] as $group) {
2311 $groups[$group['id']] = $group['title'];
2313 if (!empty($groups)) {
2314 $groupIDs = implode(',', array_keys($groups));
2315 $domain_id = CRM_Core_Config
::domainID();
2317 // get all the mailings that are in this subset of groups
2319 SELECT DISTINCT( m.id ) as id
2320 FROM civicrm_mailing m
2321 LEFT JOIN civicrm_mailing_group g ON g.mailing_id = m.id
2322 WHERE ( ( g.entity_table like 'civicrm_group%' AND g.entity_id IN ( $groupIDs ) )
2323 OR ( g.entity_table IS NULL AND g.entity_id IS NULL AND m.domain_id = $domain_id ) )
2325 $dao = CRM_Core_DAO
::executeQuery($query);
2328 while ($dao->fetch()) {
2329 $mailingIDs[] = $dao->id
;
2331 //CRM-18181 Get all mailings that use the mailings found earlier as receipients
2332 if (!empty($mailingIDs)) {
2333 $mailings = implode(',', $mailingIDs);
2335 SELECT DISTINCT ( m.id ) as id
2336 FROM civicrm_mailing m
2337 LEFT JOIN civicrm_mailing_group g ON g.mailing_id = m.id
2338 WHERE g.entity_table like 'civicrm_mailing%' AND g.entity_id IN ($mailings)";
2339 $mailingDao = CRM_Core_DAO
::executeQuery($mailingQuery);
2340 while ($mailingDao->fetch()) {
2341 $mailingIDs[] = $mailingDao->id
;
2350 * Get the rows for a browse operation.
2352 * @param int $offset
2353 * The row number to start from.
2354 * @param int $rowCount
2355 * The nmber of rows to return.
2356 * @param string $sort
2357 * The sql string that describes the sort order.
2359 * @param null $additionalClause
2360 * @param array $additionalParams
2365 public function &getRows($offset, $rowCount, $sort, $additionalClause = NULL, $additionalParams = NULL) {
2366 $mailing = self
::getTableName();
2367 $job = CRM_Mailing_BAO_MailingJob
::getTableName();
2368 $group = CRM_Mailing_DAO_MailingGroup
::getTableName();
2369 $session = CRM_Core_Session
::singleton();
2371 $mailingACL = self
::mailingACL();
2373 //get all campaigns.
2374 $allCampaigns = CRM_Campaign_BAO_Campaign
::getCampaigns(NULL, NULL, FALSE, FALSE, FALSE, TRUE);
2379 "$mailing.approval_status_id",
2380 "createdContact.sort_name as created_by",
2381 "scheduledContact.sort_name as scheduled_by",
2382 "$mailing.created_id as created_id",
2383 "$mailing.scheduled_id as scheduled_id",
2384 "$mailing.is_archived as archived",
2385 "$mailing.created_date as created_date",
2387 "$mailing.sms_provider_id as sms_provider_id",
2388 "$mailing.language",
2391 // we only care about parent jobs, since that holds all the info on
2393 $selectClause = implode(', ', $select);
2394 $groupFromSelect = CRM_Contact_BAO_Query
::getGroupByFromSelectColumns($select, "$mailing.id");
2396 SELECT {$selectClause},
2397 MIN($job.scheduled_date) as scheduled_date,
2398 MIN($job.start_date) as start_date,
2399 MAX($job.end_date) as end_date
2401 LEFT JOIN $job ON ( $job.mailing_id = $mailing.id AND $job.is_test = 0 AND $job.parent_id IS NULL )
2402 LEFT JOIN civicrm_contact createdContact ON ( civicrm_mailing.created_id = createdContact.id )
2403 LEFT JOIN civicrm_contact scheduledContact ON ( civicrm_mailing.scheduled_id = scheduledContact.id )
2404 WHERE $mailingACL $additionalClause";
2406 if (!empty($groupFromSelect)) {
2407 $query .= $groupFromSelect;
2411 $orderBy = trim($sort->orderBy());
2412 if (!empty($orderBy)) {
2413 $query .= " ORDER BY $orderBy";
2418 $offset = CRM_Utils_Type
::escape($offset, 'Int');
2419 $rowCount = CRM_Utils_Type
::escape($rowCount, 'Int');
2421 $query .= " LIMIT $offset, $rowCount ";
2424 if (!$additionalParams) {
2425 $additionalParams = [];
2428 $dao = CRM_Core_DAO
::executeQuery($query, $additionalParams);
2431 while ($dao->fetch()) {
2434 'name' => $dao->name
,
2435 'status' => $dao->status ?
$dao->status
: 'Not scheduled',
2436 'created_date' => CRM_Utils_Date
::customFormat($dao->created_date
),
2437 'scheduled' => CRM_Utils_Date
::customFormat($dao->scheduled_date
),
2438 'scheduled_iso' => $dao->scheduled_date
,
2439 'start' => CRM_Utils_Date
::customFormat($dao->start_date
),
2440 'end' => CRM_Utils_Date
::customFormat($dao->end_date
),
2441 'created_by' => $dao->created_by
,
2442 'scheduled_by' => $dao->scheduled_by
,
2443 'created_id' => $dao->created_id
,
2444 'scheduled_id' => $dao->scheduled_id
,
2445 'archived' => $dao->archived
,
2446 'approval_status_id' => $dao->approval_status_id
,
2447 'campaign_id' => $dao->campaign_id
,
2448 'campaign' => empty($dao->campaign_id
) ?
NULL : $allCampaigns[$dao->campaign_id
],
2449 'sms_provider_id' => $dao->sms_provider_id
,
2450 'language' => $dao->language
,
2457 * Show detail Mailing report.
2463 public static function showEmailDetails($id) {
2464 return CRM_Utils_System
::url('civicrm/mailing/report', "mid=$id");
2468 * Delete Mails and all its associated records.
2471 * Id of the mail to delete.
2475 public static function del($id) {
2477 throw new CRM_Core_Exception(ts('No id passed to mailing del function'));
2480 CRM_Utils_Hook
::pre('delete', 'Mailing', $id, CRM_Core_DAO
::$_nullArray);
2482 // delete all file attachments
2483 CRM_Core_BAO_File
::deleteEntityFile('civicrm_mailing',
2487 $dao = new CRM_Mailing_DAO_Mailing();
2491 CRM_Core_Session
::setStatus(ts('Selected mailing has been deleted.'), ts('Deleted'), 'success');
2493 CRM_Utils_Hook
::post('delete', 'Mailing', $id, $dao);
2498 * Use CRM_Mailing_BAO_MailingJob::del($id)
2501 * Id of the Job to delete.
2505 public static function delJob($id) {
2507 throw new CRM_Core_Exception(ts('No id passed to mailing delJob function'));
2510 \Civi
::log('This function is deprecated, use CRM_Mailing_BAO_MailingJob::del instead', ['civi.tag' => 'deprecated']);
2512 CRM_Mailing_BAO_MailingJob
::del($id);
2517 * This is used by CiviMail but will be made redundant by FlexMailer/TokenProcessor.
2520 public function getReturnProperties() {
2521 $tokens = &$this->getTokens();
2524 if (isset($tokens['html']) &&
2525 isset($tokens['html']['contact'])
2527 $properties = array_merge($properties, $tokens['html']['contact']);
2530 if (isset($tokens['text']) &&
2531 isset($tokens['text']['contact'])
2533 $properties = array_merge($properties, $tokens['text']['contact']);
2536 if (isset($tokens['subject']) &&
2537 isset($tokens['subject']['contact'])
2539 $properties = array_merge($properties, $tokens['subject']['contact']);
2542 $returnProperties = [];
2543 $returnProperties['display_name'] = $returnProperties['contact_id'] = $returnProperties['preferred_mail_format'] = $returnProperties['hash'] = 1;
2545 foreach ($properties as $p) {
2546 $returnProperties[$p] = 1;
2549 return $returnProperties;
2553 * Build the compose mail form.
2555 * @param CRM_Core_Form $form
2559 public static function commonCompose(&$form) {
2563 if (method_exists($form, 'listTokens')) {
2564 $tokens = array_merge($form->listTokens(), $tokens);
2567 //sorted in ascending order tokens by ignoring word case
2568 $form->assign('tokens', CRM_Utils_Token
::formatTokensForDisplay($tokens));
2573 'text_message' => ts('HTML Format'),
2574 'sms_text_message' => ts('SMS Message'),
2576 $modePrefixes = ['Mail' => NULL, 'SMS' => 'SMS'];
2578 $className = CRM_Utils_System
::getClassName($form);
2580 if ($className != 'CRM_SMS_Form_Upload' && $className != 'CRM_Contact_Form_Task_SMS' &&
2581 $className != 'CRM_Contact_Form_Task_SMS'
2583 $form->add('wysiwyg', 'html_message',
2584 strstr($className, 'PDF') ?
ts('Document Body') : ts('HTML Format'),
2588 'onkeyup' => "return verify(this)",
2592 if ($className != 'CRM_Admin_Form_ScheduleReminders') {
2593 unset($modePrefixes['SMS']);
2597 unset($textFields['text_message']);
2598 unset($modePrefixes['Mail']);
2601 //insert message Text by selecting "Select Template option"
2602 foreach ($textFields as $id => $label) {
2604 if ($id == 'sms_text_message') {
2606 $form->assign('max_sms_length', CRM_SMS_Provider
::MAX_SMS_CHAR
);
2608 $form->add('textarea', $id, $label,
2612 'onkeyup' => "return verify(this, '{$prefix}')",
2617 foreach ($modePrefixes as $prefix) {
2618 if ($prefix == 'SMS') {
2619 $templates[$prefix] = CRM_Core_BAO_MessageTemplate
::getMessageTemplates(FALSE, TRUE);
2622 $templates[$prefix] = CRM_Core_BAO_MessageTemplate
::getMessageTemplates(FALSE);
2624 if (!empty($templates[$prefix])) {
2625 $form->assign('templates', TRUE);
2627 $form->add('select', "{$prefix}template", ts('Use Template'),
2628 ['' => ts('- select -')] +
$templates[$prefix], FALSE,
2629 ['onChange' => "selectValue( this.value, '{$prefix}');"]
2632 $form->add('checkbox', "{$prefix}updateTemplate", ts('Update Template'), NULL);
2634 $form->add('checkbox', "{$prefix}saveTemplate", ts('Save As New Template'), NULL, FALSE,
2635 ['onclick' => "showSaveDetails(this, '{$prefix}');"]
2637 $form->add('text', "{$prefix}saveTemplateName", ts('Template Title'));
2640 // I'm not sure this is ever called.
2641 $action = CRM_Utils_Request
::retrieve('action', 'String', $form, FALSE);
2642 if ((CRM_Utils_System
::getClassName($form) == 'CRM_Contact_Form_Task_PDF') &&
2643 $action == CRM_Core_Action
::VIEW
2645 $form->freeze('html_message');
2650 * Get the search based mailing Ids.
2653 * , searched base mailing ids.
2655 public function searchMailingIDs() {
2656 $group = CRM_Mailing_DAO_MailingGroup
::getTableName();
2657 $mailing = self
::getTableName();
2660 SELECT $mailing.id as mailing_id
2661 FROM $mailing, $group
2662 WHERE $group.mailing_id = $mailing.id
2663 AND $group.group_type = 'Base'";
2665 $searchDAO = CRM_Core_DAO
::executeQuery($query);
2667 while ($searchDAO->fetch()) {
2668 $mailingIDs[] = $searchDAO->mailing_id
;
2675 * Get the content/components of mailing based on mailing Id
2677 * @param array $report
2678 * of mailing report.
2681 * Reference of this.
2683 * @param bool $isSMS
2686 * array content/component.
2688 public static function getMailingContent(&$report, &$form, $isSMS = FALSE) {
2689 $htmlHeader = $textHeader = NULL;
2690 $htmlFooter = $textFooter = NULL;
2693 if ($report['mailing']['header_id']) {
2694 $header = new CRM_Mailing_BAO_MailingComponent();
2695 $header->id
= $report['mailing']['header_id'];
2696 $header->find(TRUE);
2697 $htmlHeader = $header->body_html
;
2698 $textHeader = $header->body_text
;
2701 if ($report['mailing']['footer_id']) {
2702 $footer = new CRM_Mailing_BAO_MailingComponent();
2703 $footer->id
= $report['mailing']['footer_id'];
2704 $footer->find(TRUE);
2705 $htmlFooter = $footer->body_html
;
2706 $textFooter = $footer->body_text
;
2710 $mailingKey = $form->_mailing_id
;
2712 if ($hash = CRM_Mailing_BAO_Mailing
::getMailingHash($mailingKey)) {
2713 $mailingKey = $hash;
2717 if (!empty($report['mailing']['body_text'])) {
2718 $url = CRM_Utils_System
::url('civicrm/mailing/view', 'reset=1&text=1&id=' . $mailingKey);
2719 $form->assign('textViewURL', $url);
2723 if (!empty($report['mailing']['body_html'])) {
2724 $url = CRM_Utils_System
::url('civicrm/mailing/view', 'reset=1&id=' . $mailingKey);
2725 $form->assign('htmlViewURL', $url);
2730 $report['mailing']['attachment'] = CRM_Core_BAO_File
::attachmentInfo('civicrm_mailing', $form->_mailing_id
);
2740 public static function overrideVerp($jobID) {
2741 static $_cache = [];
2743 if (!isset($_cache[$jobID])) {
2745 SELECT override_verp
2746 FROM civicrm_mailing
2747 INNER JOIN civicrm_mailing_job ON civicrm_mailing.id = civicrm_mailing_job.mailing_id
2748 WHERE civicrm_mailing_job.id = %1
2750 $params = [1 => [$jobID, 'Integer']];
2751 $_cache[$jobID] = CRM_Core_DAO
::singleValueQuery($query, $params);
2753 return $_cache[$jobID];
2762 public static function processQueue($mode = NULL) {
2763 $config = CRM_Core_Config
::singleton();
2765 if ($mode == NULL && CRM_Core_BAO_MailSettings
::defaultDomain() == "EXAMPLE.ORG") {
2766 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>', [
2767 1 => CRM_Utils_System
::url('civicrm/admin/mailSettings', 'reset=1'),
2768 2 => "https://docs.civicrm.org/sysadmin/en/latest/setup/civimail/",
2772 // check if we are enforcing number of parallel cron jobs
2774 $gotCronLock = FALSE;
2776 $mailerJobsMax = Civi
::settings()->get('mailerJobsMax');
2777 if (is_numeric($mailerJobsMax) && $mailerJobsMax > 0) {
2778 $lockArray = range(1, $mailerJobsMax);
2780 // Shuffle the array to improve chances of quickly finding an open thread
2781 shuffle($lockArray);
2783 // Check if we are using global locks
2784 foreach ($lockArray as $lockID) {
2785 $cronLock = Civi
::lockManager()
2786 ->acquire("worker.mailing.send.{$lockID}");
2787 if ($cronLock->isAcquired()) {
2788 $gotCronLock = TRUE;
2793 // Exit here since we have enough mailing processes running
2794 if (!$gotCronLock) {
2795 CRM_Core_Error
::debug_log_message('Returning early, since the maximum number of mailing processes are running');
2799 if (getenv('CIVICRM_CRON_HOLD')) {
2800 // In testing, we may need to simulate some slow activities.
2801 sleep(getenv('CIVICRM_CRON_HOLD'));
2805 // Split up the parent jobs into multiple child jobs
2806 $mailerJobSize = Civi
::settings()->get('mailerJobSize');
2807 CRM_Mailing_BAO_MailingJob
::runJobs_pre($mailerJobSize, $mode);
2808 CRM_Mailing_BAO_MailingJob
::runJobs(NULL, $mode);
2809 CRM_Mailing_BAO_MailingJob
::runJobs_post($mode);
2811 // Release the global lock if we do have one
2813 $cronLock->release();
2820 * @param int $mailingID
2822 private static function addMultipleEmails($mailingID) {
2824 INSERT INTO civicrm_mailing_recipients
2825 (mailing_id, email_id, contact_id)
2826 SELECT %1, e.id, e.contact_id FROM civicrm_email e
2828 AND e.is_bulkmail = 1
2830 ( SELECT contact_id FROM civicrm_mailing_recipients mr WHERE mailing_id = %1 )
2831 AND e.id NOT IN ( SELECT email_id FROM civicrm_mailing_recipients mr WHERE mailing_id = %1 )
2833 $params = [1 => [$mailingID, 'Integer']];
2835 $dao = CRM_Core_DAO
::executeQuery($sql, $params);
2839 * @param bool $isSMS
2843 public static function getMailingsList($isSMS = FALSE) {
2847 $where .= " civicrm_mailing.sms_provider_id IS NULL ";
2850 $where .= " civicrm_mailing.sms_provider_id IS NOT NULL ";
2855 SELECT civicrm_mailing.id, civicrm_mailing.name, civicrm_mailing_job.end_date
2856 FROM civicrm_mailing
2857 INNER JOIN civicrm_mailing_job ON civicrm_mailing.id = civicrm_mailing_job.mailing_id {$where}
2858 ORDER BY civicrm_mailing.name";
2859 $mailing = CRM_Core_DAO
::executeQuery($query);
2861 while ($mailing->fetch()) {
2862 $list[$mailing->id
] = "{$mailing->name} :: {$mailing->end_date}";
2870 * wrapper for ajax activity selector.
2872 * @param array $params
2873 * Associated array for params record id.
2876 * associated array of contact activities
2878 public static function getContactMailingSelector(&$params) {
2879 // format the params
2880 $params['offset'] = ($params['page'] - 1) * $params['rp'];
2881 $params['rowCount'] = $params['rp'];
2882 $params['sort'] = CRM_Utils_Array
::value('sortBy', $params);
2883 $params['caseId'] = NULL;
2885 // get contact mailings
2886 $mailings = CRM_Mailing_BAO_Mailing
::getContactMailings($params);
2889 $params['total'] = CRM_Mailing_BAO_Mailing
::getContactMailingsCount($params);
2892 if (!empty($mailings)) {
2893 $openCounts = CRM_Mailing_Event_BAO_Opened
::getMailingContactCount(array_keys($mailings), $params['contact_id']);
2894 $clickCounts = CRM_Mailing_Event_BAO_TrackableURLOpen
::getMailingContactCount(array_keys($mailings), $params['contact_id']);
2897 // format params and add links
2898 $contactMailings = [];
2899 foreach ($mailings as $mailingId => $values) {
2901 $mailing['subject'] = $values['subject'];
2902 $mailing['creator_name'] = CRM_Utils_System
::href(
2903 $values['creator_name'],
2904 'civicrm/contact/view',
2905 "reset=1&cid={$values['creator_id']}");
2906 $mailing['recipients'] = CRM_Utils_System
::href(ts('(recipients)'), 'civicrm/mailing/report/event',
2907 "mid={$values['mailing_id']}&reset=1&cid={$params['contact_id']}&event=queue&context=mailing");
2908 $mailing['start_date'] = CRM_Utils_Date
::customFormat($values['start_date']);
2910 $mailing['openstats'] = "Opens: " .
2911 CRM_Utils_Array
::value($values['mailing_id'], $openCounts, 0) .
2913 CRM_Utils_Array
::value($values['mailing_id'], $clickCounts, 0);
2916 CRM_Core_Action
::VIEW
=> [
2917 'name' => ts('View'),
2918 'url' => 'civicrm/mailing/view',
2919 'qs' => "reset=1&id=%%mkey%%",
2920 'title' => ts('View Mailing'),
2921 'class' => 'crm-popup',
2923 CRM_Core_Action
::BROWSE
=> [
2924 'name' => ts('Mailing Report'),
2925 'url' => 'civicrm/mailing/report',
2926 'qs' => "mid=%%mid%%&reset=1&cid=%%cid%%&context=mailing",
2927 'title' => ts('View Mailing Report'),
2931 $mailingKey = $values['mailing_id'];
2932 if ($hash = CRM_Mailing_BAO_Mailing
::getMailingHash($mailingKey)) {
2933 $mailingKey = $hash;
2936 $mailing['links'] = CRM_Core_Action
::formLink(
2940 'mid' => $values['mailing_id'],
2941 'cid' => $params['contact_id'],
2942 'mkey' => $mailingKey,
2946 'mailing.contact.action',
2948 $values['mailing_id']
2951 array_push($contactMailings, $mailing);
2954 $contactMailingsDT = [];
2955 $contactMailingsDT['data'] = $contactMailings;
2956 $contactMailingsDT['recordsTotal'] = $params['total'];
2957 $contactMailingsDT['recordsFiltered'] = $params['total'];
2959 return $contactMailingsDT;
2963 * Retrieve contact mailing.
2965 * @param array $params
2968 * Array of mailings for a contact
2971 public static function getContactMailings(&$params) {
2972 $params['version'] = 3;
2973 $params['offset'] = ($params['page'] - 1) * $params['rp'];
2974 $params['limit'] = $params['rp'];
2975 $params['sort'] = CRM_Utils_Array
::value('sortBy', $params);
2977 $result = civicrm_api('MailingContact', 'get', $params);
2978 return $result['values'];
2982 * Retrieve contact mailing count.
2984 * @param array $params
2987 * count of mailings for a contact
2990 public static function getContactMailingsCount(&$params) {
2991 $params['version'] = 3;
2992 return civicrm_api('MailingContact', 'getcount', $params);
2996 * Get a list of permissions required for CRUD'ing each field
2997 * (when workflow is enabled).
3000 * Array (string $fieldName => string $permName)
3002 public static function getWorkflowFieldPerms() {
3003 $fieldNames = array_keys(CRM_Mailing_DAO_Mailing
::fields());
3005 foreach ($fieldNames as $fieldName) {
3006 if ($fieldName == 'id') {
3007 $fieldPerms[$fieldName] = [
3011 'schedule mailings',
3016 elseif (in_array($fieldName, ['scheduled_date', 'scheduled_id'])) {
3017 $fieldPerms[$fieldName] = [
3019 ['access CiviMail', 'schedule mailings'],
3022 elseif (in_array($fieldName, [
3025 'approval_status_id',
3028 $fieldPerms[$fieldName] = [
3030 ['access CiviMail', 'approve mailings'],
3034 $fieldPerms[$fieldName] = [
3036 ['access CiviMail', 'create mailings'],
3044 * White-list of possible values for the entity_table field.
3048 public static function mailingGroupEntityTables() {
3050 CRM_Contact_BAO_Group
::getTableName() => 'Group',
3051 CRM_Mailing_BAO_Mailing
::getTableName() => 'Mailing',
3056 * Get the public view url.
3059 * @param bool $absolute
3063 public static function getPublicViewUrl($id, $absolute = TRUE) {
3064 if ((civicrm_api3('Mailing', 'getvalue', [
3066 'return' => 'visibility',
3067 ])) === 'Public Pages') {
3069 // if hash setting is on then we change the public url into a hash
3070 $hash = CRM_Mailing_BAO_Mailing
::getMailingHash($id);
3071 if (!empty($hash)) {
3075 return CRM_Utils_System
::url('civicrm/mailing/view', ['id' => $id], $absolute, NULL, TRUE, TRUE);
3080 * Get a list of template types which can be used as `civicrm_mailing.template_type`.
3083 * A list of template-types, keyed numerically. Each defines:
3084 * - name: string, a short symbolic name
3085 * - editorUrl: string, Angular template name
3087 * Ex: $templateTypes[0] === array('name' => 'mosaico', 'editorUrl' => '~/crmMosaico/editor.html').
3089 public static function getTemplateTypes() {
3090 if (!isset(Civi
::$statics[__CLASS__
]['templateTypes'])) {
3093 'name' => 'traditional',
3094 'editorUrl' => CRM_Mailing_Info
::workflowEnabled() ?
'~/crmMailing/EditMailingCtrl/workflow.html' : '~/crmMailing/EditMailingCtrl/2step.html',
3098 CRM_Utils_Hook
::mailingTemplateTypes($types);
3100 $defaults = ['weight' => 0];
3101 foreach (array_keys($types) as $typeName) {
3102 $types[$typeName] = array_merge($defaults, $types[$typeName]);
3104 usort($types, function ($a, $b) {
3105 if ($a['weight'] === $b['weight']) {
3108 return $a['weight'] < $b['weight'] ?
-1 : 1;
3111 Civi
::$statics[__CLASS__
]['templateTypes'] = $types;
3114 return Civi
::$statics[__CLASS__
]['templateTypes'];
3118 * Get a list of template types.
3121 * Array(string $name => string $label).
3123 public static function getTemplateTypeNames() {
3125 foreach (self
::getTemplateTypes() as $type) {
3126 $r[$type['name']] = $type['name'];