3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 class CRM_Mailing_Event_BAO_Bounce
extends CRM_Mailing_Event_DAO_Bounce
{
20 * Create a new bounce event, update the email address if necessary
22 * @param array $params
26 public static function create(&$params) {
27 $q = CRM_Mailing_Event_BAO_Queue
::verify($params['job_id'],
28 $params['event_queue_id'],
37 $transaction = new CRM_Core_Transaction();
38 $bounce = new CRM_Mailing_Event_BAO_Bounce();
39 $bounce->time_stamp
= date('YmdHis');
41 $action = empty($params['id']) ?
'create' : 'edit';
42 CRM_Utils_Hook
::pre($action, 'MailingEventBounce', CRM_Utils_Array
::value('id', $params), $params);
44 // if we dont have a valid bounce type, we should set it
45 // to bounce_type_id 11 which is Syntax error. this allows such email
46 // addresses to be bounce a few more time before being put on hold
48 // we changed this behavior since this bounce type might be due to some issue
49 // with the connection or smtp server etc
50 if (empty($params['bounce_type_id'])) {
51 $params['bounce_type_id'] = 11;
52 if (empty($params['bounce_reason'])) {
53 $params['bounce_reason'] = ts('Unknown bounce type: Could not parse bounce email');
57 // replace any invalid unicode characters with replacement characters
58 $params['bounce_reason'] = mb_convert_encoding($params['bounce_reason'], 'UTF-8', 'UTF-8');
60 // dev/mail#37 Replace 4-byte utf8 characaters with the unicode replacement character
61 // while CiviCRM does not support utf8mb4 for MySQL
62 $params['bounce_reason'] = preg_replace('/[\x{10000}-\x{10FFFF}]/u', "\xEF\xBF\xBD", $params['bounce_reason']);
65 $params['bounce_reason'] = mb_strcut($params['bounce_reason'], 0, 254);
67 $bounce->copyValues($params);
70 CRM_Utils_Hook
::post($action, 'MailingEventBounce', $bounce->id
, $bounce);
73 self
::putEmailOnHold($q->email_id
);
75 $transaction->commit();
81 * Get row count for the event selector.
83 * @param int $mailing_id
86 * Optional ID of a job to filter on.
88 * @param string|null $toDate
91 * Number of rows in result set
93 public static function getTotalCount($mailing_id, $job_id = NULL, $toDate = NULL) {
94 $dao = new CRM_Core_DAO();
96 $bounce = self
::getTableName();
97 $queue = CRM_Mailing_Event_BAO_Queue
::getTableName();
98 $mailing = CRM_Mailing_BAO_Mailing
::getTableName();
99 $job = CRM_Mailing_BAO_MailingJob
::getTableName();
102 SELECT COUNT($bounce.id) as bounce
105 ON $bounce.event_queue_id = $queue.id
107 ON $queue.job_id = $job.id
109 ON $job.mailing_id = $mailing.id
110 WHERE $mailing.id = " . CRM_Utils_Type
::escape($mailing_id, 'Integer');
112 if (!empty($toDate)) {
113 $query .= " AND $bounce.time_stamp <= $toDate";
116 if (!empty($job_id)) {
117 $query .= " AND $job.id = " . CRM_Utils_Type
::escape($job_id, 'Integer');
131 * Get rows for the event browser.
133 * @param int $mailing_id
136 * Optional ID of the job.
137 * @param bool $is_distinct
138 * Group by queue id?.
141 * @param int $rowCount
149 public static function &getRows(
150 $mailing_id, $job_id = NULL,
151 $is_distinct = FALSE, $offset = NULL, $rowCount = NULL, $sort = NULL
154 $dao = new CRM_Core_DAO();
156 $bounce = self
::getTableName();
157 $bounceType = CRM_Mailing_DAO_BounceType
::getTableName();
158 $queue = CRM_Mailing_Event_BAO_Queue
::getTableName();
159 $mailing = CRM_Mailing_BAO_Mailing
::getTableName();
160 $job = CRM_Mailing_BAO_MailingJob
::getTableName();
161 $contact = CRM_Contact_BAO_Contact
::getTableName();
162 $email = CRM_Core_BAO_Email
::getTableName();
165 SELECT $contact.display_name as display_name,
166 $contact.id as contact_id,
167 $email.email as email,
168 $bounce.time_stamp as date,
169 $bounce.bounce_reason as reason,
170 $bounceType.name as bounce_type
173 ON $queue.contact_id = $contact.id
175 ON $queue.email_id = $email.id
177 ON $bounce.event_queue_id = $queue.id
178 LEFT JOIN $bounceType
179 ON $bounce.bounce_type_id = $bounceType.id
181 ON $queue.job_id = $job.id
184 ON $job.mailing_id = $mailing.id
185 WHERE $mailing.id = " . CRM_Utils_Type
::escape($mailing_id, 'Integer');
187 if (!empty($job_id)) {
188 $query .= " AND $job.id = " . CRM_Utils_Type
::escape($job_id, 'Integer');
192 $query .= " GROUP BY $queue.id, $bounce.time_stamp, $bounce.bounce_reason, $bounceType.name ";
195 $orderBy = "sort_name ASC, {$bounce}.time_stamp DESC";
197 if (is_string($sort)) {
198 $sort = CRM_Utils_Type
::escape($sort, 'String');
202 $orderBy = trim($sort->orderBy());
205 $query .= " ORDER BY {$orderBy} ";
207 if ($offset ||
$rowCount) {
208 //Added "||$rowCount" to avoid displaying all records on first page
209 $query .= ' LIMIT ' . CRM_Utils_Type
::escape($offset, 'Integer') . ', ' . CRM_Utils_Type
::escape($rowCount, 'Integer');
216 while ($dao->fetch()) {
217 $url = CRM_Utils_System
::url('civicrm/contact/view',
218 "reset=1&cid={$dao->contact_id}"
221 'name' => "<a href=\"$url\">{$dao->display_name}</a>",
222 'email' => $dao->email
,
223 // FIXME: translate this
224 'type' => (empty($dao->bounce_type
) ?
ts('Unknown') : $dao->bounce_type
226 'reason' => $dao->reason
,
227 'date' => CRM_Utils_Date
::customFormat($dao->date
),
234 * Put the email on hold if it has met the threshold.
236 * @param int $email_id
238 protected static function putEmailOnHold($email_id) {
240 $bounceTable = CRM_Mailing_Event_BAO_Bounce
::getTableName();
241 $bounceType = CRM_Mailing_DAO_BounceType
::getTableName();
242 $emailTable = CRM_Core_BAO_Email
::getTableName();
243 $queueTable = CRM_Mailing_Event_BAO_Queue
::getTableName();
245 // might want to put distinct inside the count
246 $query = "SELECT count($bounceTable.id) as bounces,
247 $bounceType.hold_threshold as threshold
249 INNER JOIN $bounceType
250 ON $bounceTable.bounce_type_id = $bounceType.id
251 INNER JOIN $queueTable
252 ON $bounceTable.event_queue_id = $queueTable.id
253 INNER JOIN $emailTable
254 ON $queueTable.email_id = $emailTable.id
255 WHERE $emailTable.id = $email_id
256 AND ($emailTable.reset_date IS NULL
257 OR $bounceTable.time_stamp >= $emailTable.reset_date)
258 GROUP BY $bounceTable.bounce_type_id
259 ORDER BY threshold, bounces desc";
261 $dao = CRM_Core_DAO
::executeQuery($query);
263 while ($dao->fetch()) {
264 if ($dao->bounces
>= $dao->threshold
) {
265 $email = new CRM_Core_BAO_Email();
266 $email->id
= $email_id;
267 $email->on_hold
= TRUE;
268 $email->hold_date
= date('YmdHis');