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 +--------------------------------------------------------------------+
14 * APIv3 functions for registering/processing mailing events.
16 * @package CiviCRM_APIv3
20 * Handle a create event.
22 * @param array $params
26 * @throws \API_Exception
27 * @throws \Civi\API\Exception\UnauthorizedException
29 function civicrm_api3_mailing_create($params) {
30 $safeParams = $params;
31 $timestampCheck = TRUE;
32 if (!empty($params['id']) && !empty($params['modified_date'])) {
33 $timestampCheck = _civicrm_api3_compare_timestamps($safeParams['modified_date'], $safeParams['id'], 'Mailing');
34 unset($safeParams['modified_date']);
36 if (!$timestampCheck) {
37 throw new API_Exception("Mailing has not been saved, Content maybe out of date, please refresh the page and try again");
39 // If we're going to autosend, then check validity before saving.
40 if (empty($params['is_completed']) && !empty($params['scheduled_date'])
41 && $params['scheduled_date'] !== 'null'
42 // This might have been passed in as empty to prevent us validating, is set skip.
43 && !isset($params['_evil_bao_validator_'])) {
45 // FlexMailer is a refactoring of CiviMail which provides new hooks/APIs/docs. If the sysadmin has opted to enable it, then use that instead of CiviMail.
46 $function = \CRM_Utils_Constant
::value('CIVICRM_FLEXMAILER_HACK_SENDABLE', 'CRM_Mailing_BAO_Mailing::checkSendable');
47 $validationFunction = Civi\Core\Resolver
::singleton()->get($function);
48 $errors = call_user_func($validationFunction, $params);
49 if (!empty($errors)) {
50 $fields = implode(',', array_keys($errors));
51 throw new CiviCRM_API3_Exception("Mailing cannot be sent. There are missing or invalid fields ($fields).", 'cannot-send', $errors);
55 $result = _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__
), $safeParams, 'Mailing');
56 return _civicrm_api3_mailing_get_formatResult($result);
60 * Get tokens for one or more entity type
62 * Output will be formatted either as a flat list,
63 * or pass sequential=1 to retrieve as a hierarchy formatted for select2.
65 * @param array $params
66 * Should contain an array of entities to retrieve tokens for.
69 * @throws \API_Exception
71 function civicrm_api3_mailing_gettokens($params) {
73 foreach ((array) $params['entity'] as $ent) {
74 $func = lcfirst($ent) . 'Tokens';
75 if (!method_exists('CRM_Core_SelectValues', $func)) {
76 throw new API_Exception('Unknown token entity: ' . $ent);
78 $tokens = array_merge(CRM_Core_SelectValues
::$func(), $tokens);
80 if (!empty($params['sequential'])) {
81 $tokens = CRM_Utils_Token
::formatTokensForDisplay($tokens);
83 return civicrm_api3_create_success($tokens, $params, 'Mailing', 'gettokens');
87 * Adjust Metadata for Create action.
89 * The metadata is used for setting defaults, documentation & validation.
91 * @param array $params
92 * Array of parameters determined by getfields.
94 function _civicrm_api3_mailing_gettokens_spec(&$params) {
96 'api.default' => ['contact'],
102 // Fetch a list of token functions and format to look like entity names
103 foreach (get_class_methods('CRM_Core_SelectValues') as $func) {
104 if (strpos($func, 'Tokens')) {
105 $ent = ucfirst(str_replace('Tokens', '', $func));
106 $params['entity']['options'][$ent] = $ent;
112 * Adjust Metadata for Create action.
114 * The metadata is used for setting defaults, documentation & validation.
116 * @param array $params
117 * Array of parameters determined by getfields.
119 function _civicrm_api3_mailing_create_spec(&$params) {
120 $params['created_id']['api.default'] = 'user_contact_id';
122 $params['override_verp']['api.default'] = !CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::MAILING_PREFERENCES_NAME
, 'track_civimail_replies');
123 $params['visibility']['api.default'] = 'Public Pages';
124 $params['dedupe_email']['api.default'] = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::MAILING_PREFERENCES_NAME
, 'dedupe_email_default');
126 $params['forward_replies']['api.default'] = FALSE;
127 $params['auto_responder']['api.default'] = FALSE;
128 $params['open_tracking']['api.default'] = Civi
::settings()->get('open_tracking_default');
129 $params['url_tracking']['api.default'] = Civi
::settings()->get('url_tracking_default');
131 $params['header_id']['api.default'] = CRM_Mailing_PseudoConstant
::defaultComponent('Header', '');
132 $params['footer_id']['api.default'] = CRM_Mailing_PseudoConstant
::defaultComponent('Footer', '');
133 $params['optout_id']['api.default'] = CRM_Mailing_PseudoConstant
::defaultComponent('OptOut', '');
134 $params['reply_id']['api.default'] = CRM_Mailing_PseudoConstant
::defaultComponent('Reply', '');
135 $params['resubscribe_id']['api.default'] = CRM_Mailing_PseudoConstant
::defaultComponent('Resubscribe', '');
136 $params['unsubscribe_id']['api.default'] = CRM_Mailing_PseudoConstant
::defaultComponent('Unsubscribe', '');
137 $params['mailing_type']['api.default'] = 'standalone';
138 $defaultAddress = CRM_Core_BAO_Domain
::getNameAndEmail(TRUE, TRUE);
139 foreach ($defaultAddress as $value) {
140 if (preg_match('/"(.*)" <(.*)>/', $value, $match)) {
141 $params['from_email']['api.default'] = $match[2];
142 $params['from_name']['api.default'] = $match[1];
148 * Adjust metadata for clone spec action.
152 function _civicrm_api3_mailing_clone_spec(&$spec) {
153 $mailingFields = CRM_Mailing_DAO_Mailing
::fields();
154 $spec['id'] = $mailingFields['id'];
155 $spec['id']['api.required'] = 1;
161 * @param array $params
164 * @throws \CiviCRM_API3_Exception
166 function civicrm_api3_mailing_clone($params) {
176 'approval_status_id',
183 $get = civicrm_api3('Mailing', 'getsingle', ['id' => $params['id']]);
186 $newParams['debug'] = $params['debug'] ??
NULL;
187 $newParams['groups']['include'] = [];
188 $newParams['groups']['exclude'] = [];
189 $newParams['mailings']['include'] = [];
190 $newParams['mailings']['exclude'] = [];
191 foreach ($get as $field => $value) {
192 if (!in_array($field, $BLACKLIST)) {
193 $newParams[$field] = $value;
197 $dao = new CRM_Mailing_DAO_MailingGroup();
198 $dao->mailing_id
= $params['id'];
200 while ($dao->fetch()) {
201 // CRM-11431; account for multi-lingual
202 $entity = (substr($dao->entity_table
, 0, 15) == 'civicrm_mailing') ?
'mailings' : 'groups';
203 $newParams[$entity][strtolower($dao->group_type
)][] = $dao->entity_id
;
206 return civicrm_api3('Mailing', 'create', $newParams);
210 * Handle a delete event.
212 * @param array $params
217 function civicrm_api3_mailing_delete($params) {
218 return _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__
), $params);
222 * Handle a get event.
224 * @param array $params
228 function civicrm_api3_mailing_get($params) {
229 $result = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__
), $params, TRUE, 'Mailing');
230 return _civicrm_api3_mailing_get_formatResult($result);
236 * @param array $result
239 * @throws \CRM_Core_Exception
241 function _civicrm_api3_mailing_get_formatResult($result) {
242 if (isset($result['values']) && is_array($result['values'])) {
243 foreach ($result['values'] as $key => $caseType) {
244 if (isset($result['values'][$key]['template_options']) && is_string($result['values'][$key]['template_options'])) {
245 $result['values'][$key]['template_options'] = json_decode($result['values'][$key]['template_options'], TRUE);
253 * Adjust metadata for mailing submit api function.
257 function _civicrm_api3_mailing_submit_spec(&$spec) {
258 $mailingFields = CRM_Mailing_DAO_Mailing
::fields();
259 $spec['id'] = $mailingFields['id'];
260 $spec['scheduled_date'] = $mailingFields['scheduled_date'];
261 $spec['approval_date'] = $mailingFields['approval_date'];
262 $spec['approval_status_id'] = $mailingFields['approval_status_id'];
263 $spec['approval_note'] = $mailingFields['approval_note'];
264 // _skip_evil_bao_auto_recipients_: bool
270 * @param array $params
273 * @throws API_Exception
275 function civicrm_api3_mailing_submit($params) {
276 civicrm_api3_verify_mandatory($params, 'CRM_Mailing_DAO_Mailing', ['id']);
278 if (!isset($params['scheduled_date']) && !isset($updateParams['approval_date'])) {
279 throw new API_Exception("Missing parameter scheduled_date and/or approval_date");
281 if (!is_numeric(CRM_Core_Session
::getLoggedInContactID())) {
282 throw new API_Exception("Failed to determine current user");
286 $updateParams['id'] = $params['id'];
288 // Note: we'll pass along scheduling/approval fields, but they may get ignored
289 // if we don't have permission.
290 if (isset($params['scheduled_date'])) {
291 $updateParams['scheduled_date'] = $params['scheduled_date'];
292 $updateParams['scheduled_id'] = CRM_Core_Session
::getLoggedInContactID();
294 if (isset($params['approval_date'])) {
295 $updateParams['approval_date'] = $params['approval_date'];
296 $updateParams['approver_id'] = CRM_Core_Session
::getLoggedInContactID();
297 $updateParams['approval_status_id'] = CRM_Utils_Array
::value('approval_status_id', $updateParams, CRM_Core_OptionGroup
::getDefaultValue('mail_approval_status'));
299 if (isset($params['approval_note'])) {
300 $updateParams['approval_note'] = $params['approval_note'];
302 if (isset($params['_skip_evil_bao_auto_recipients_'])) {
303 $updateParams['_skip_evil_bao_auto_recipients_'] = $params['_skip_evil_bao_auto_recipients_'];
306 $updateParams['options']['reload'] = 1;
307 return civicrm_api3('Mailing', 'create', $updateParams);
311 * Process a bounce event by passing through to the BAOs.
313 * @param array $params
315 * @throws API_Exception
318 function civicrm_api3_mailing_event_bounce($params) {
319 $body = $params['body'];
320 unset($params['body']);
322 $params +
= CRM_Mailing_BAO_BouncePattern
::match($body);
324 if (CRM_Mailing_Event_BAO_Bounce
::create($params)) {
325 return civicrm_api3_create_success($params);
328 throw new API_Exception(ts('Queue event could not be found'), 'no_queue_event');
333 * Adjust Metadata for bounce_spec action.
335 * The metadata is used for setting defaults, documentation & validation.
337 * @param array $params
338 * Array of parameters determined by getfields.
340 function _civicrm_api3_mailing_event_bounce_spec(&$params) {
341 $params['job_id']['api.required'] = 1;
342 $params['job_id']['title'] = 'Job ID';
343 $params['event_queue_id']['api.required'] = 1;
344 $params['event_queue_id']['title'] = 'Event Queue ID';
345 $params['hash']['api.required'] = 1;
346 $params['hash']['title'] = 'Hash';
347 $params['body']['api.required'] = 1;
348 $params['body']['title'] = 'Body';
352 * Handle a confirm event.
356 * @param array $params
360 function civicrm_api3_mailing_event_confirm($params) {
361 return civicrm_api('MailingEventConfirm', 'create', $params);
365 * Declare deprecated functions.
367 * @deprecated api notice
369 * Array of deprecated actions
371 function _civicrm_api3_mailing_deprecation() {
372 return ['event_confirm' => 'Mailing api "event_confirm" action is deprecated. Use the mailing_event_confirm api instead.'];
376 * Handle a reply event.
378 * @param array $params
382 function civicrm_api3_mailing_event_reply($params) {
383 $job = $params['job_id'];
384 $queue = $params['event_queue_id'];
385 $hash = $params['hash'];
386 $replyto = $params['replyTo'];
387 $bodyTxt = $params['bodyTxt'] ??
NULL;
388 $bodyHTML = $params['bodyHTML'] ??
NULL;
389 $fullEmail = $params['fullEmail'] ??
NULL;
391 $mailing = CRM_Mailing_Event_BAO_Reply
::reply($job, $queue, $hash, $replyto);
393 if (empty($mailing)) {
394 return civicrm_api3_create_error('Queue event could not be found');
397 CRM_Mailing_Event_BAO_Reply
::send($queue, $mailing, $bodyTxt, $replyto, $bodyHTML, $fullEmail);
399 return civicrm_api3_create_success($params);
403 * Adjust Metadata for event_reply action.
405 * The metadata is used for setting defaults, documentation & validation.
407 * @param array $params
408 * Array of parameters determined by getfields.
410 function _civicrm_api3_mailing_event_reply_spec(&$params) {
411 $params['job_id']['api.required'] = 1;
412 $params['job_id']['title'] = 'Job ID';
413 $params['event_queue_id']['api.required'] = 1;
414 $params['event_queue_id']['title'] = 'Event Queue ID';
415 $params['hash']['api.required'] = 1;
416 $params['hash']['title'] = 'Hash';
417 $params['replyTo']['api.required'] = 0;
418 //doesn't really explain adequately
419 $params['replyTo']['title'] = 'Reply To';
423 * Handle a forward event.
425 * @param array $params
429 function civicrm_api3_mailing_event_forward($params) {
430 $job = $params['job_id'];
431 $queue = $params['event_queue_id'];
432 $hash = $params['hash'];
433 $email = $params['email'];
434 $fromEmail = $params['fromEmail'] ??
NULL;
435 $params = $params['params'] ??
NULL;
437 $forward = CRM_Mailing_Event_BAO_Forward
::forward($job, $queue, $hash, $email, $fromEmail, $params);
440 return civicrm_api3_create_success($params);
443 return civicrm_api3_create_error('Queue event could not be found');
447 * Adjust Metadata for event_forward action.
449 * The metadata is used for setting defaults, documentation & validation.
451 * @param array $params
452 * Array of parameters determined by getfields.
454 function _civicrm_api3_mailing_event_forward_spec(&$params) {
455 $params['job_id']['api.required'] = 1;
456 $params['job_id']['title'] = 'Job ID';
457 $params['event_queue_id']['api.required'] = 1;
458 $params['event_queue_id']['title'] = 'Event Queue ID';
459 $params['hash']['api.required'] = 1;
460 $params['hash']['title'] = 'Hash';
461 $params['email']['api.required'] = 1;
462 $params['email']['title'] = 'Forwarded to Email';
466 * Handle a click event.
468 * @param array $params
472 function civicrm_api3_mailing_event_click($params) {
473 civicrm_api3_verify_mandatory($params,
474 'CRM_Mailing_Event_DAO_TrackableURLOpen',
475 ['event_queue_id', 'url_id'],
479 $url_id = $params['url_id'];
480 $queue = $params['event_queue_id'];
482 $url = CRM_Mailing_Event_BAO_TrackableURLOpen
::track($queue, $url_id);
485 $values['url'] = $url;
486 $values['is_error'] = 0;
488 return civicrm_api3_create_success($values);
492 * Handle an open event.
494 * @param array $params
498 function civicrm_api3_mailing_event_open($params) {
500 civicrm_api3_verify_mandatory($params,
501 'CRM_Mailing_Event_DAO_Opened',
506 $queue = $params['event_queue_id'];
507 $success = CRM_Mailing_Event_BAO_Opened
::open($queue);
510 return civicrm_api3_create_error('mailing open event failed');
513 return civicrm_api3_create_success($params);
519 * @param array $params
520 * Array per getfields metadata.
523 * @throws \API_Exception
525 function civicrm_api3_mailing_preview($params) {
527 if (!empty($params['from_email'])) {
528 $fromEmail = $params['from_email'];
531 $mailing = new CRM_Mailing_BAO_Mailing();
532 $mailingID = $params['id'] ??
NULL;
534 $mailing->id
= $mailingID;
535 $mailing->find(TRUE);
538 $mailing->copyValues($params);
541 $session = CRM_Core_Session
::singleton();
543 CRM_Mailing_BAO_Mailing
::tokenReplace($mailing);
545 // get and format attachments
546 $attachments = CRM_Core_BAO_File
::getEntityFile('civicrm_mailing', $mailing->id
);
548 $returnProperties = $mailing->getReturnProperties();
549 $contactID = $params['contact_id'] ??
NULL;
551 // If we still don't have a userID in a session because we are annon then set contactID to be 0
552 $contactID = empty($session->get('userID')) ?
0 : $session->get('userID');
554 $mailingParams = ['contact_id' => $contactID];
557 $details = CRM_Utils_Token
::getAnonymousTokenDetails($mailingParams, $returnProperties, TRUE, TRUE, NULL, $mailing->getFlattenedTokens());
558 $details = $details[0][0] ??
NULL;
561 [$details] = CRM_Utils_Token
::getTokenDetails($mailingParams, $returnProperties, TRUE, TRUE, NULL, $mailing->getFlattenedTokens());
562 $details = $details[$contactID];
565 $mime = $mailing->compose(NULL, NULL, NULL, $contactID, $fromEmail, $fromEmail,
566 TRUE, $details, $attachments
569 return civicrm_api3_create_success([
571 'contact_id' => $contactID,
572 'subject' => CRM_Utils_Array
::value('Subject', $mime->headers(), ''),
573 'body_html' => $mime->getHTMLBody(),
574 'body_text' => $mime->getTXTBody(),
579 * Adjust metadata for send test function.
583 function _civicrm_api3_mailing_send_test_spec(&$spec) {
584 $spec['test_group']['title'] = 'Test Group ID';
585 $spec['test_email']['title'] = 'Test Email Address';
586 $spec['mailing_id']['api.required'] = TRUE;
587 $spec['mailing_id']['title'] = ts('Mailing Id');
593 * @param array $params
596 * @throws \API_Exception
597 * @throws \CiviCRM_API3_Exception
599 function civicrm_api3_mailing_send_test($params) {
600 if (!array_key_exists('test_group', $params) && !array_key_exists('test_email', $params)) {
601 throw new API_Exception("Mandatory key(s) missing from params array: test_group and/or test_email field are required");
603 civicrm_api3_verify_mandatory($params,
604 'CRM_Mailing_DAO_MailingJob',
609 $testEmailParams = _civicrm_api3_generic_replace_base_params($params);
610 if (isset($testEmailParams['id'])) {
611 unset($testEmailParams['id']);
614 $testEmailParams['is_test'] = 1;
615 $testEmailParams['status'] = 'Scheduled';
616 $testEmailParams['scheduled_date'] = CRM_Utils_Date
::processDate(date('Y-m-d'), date('H:i:s'));
617 $testEmailParams['is_calling_function_updated_to_reflect_deprecation'] = TRUE;
618 $job = civicrm_api3('MailingJob', 'create', $testEmailParams);
619 CRM_Mailing_BAO_Mailing
::getRecipients($testEmailParams['mailing_id']);
620 $testEmailParams['job_id'] = $job['id'];
621 $testEmailParams['emails'] = array_key_exists('test_email', $testEmailParams) ?
explode(',', strtolower($testEmailParams['test_email'])) : NULL;
622 if (!empty($params['test_email'])) {
623 $query = CRM_Utils_SQL_Select
::from('civicrm_email e')
624 ->select(['e.id', 'e.contact_id', 'e.email'])
625 ->join('c', 'INNER JOIN civicrm_contact c ON e.contact_id = c.id')
626 ->where('e.email IN (@emails)', ['@emails' => $testEmailParams['emails']])
627 ->where('e.on_hold = 0')
628 ->where('c.is_opt_out = 0')
629 ->where('c.do_not_email = 0')
630 ->where('c.is_deceased = 0')
631 ->where('c.is_deleted = 0')
633 ->orderBy(['e.is_bulkmail DESC', 'e.is_primary DESC'])
635 $dao = CRM_Core_DAO
::executeQuery($query);
637 // fetch contact_id and email id for all existing emails
638 while ($dao->fetch()) {
639 $emailDetail[strtolower($dao->email
)] = [
640 'contact_id' => $dao->contact_id
,
641 'email_id' => $dao->id
,
644 foreach ($testEmailParams['emails'] as $key => $email) {
645 $email = trim($email);
646 $contactId = $emailId = NULL;
647 if (array_key_exists($email, $emailDetail)) {
648 $emailId = $emailDetail[$email]['email_id'];
649 $contactId = $emailDetail[$email]['contact_id'];
651 if (!$contactId && CRM_Core_Permission
::check('add contacts')) {
652 //create new contact.
653 $contact = civicrm_api3('Contact', 'create',
655 'contact_type' => 'Individual',
657 'api.Email.get' => ['return' => 'id'],
660 $contactId = $contact['id'];
661 $emailId = $contact['values'][$contactId]['api.Email.get']['id'];
663 if ($emailId && $contactId) {
664 civicrm_api3('MailingEventQueue', 'create',
666 'job_id' => $job['id'],
667 'email_id' => $emailId,
668 'contact_id' => $contactId,
677 while (!$isComplete) {
678 // Q: In CRM_Mailing_BAO_Mailing::processQueue(), the three runJobs*()
679 // functions are all called. Why does Mailing.send_test only call one?
680 // CRM_Mailing_BAO_MailingJob::runJobs_pre($mailerJobSize, NULL);
681 $isComplete = CRM_Mailing_BAO_MailingJob
::runJobs($testEmailParams);
682 // CRM_Mailing_BAO_MailingJob::runJobs_post(NULL);
685 //return delivered mail info
686 $mailDelivered = CRM_Mailing_Event_BAO_Delivered
::getRows($params['mailing_id'], $job['id'], TRUE, NULL, NULL, NULL, TRUE);
688 return civicrm_api3_create_success($mailDelivered);
692 * Adjust Metadata for send_mail action.
694 * The metadata is used for setting defaults, documentation & validation.
696 * @param array $params
697 * Array of parameters determined by getfields.
699 function _civicrm_api3_mailing_stats_spec(&$params) {
700 $params['date']['api.default'] = 'now';
701 $params['date']['title'] = 'Date';
702 $params['is_distinct']['api.default'] = FALSE;
703 $params['is_distinct']['title'] = 'Is Distinct';
707 * Function which needs to be explained.
709 * @param array $params
712 * @throws \API_Exception
714 function civicrm_api3_mailing_stats($params) {
715 civicrm_api3_verify_mandatory($params,
716 'CRM_Mailing_DAO_MailingJob',
721 if ($params['date'] == 'now') {
722 $params['date'] = date('YmdHis');
725 $params['date'] = CRM_Utils_Date
::processDate($params['date'] . ' ' . $params['date_time']);
728 $stats[$params['mailing_id']] = [];
729 if (empty($params['job_id'])) {
730 $params['job_id'] = NULL;
732 foreach (['Recipients', 'Delivered', 'Bounces', 'Unsubscribers', 'Unique Clicks', 'Opened'] as $detail) {
735 $stats[$params['mailing_id']] +
= [
736 $detail => CRM_Mailing_Event_BAO_Queue
::getTotalCount($params['mailing_id'], $params['job_id']),
741 $stats[$params['mailing_id']] +
= [
742 $detail => CRM_Mailing_Event_BAO_Delivered
::getTotalCount($params['mailing_id'], $params['job_id'], $params['date']),
747 $stats[$params['mailing_id']] +
= [
748 $detail => CRM_Mailing_Event_BAO_Bounce
::getTotalCount($params['mailing_id'], $params['job_id'], $params['date']),
752 case 'Unsubscribers':
753 $stats[$params['mailing_id']] +
= [
754 $detail => CRM_Mailing_Event_BAO_Unsubscribe
::getTotalCount($params['mailing_id'], $params['job_id'], (bool) $params['is_distinct'], NULL, $params['date']),
758 case 'Unique Clicks':
759 $stats[$params['mailing_id']] +
= [
760 $detail => CRM_Mailing_Event_BAO_TrackableURLOpen
::getTotalCount($params['mailing_id'], $params['job_id'], (bool) $params['is_distinct'], NULL, $params['date']),
765 $stats[$params['mailing_id']] +
= [
766 $detail => CRM_Mailing_Event_BAO_Opened
::getTotalCount($params['mailing_id'], $params['job_id'], (bool) $params['is_distinct'], $params['date']),
771 $stats[$params['mailing_id']]['delivered_rate'] = $stats[$params['mailing_id']]['opened_rate'] = $stats[$params['mailing_id']]['clickthrough_rate'] = '0.00%';
772 if (!empty(CRM_Mailing_Event_BAO_Queue
::getTotalCount($params['mailing_id'], $params['job_id']))) {
773 $stats[$params['mailing_id']]['delivered_rate'] = round((100.0 * $stats[$params['mailing_id']]['Delivered']) / CRM_Mailing_Event_BAO_Queue
::getTotalCount($params['mailing_id'], $params['job_id']), 2) . '%';
775 if (!empty($stats[$params['mailing_id']]['Delivered'])) {
776 $stats[$params['mailing_id']]['opened_rate'] = round($stats[$params['mailing_id']]['Opened'] / $stats[$params['mailing_id']]['Delivered'] * 100.0, 2) . '%';
777 $stats[$params['mailing_id']]['clickthrough_rate'] = round($stats[$params['mailing_id']]['Unique Clicks'] / $stats[$params['mailing_id']]['Delivered'] * 100.0, 2) . '%';
779 return civicrm_api3_create_success($stats);
782 function _civicrm_api3_mailing_update_email_resetdate_spec(&$spec) {
783 $spec['minDays']['title'] = 'Number of days to wait without a bounce to assume successful delivery (default 3)';
784 $spec['minDays']['type'] = CRM_Utils_Type
::T_INT
;
785 $spec['minDays']['api.default'] = 3;
786 $spec['minDays']['api.required'] = 1;
788 $spec['maxDays']['title'] = 'Analyze mailings since this many days ago (default 7)';
789 $spec['maxDays']['type'] = CRM_Utils_Type
::T_INT
;
790 $spec['maxDays']['api.default'] = 7;
791 $spec['maxDays']['api.required'] = 1;
795 * Fix the reset dates on the email record based on when a mail was last delivered.
797 * We only consider mailings that were completed and finished in the last 3 to 7 days
798 * Both the min and max days can be set via the params
800 * @param array $params
804 function civicrm_api3_mailing_update_email_resetdate($params) {
805 CRM_Mailing_Event_BAO_Delivered
::updateEmailResetDate((int) $params['minDays'], (int) $params['maxDays']);
806 return civicrm_api3_create_success();