3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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-2018
34 require_once 'Mail/mime.php';
37 * Class CRM_Mailing_Event_BAO_Reply
39 class CRM_Mailing_Event_BAO_Reply
extends CRM_Mailing_Event_DAO_Reply
{
44 public function __construct() {
45 parent
::__construct();
49 * Register a reply event.
52 * The job ID of the reply.
53 * @param int $queue_id
58 * @param null $replyto
61 * The mailing object, or null on failure
63 public static function &reply($job_id, $queue_id, $hash, $replyto = NULL) {
64 // First make sure there's a matching queue event.
65 $q = CRM_Mailing_Event_BAO_Queue
::verify($job_id, $queue_id, $hash);
73 $mailing = new CRM_Mailing_BAO_Mailing();
74 $mailings = CRM_Mailing_BAO_Mailing
::getTableName();
75 $jobs = CRM_Mailing_BAO_MailingJob
::getTableName();
77 "SELECT * FROM $mailings
79 ON $jobs.mailing_id = $mailings.id
80 WHERE $jobs.id = {$q->job_id}"
83 if ($mailing->auto_responder
) {
84 self
::autoRespond($mailing, $queue_id, $replyto);
87 $re = new CRM_Mailing_Event_BAO_Reply();
88 $re->event_queue_id
= $queue_id;
89 $re->time_stamp
= date('YmdHis');
92 if (!$mailing->forward_replies ||
empty($mailing->replyto_email
)) {
100 * Forward a mailing reply.
102 * @param int $queue_id
103 * Queue event ID of the sender.
104 * @param string $mailing
105 * The mailing object.
106 * @param string $bodyTxt
107 * Text part of the body (ignored if $fullEmail provided).
108 * @param string $replyto
109 * Reply-to of the incoming message.
110 * @param string $bodyHTML
111 * HTML part of the body (ignored if $fullEmail provided).
112 * @param string $fullEmail
113 * Whole email to forward in one string.
115 public static function send($queue_id, &$mailing, &$bodyTxt, $replyto, &$bodyHTML = NULL, &$fullEmail = NULL) {
116 $domain = CRM_Core_BAO_Domain
::getDomain();
117 $emails = CRM_Core_BAO_Email
::getTableName();
118 $queue = CRM_Mailing_Event_BAO_Queue
::getTableName();
119 $contacts = CRM_Contact_BAO_Contact
::getTableName();
120 $domain_id = CRM_Core_Config
::domainID();
121 $domainValues = civicrm_api3('Domain', 'get', array('sequential' => 1, 'id' => $domain_id));
123 $eq = new CRM_Core_DAO();
124 $eq->query("SELECT $contacts.display_name as display_name,
125 $emails.email as email,
126 $queue.job_id as job_id,
130 ON $queue.contact_id = $contacts.id
132 ON $queue.email_id = $emails.id
133 WHERE $queue.id = " . CRM_Utils_Type
::escape($queue_id, 'Integer')
138 // parse the email and set a new destination
139 $parser = new ezcMailParser();
140 $set = new ezcMailVariableSet($fullEmail);
141 $parsed = array_shift($parser->parseMail($set));
142 $parsed->to
= array(new ezcMailAddress($mailing->replyto_email
));
144 // CRM-5567: we need to set Reply-To: so that any response
145 // to the forward goes to the sender of the reply
146 $parsed->setHeader('Reply-To', $replyto instanceof ezcMailAddress ?
$replyto : $parsed->from
->__toString());
148 // CRM-17754 Include re-sent headers to indicate that we have forwarded on the email
149 $domainEmail = $domainValues['values'][0]['from_email'];
150 $parsed->setHeader('Resent-From', $domainEmail);
151 $parsed->setHeader('Resent-Date', date('r'));
153 // $h must be an array, so we can't use generateHeaders()'s result,
154 // but we have to regenerate the headers because we changed To
155 $parsed->generateHeaders();
156 $h = $parsed->headers
->getCaseSensitiveArray();
157 $b = $parsed->generateBody();
159 // strip Return-Path of possible bounding brackets, CRM-4502
160 if (!empty($h['Return-Path'])) {
161 $h['Return-Path'] = trim($h['Return-Path'], '<>');
164 // FIXME: ugly hack - find the first MIME boundary in
165 // the body and make the boundary in the header match it
166 $ct = $h['Content-Type'];
167 if (substr_count($ct, 'boundary=')) {
169 preg_match('/^--(.*)$/m', $b, $matches);
170 $boundary = rtrim($matches[1]);
171 $parts = explode('boundary=', $ct);
172 $ct = "{$parts[0]} boundary=\"$boundary\"";
176 if (empty($eq->display_name
)) {
180 $from = "\"{$eq->display_name}\" <{$eq->email}>";
183 $message = new Mail_mime("\n");
186 'Subject' => "Re: {$mailing->subject}",
187 'To' => $mailing->replyto_email
,
189 'Reply-To' => empty($replyto) ?
$eq->email
: $replyto,
190 'Return-Path' => CRM_Core_BAO_Domain
::getNoReplyEmailAddress(),
191 // CRM-17754 Include re-sent headers to indicate that we have forwarded on the email
192 'Resent-From' => $domainValues['values'][0]['from_email'],
193 'Resent-Date' => date('r'),
196 $message->setTxtBody($bodyTxt);
197 $message->setHTMLBody($bodyHTML);
198 $b = CRM_Utils_Mail
::setMimeParams($message);
199 $h = $message->headers($headers);
202 CRM_Mailing_BAO_Mailing
::addMessageIdHeader($h, 'r', $eq->job_id
, $queue_id, $eq->hash
);
203 $config = CRM_Core_Config
::singleton();
204 $mailer = \Civi
::service('pear_mail');
206 if (is_object($mailer)) {
207 $errorScope = CRM_Core_TemporaryErrorScope
::ignoreException();
208 $mailer->send($mailing->replyto_email
, $h, $b);
214 * Send an automated response.
216 * @param object $mailing
217 * The mailing object.
218 * @param int $queue_id
220 * @param string $replyto
221 * Optional reply-to from the reply.
223 private static function autoRespond(&$mailing, $queue_id, $replyto) {
224 $config = CRM_Core_Config
::singleton();
226 $contacts = CRM_Contact_DAO_Contact
::getTableName();
227 $email = CRM_Core_DAO_Email
::getTableName();
228 $queue = CRM_Mailing_Event_DAO_Queue
::getTableName();
230 $eq = new CRM_Core_DAO();
232 "SELECT $contacts.preferred_mail_format as format,
233 $email.email as email,
234 $queue.job_id as job_id,
237 INNER JOIN $queue ON $queue.contact_id = $contacts.id
238 INNER JOIN $email ON $queue.email_id = $email.id
239 WHERE $queue.id = " . CRM_Utils_Type
::escape($queue_id, 'Integer')
243 $to = empty($replyto) ?
$eq->email
: $replyto;
245 $component = new CRM_Mailing_BAO_MailingComponent();
246 $component->id
= $mailing->reply_id
;
247 $component->find(TRUE);
249 $message = new Mail_Mime("\n");
251 $domain = CRM_Core_BAO_Domain
::getDomain();
252 list($domainEmailName, $_) = CRM_Core_BAO_Domain
::getNameAndEmail();
255 'Subject' => $component->subject
,
257 'From' => "\"$domainEmailName\" <" . CRM_Core_BAO_Domain
::getNoReplyEmailAddress() . '>',
258 'Reply-To' => CRM_Core_BAO_Domain
::getNoReplyEmailAddress(),
259 'Return-Path' => CRM_Core_BAO_Domain
::getNoReplyEmailAddress(),
262 // TODO: do we need reply tokens?
263 $html = $component->body_html
;
264 if ($component->body_text
) {
265 $text = $component->body_text
;
268 $text = CRM_Utils_String
::htmlToText($component->body_html
);
271 $bao = new CRM_Mailing_BAO_Mailing();
272 $bao->body_text
= $text;
273 $bao->body_html
= $html;
274 $tokens = $bao->getTokens();
276 if ($eq->format
== 'HTML' ||
$eq->format
== 'Both') {
277 $html = CRM_Utils_Token
::replaceDomainTokens($html, $domain, TRUE, $tokens['html']);
278 $html = CRM_Utils_Token
::replaceMailingTokens($html, $mailing, NULL, $tokens['html']);
279 $message->setHTMLBody($html);
281 if (!$html ||
$eq->format
== 'Text' ||
$eq->format
== 'Both') {
282 $text = CRM_Utils_Token
::replaceDomainTokens($text, $domain, FALSE, $tokens['text']);
283 $text = CRM_Utils_Token
::replaceMailingTokens($text, $mailing, NULL, $tokens['text']);
284 $message->setTxtBody($text);
287 $b = CRM_Utils_Mail
::setMimeParams($message);
288 $h = $message->headers($headers);
289 CRM_Mailing_BAO_Mailing
::addMessageIdHeader($h, 'a', $eq->job_id
, queue_id
, $eq->hash
);
291 $mailer = \Civi
::service('pear_mail');
292 if (is_object($mailer)) {
293 $errorScope = CRM_Core_TemporaryErrorScope
::ignoreException();
294 $mailer->send($to, $h, $b);
300 * Get row count for the event selector.
302 * @param int $mailing_id
305 * Optional ID of a job to filter on.
306 * @param bool $is_distinct
307 * Group by queue ID?.
310 * Number of rows in result set
312 public static function getTotalCount(
313 $mailing_id, $job_id = NULL,
316 $dao = new CRM_Core_DAO();
318 $reply = self
::getTableName();
319 $queue = CRM_Mailing_Event_BAO_Queue
::getTableName();
320 $mailing = CRM_Mailing_BAO_Mailing
::getTableName();
321 $job = CRM_Mailing_BAO_MailingJob
::getTableName();
324 SELECT COUNT($reply.id) as reply
327 ON $reply.event_queue_id = $queue.id
329 ON $queue.job_id = $job.id
331 ON $job.mailing_id = $mailing.id
333 WHERE $mailing.id = " . CRM_Utils_Type
::escape($mailing_id, 'Integer');
335 if (!empty($job_id)) {
336 $query .= " AND $job.id = " . CRM_Utils_Type
::escape($job_id, 'Integer');
340 $query .= " GROUP BY $queue.id ";
354 * Get rows for the event browser.
356 * @param int $mailing_id
359 * Optional ID of the job.
360 * @param bool $is_distinct
361 * Group by queue id?.
364 * @param int $rowCount
372 public static function &getRows(
373 $mailing_id, $job_id = NULL,
374 $is_distinct = FALSE, $offset = NULL, $rowCount = NULL, $sort = NULL
377 $dao = new CRM_Core_Dao();
379 $reply = self
::getTableName();
380 $queue = CRM_Mailing_Event_BAO_Queue
::getTableName();
381 $mailing = CRM_Mailing_BAO_Mailing
::getTableName();
382 $job = CRM_Mailing_BAO_MailingJob
::getTableName();
383 $contact = CRM_Contact_BAO_Contact
::getTableName();
384 $email = CRM_Core_BAO_Email
::getTableName();
387 SELECT $contact.display_name as display_name,
388 $contact.id as contact_id,
389 $email.email as email,
390 $reply.time_stamp as date
393 ON $queue.contact_id = $contact.id
395 ON $queue.email_id = $email.id
397 ON $reply.event_queue_id = $queue.id
399 ON $queue.job_id = $job.id
401 ON $job.mailing_id = $mailing.id
403 WHERE $mailing.id = " . CRM_Utils_Type
::escape($mailing_id, 'Integer');
405 if (!empty($job_id)) {
406 $query .= " AND $job.id = " . CRM_Utils_Type
::escape($job_id, 'Integer');
410 $query .= " GROUP BY $queue.id, $contact.id, $reply.time_stamp ";
413 $orderBy = "sort_name ASC, {$reply}.time_stamp DESC";
415 if (is_string($sort)) {
416 $sort = CRM_Utils_Type
::escape($sort, 'String');
420 $orderBy = trim($sort->orderBy());
424 $query .= " ORDER BY {$orderBy} ";
426 if ($offset ||
$rowCount) {
427 //Added "||$rowCount" to avoid displaying all records on first page
428 $query .= ' LIMIT ' . CRM_Utils_Type
::escape($offset, 'Integer') . ', ' . CRM_Utils_Type
::escape($rowCount, 'Integer');
435 while ($dao->fetch()) {
436 $url = CRM_Utils_System
::url('civicrm/contact/view',
437 "reset=1&cid={$dao->contact_id}"
440 'name' => "<a href=\"$url\">{$dao->display_name}</a>",
441 'email' => $dao->email
,
442 'date' => CRM_Utils_Date
::customFormat($dao->date
),