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
{
22 public function __construct() {
23 parent
::__construct();
27 * Create a new bounce event, update the email address if necessary
33 public static function create(&$params) {
34 $q = CRM_Mailing_Event_BAO_Queue
::verify($params['job_id'],
35 $params['event_queue_id'],
44 $transaction = new CRM_Core_Transaction();
45 $bounce = new CRM_Mailing_Event_BAO_Bounce();
46 $bounce->time_stamp
= date('YmdHis');
48 $action = empty($params['id']) ?
'create' : 'edit';
49 CRM_Utils_Hook
::pre($action, 'MailingEventBounce', CRM_Utils_Array
::value('id', $params), $params);
51 // if we dont have a valid bounce type, we should set it
52 // to bounce_type_id 11 which is Syntax error. this allows such email
53 // addresses to be bounce a few more time before being put on hold
55 // we changed this behavior since this bounce type might be due to some issue
56 // with the connection or smtp server etc
57 if (empty($params['bounce_type_id'])) {
58 $params['bounce_type_id'] = 11;
59 if (empty($params['bounce_reason'])) {
60 $params['bounce_reason'] = ts('Unknown bounce type: Could not parse bounce email');
64 // replace any invalid unicode characters with replacement characters
65 $params['bounce_reason'] = mb_convert_encoding($params['bounce_reason'], 'UTF-8', 'UTF-8');
67 // dev/mail#37 Replace 4-byte utf8 characaters with the unicode replacement character
68 // while CiviCRM does not support utf8mb4 for MySQL
69 $params['bounce_reason'] = preg_replace('/[\x{10000}-\x{10FFFF}]/u', "\xEF\xBF\xBD", $params['bounce_reason']);
72 $params['bounce_reason'] = mb_strcut($params['bounce_reason'], 0, 254);
74 $bounce->copyValues($params);
77 CRM_Utils_Hook
::post($action, 'MailingEventBounce', $bounce->id
, $bounce);
80 self
::putEmailOnHold($q->email_id
);
82 $transaction->commit();
88 * Get row count for the event selector.
90 * @param int $mailing_id
93 * Optional ID of a job to filter on.
94 * @param bool $is_distinct
97 * @param string|null $toDate
100 * Number of rows in result set
102 public static function getTotalCount($mailing_id, $job_id = NULL, $is_distinct = FALSE, $toDate = NULL) {
103 $dao = new CRM_Core_DAO();
105 $bounce = self
::getTableName();
106 $queue = CRM_Mailing_Event_BAO_Queue
::getTableName();
107 $mailing = CRM_Mailing_BAO_Mailing
::getTableName();
108 $job = CRM_Mailing_BAO_MailingJob
::getTableName();
111 SELECT COUNT($bounce.id) as bounce
114 ON $bounce.event_queue_id = $queue.id
116 ON $queue.job_id = $job.id
118 ON $job.mailing_id = $mailing.id
119 WHERE $mailing.id = " . CRM_Utils_Type
::escape($mailing_id, 'Integer');
121 if (!empty($toDate)) {
122 $query .= " AND $bounce.time_stamp <= $toDate";
125 if (!empty($job_id)) {
126 $query .= " AND $job.id = " . CRM_Utils_Type
::escape($job_id, 'Integer');
130 $query .= " GROUP BY $queue.id ";
144 * Get rows for the event browser.
146 * @param int $mailing_id
149 * Optional ID of the job.
150 * @param bool $is_distinct
151 * Group by queue id?.
154 * @param int $rowCount
162 public static function &getRows(
163 $mailing_id, $job_id = NULL,
164 $is_distinct = FALSE, $offset = NULL, $rowCount = NULL, $sort = NULL
167 $dao = new CRM_Core_DAO();
169 $bounce = self
::getTableName();
170 $bounceType = CRM_Mailing_DAO_BounceType
::getTableName();
171 $queue = CRM_Mailing_Event_BAO_Queue
::getTableName();
172 $mailing = CRM_Mailing_BAO_Mailing
::getTableName();
173 $job = CRM_Mailing_BAO_MailingJob
::getTableName();
174 $contact = CRM_Contact_BAO_Contact
::getTableName();
175 $email = CRM_Core_BAO_Email
::getTableName();
178 SELECT $contact.display_name as display_name,
179 $contact.id as contact_id,
180 $email.email as email,
181 $bounce.time_stamp as date,
182 $bounce.bounce_reason as reason,
183 $bounceType.name as bounce_type
186 ON $queue.contact_id = $contact.id
188 ON $queue.email_id = $email.id
190 ON $bounce.event_queue_id = $queue.id
191 LEFT JOIN $bounceType
192 ON $bounce.bounce_type_id = $bounceType.id
194 ON $queue.job_id = $job.id
197 ON $job.mailing_id = $mailing.id
198 WHERE $mailing.id = " . CRM_Utils_Type
::escape($mailing_id, 'Integer');
200 if (!empty($job_id)) {
201 $query .= " AND $job.id = " . CRM_Utils_Type
::escape($job_id, 'Integer');
205 $query .= " GROUP BY $queue.id, $bounce.time_stamp, $bounce.bounce_reason, $bounceType.name ";
208 $orderBy = "sort_name ASC, {$bounce}.time_stamp DESC";
210 if (is_string($sort)) {
211 $sort = CRM_Utils_Type
::escape($sort, 'String');
215 $orderBy = trim($sort->orderBy());
218 $query .= " ORDER BY {$orderBy} ";
220 if ($offset ||
$rowCount) {
221 //Added "||$rowCount" to avoid displaying all records on first page
222 $query .= ' LIMIT ' . CRM_Utils_Type
::escape($offset, 'Integer') . ', ' . CRM_Utils_Type
::escape($rowCount, 'Integer');
229 while ($dao->fetch()) {
230 $url = CRM_Utils_System
::url('civicrm/contact/view',
231 "reset=1&cid={$dao->contact_id}"
234 'name' => "<a href=\"$url\">{$dao->display_name}</a>",
235 'email' => $dao->email
,
236 // FIXME: translate this
237 'type' => (empty($dao->bounce_type
) ?
ts('Unknown') : $dao->bounce_type
239 'reason' => $dao->reason
,
240 'date' => CRM_Utils_Date
::customFormat($dao->date
),
247 * Put the email on hold if it has met the threshold.
249 * @param int $email_id
251 protected static function putEmailOnHold($email_id) {
253 $bounceTable = CRM_Mailing_Event_BAO_Bounce
::getTableName();
254 $bounceType = CRM_Mailing_DAO_BounceType
::getTableName();
255 $emailTable = CRM_Core_BAO_Email
::getTableName();
256 $queueTable = CRM_Mailing_Event_BAO_Queue
::getTableName();
258 // might want to put distinct inside the count
259 $query = "SELECT count($bounceTable.id) as bounces,
260 $bounceType.hold_threshold as threshold
262 INNER JOIN $bounceType
263 ON $bounceTable.bounce_type_id = $bounceType.id
264 INNER JOIN $queueTable
265 ON $bounceTable.event_queue_id = $queueTable.id
266 INNER JOIN $emailTable
267 ON $queueTable.email_id = $emailTable.id
268 WHERE $emailTable.id = $email_id
269 AND ($emailTable.reset_date IS NULL
270 OR $bounceTable.time_stamp >= $emailTable.reset_date)
271 GROUP BY $bounceTable.bounce_type_id
272 ORDER BY threshold, bounces desc";
274 $dao = CRM_Core_DAO
::executeQuery($query);
276 while ($dao->fetch()) {
277 if ($dao->bounces
>= $dao->threshold
) {
278 $email = new CRM_Core_BAO_Email();
279 $email->id
= $email_id;
280 $email->on_hold
= TRUE;
281 $email->hold_date
= date('YmdHis');