3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
36 require_once 'Mail/mime.php';
38 require_once 'ezc/Base/src/ezc_bootstrap.php';
39 require_once 'ezc/autoload/mail_autoload.php';
42 * Class CRM_Mailing_Event_BAO_Reply
44 class CRM_Mailing_Event_BAO_Reply
extends CRM_Mailing_Event_DAO_Reply
{
49 public function __construct() {
50 parent
::__construct();
54 * Register a reply event.
57 * The job ID of the reply.
58 * @param int $queue_id
63 * @param null $replyto
65 * @return object|null The mailing object, or null on failure
68 public static function &reply($job_id, $queue_id, $hash, $replyto = NULL) {
69 /* First make sure there's a matching queue event */
71 $q = CRM_Mailing_Event_BAO_Queue
::verify($job_id, $queue_id, $hash);
79 $mailing = new CRM_Mailing_BAO_Mailing();
80 $mailings = CRM_Mailing_BAO_Mailing
::getTableName();
81 $jobs = CRM_Mailing_BAO_MailingJob
::getTableName();
83 "SELECT * FROM $mailings
85 ON $jobs.mailing_id = $mailings.id
86 WHERE $jobs.id = {$q->job_id}"
89 if ($mailing->auto_responder
) {
90 self
::autoRespond($mailing, $queue_id, $replyto);
93 $re = new CRM_Mailing_Event_BAO_Reply();
94 $re->event_queue_id
= $queue_id;
95 $re->time_stamp
= date('YmdHis');
98 if (!$mailing->forward_replies ||
empty($mailing->replyto_email
)) {
106 * Forward a mailing reply
108 * @param int $queue_id
109 * Queue event ID of the sender.
110 * @param string $mailing
111 * The mailing object.
112 * @param string $bodyTxt
113 * Text part of the body (ignored if $fullEmail provided).
114 * @param string $replyto
115 * Reply-to of the incoming message.
116 * @param string $bodyHTML
117 * HTML part of the body (ignored if $fullEmail provided).
118 * @param string $fullEmail
119 * Whole email to forward in one string.
124 public static function send($queue_id, &$mailing, &$bodyTxt, $replyto, &$bodyHTML = NULL, &$fullEmail = NULL) {
125 $domain = CRM_Core_BAO_Domain
::getDomain();
126 $emails = CRM_Core_BAO_Email
::getTableName();
127 $queue = CRM_Mailing_Event_BAO_Queue
::getTableName();
128 $contacts = CRM_Contact_BAO_Contact
::getTableName();
130 $eq = new CRM_Core_DAO();
131 $eq->query("SELECT $contacts.display_name as display_name,
132 $emails.email as email,
133 $queue.job_id as job_id,
137 ON $queue.contact_id = $contacts.id
139 ON $queue.email_id = $emails.id
140 WHERE $queue.id = " . CRM_Utils_Type
::escape($queue_id, 'Integer')
145 // parse the email and set a new destination
146 $parser = new ezcMailParser
;
147 $set = new ezcMailVariableSet($fullEmail);
148 $parsed = array_shift($parser->parseMail($set));
149 $parsed->to
= array(new ezcMailAddress($mailing->replyto_email
));
151 // CRM-5567: we need to set Reply-To: so that any response
152 // to the forward goes to the sender of the reply
153 $parsed->setHeader('Reply-To', $replyto instanceof ezcMailAddress ?
$replyto : $parsed->from
->__toString());
155 // $h must be an array, so we can't use generateHeaders()'s result,
156 // but we have to regenerate the headers because we changed To
157 $parsed->generateHeaders();
158 $h = $parsed->headers
->getCaseSensitiveArray();
159 $b = $parsed->generateBody();
161 // strip Return-Path of possible bounding brackets, CRM-4502
162 if (!empty($h['Return-Path'])) {
163 $h['Return-Path'] = trim($h['Return-Path'], '<>');
166 // FIXME: ugly hack - find the first MIME boundary in
167 // the body and make the boundary in the header match it
168 $ct = $h['Content-Type'];
169 if (substr_count($ct, 'boundary=')) {
171 preg_match('/^--(.*)$/m', $b, $matches);
172 $boundary = rtrim($matches[1]);
173 $parts = explode('boundary=', $ct);
174 $ct = "{$parts[0]} boundary=\"$boundary\"";
178 $emailDomain = CRM_Core_BAO_MailSettings
::defaultDomain();
180 if (empty($eq->display_name
)) {
184 $from = "\"{$eq->display_name}\" <{$eq->email}>";
187 $message = new Mail_mime("\n");
190 'Subject' => "Re: {$mailing->subject}",
191 'To' => $mailing->replyto_email
,
193 'Reply-To' => empty($replyto) ?
$eq->email
: $replyto,
194 'Return-Path' => "do-not-reply@{$emailDomain}",
197 $message->setTxtBody($bodyTxt);
198 $message->setHTMLBody($bodyHTML);
199 $b = CRM_Utils_Mail
::setMimeParams($message);
200 $h = $message->headers($headers);
203 CRM_Mailing_BAO_Mailing
::addMessageIdHeader($h, 'r', $eq->job_id
, $queue_id, $eq->hash
);
204 $config = CRM_Core_Config
::singleton();
205 $mailer = $config->getMailer();
207 if (is_object($mailer)) {
208 $errorScope = CRM_Core_TemporaryErrorScope
::ignoreException();
209 $mailer->send($mailing->replyto_email
, $h, $b);
215 * Send an automated response
217 * @param object $mailing
218 * The mailing object.
219 * @param int $queue_id
221 * @param string $replyto
222 * Optional reply-to from the reply.
227 private static function autoRespond(&$mailing, $queue_id, $replyto) {
228 $config = CRM_Core_Config
::singleton();
230 $contacts = CRM_Contact_DAO_Contact
::getTableName();
231 $email = CRM_Core_DAO_Email
::getTableName();
232 $queue = CRM_Mailing_Event_DAO_Queue
::getTableName();
234 $eq = new CRM_Core_DAO();
236 "SELECT $contacts.preferred_mail_format as format,
237 $email.email as email,
238 $queue.job_id as job_id,
241 INNER JOIN $queue ON $queue.contact_id = $contacts.id
242 INNER JOIN $email ON $queue.email_id = $email.id
243 WHERE $queue.id = " . CRM_Utils_Type
::escape($queue_id, 'Integer')
247 $to = empty($replyto) ?
$eq->email
: $replyto;
249 $component = new CRM_Mailing_BAO_Component();
250 $component->id
= $mailing->reply_id
;
251 $component->find(TRUE);
253 $message = new Mail_Mime("\n");
255 $domain = CRM_Core_BAO_Domain
::getDomain();
256 list($domainEmailName, $_) = CRM_Core_BAO_Domain
::getNameAndEmail();
258 $emailDomain = CRM_Core_BAO_MailSettings
::defaultDomain();
261 'Subject' => $component->subject
,
263 'From' => "\"$domainEmailName\" <do-not-reply@$emailDomain>",
264 'Reply-To' => "do-not-reply@$emailDomain",
265 'Return-Path' => "do-not-reply@$emailDomain",
268 /* TODO: do we need reply tokens? */
270 $html = $component->body_html
;
271 if ($component->body_text
) {
272 $text = $component->body_text
;
275 $text = CRM_Utils_String
::htmlToText($component->body_html
);
278 $bao = new CRM_Mailing_BAO_Mailing();
279 $bao->body_text
= $text;
280 $bao->body_html
= $html;
281 $tokens = $bao->getTokens();
283 if ($eq->format
== 'HTML' ||
$eq->format
== 'Both') {
284 $html = CRM_Utils_Token
::replaceDomainTokens($html, $domain, TRUE, $tokens['html']);
285 $html = CRM_Utils_Token
::replaceMailingTokens($html, $mailing, NULL, $tokens['html']);
286 $message->setHTMLBody($html);
288 if (!$html ||
$eq->format
== 'Text' ||
$eq->format
== 'Both') {
289 $text = CRM_Utils_Token
::replaceDomainTokens($text, $domain, FALSE, $tokens['text']);
290 $text = CRM_Utils_Token
::replaceMailingTokens($text, $mailing, NULL, $tokens['text']);
291 $message->setTxtBody($text);
294 $b = CRM_Utils_Mail
::setMimeParams($message);
295 $h = $message->headers($headers);
296 CRM_Mailing_BAO_Mailing
::addMessageIdHeader($h, 'a', $eq->job_id
, queue_id
, $eq->hash
);
298 $mailer = $config->getMailer();
299 if (is_object($mailer)) {
300 $errorScope = CRM_Core_TemporaryErrorScope
::ignoreException();
301 $mailer->send($to, $h, $b);
307 * Get row count for the event selector
309 * @param int $mailing_id
312 * Optional ID of a job to filter on.
313 * @param bool $is_distinct
314 * Group by queue ID?.
316 * @return int Number of rows in result set
319 public static function getTotalCount(
320 $mailing_id, $job_id = NULL,
323 $dao = new CRM_Core_DAO();
325 $reply = self
::getTableName();
326 $queue = CRM_Mailing_Event_BAO_Queue
::getTableName();
327 $mailing = CRM_Mailing_BAO_Mailing
::getTableName();
328 $job = CRM_Mailing_BAO_MailingJob
::getTableName();
331 SELECT COUNT($reply.id) as reply
334 ON $reply.event_queue_id = $queue.id
336 ON $queue.job_id = $job.id
338 ON $job.mailing_id = $mailing.id
340 WHERE $mailing.id = " . CRM_Utils_Type
::escape($mailing_id, 'Integer');
342 if (!empty($job_id)) {
343 $query .= " AND $job.id = " . CRM_Utils_Type
::escape($job_id, 'Integer');
347 $query .= " GROUP BY $queue.id ";
361 * Get rows for the event browser
363 * @param int $mailing_id
366 * Optional ID of the job.
367 * @param bool $is_distinct
368 * Group by queue id?.
371 * @param int $rowCount
376 * @return array Result set
379 public static function &getRows(
380 $mailing_id, $job_id = NULL,
381 $is_distinct = FALSE, $offset = NULL, $rowCount = NULL, $sort = NULL
384 $dao = new CRM_Core_Dao();
386 $reply = self
::getTableName();
387 $queue = CRM_Mailing_Event_BAO_Queue
::getTableName();
388 $mailing = CRM_Mailing_BAO_Mailing
::getTableName();
389 $job = CRM_Mailing_BAO_MailingJob
::getTableName();
390 $contact = CRM_Contact_BAO_Contact
::getTableName();
391 $email = CRM_Core_BAO_Email
::getTableName();
394 SELECT $contact.display_name as display_name,
395 $contact.id as contact_id,
396 $email.email as email,
397 $reply.time_stamp as date
400 ON $queue.contact_id = $contact.id
402 ON $queue.email_id = $email.id
404 ON $reply.event_queue_id = $queue.id
406 ON $queue.job_id = $job.id
408 ON $job.mailing_id = $mailing.id
410 WHERE $mailing.id = " . CRM_Utils_Type
::escape($mailing_id, 'Integer');
412 if (!empty($job_id)) {
413 $query .= " AND $job.id = " . CRM_Utils_Type
::escape($job_id, 'Integer');
417 $query .= " GROUP BY $queue.id ";
420 $orderBy = "sort_name ASC, {$reply}.time_stamp DESC";
422 if (is_string($sort)) {
423 $sort = CRM_Utils_Type
::escape($sort, 'String');
427 $orderBy = trim($sort->orderBy());
431 $query .= " ORDER BY {$orderBy} ";
433 if ($offset ||
$rowCount) {
434 //Added "||$rowCount" to avoid displaying all records on first page
435 $query .= ' LIMIT ' . CRM_Utils_Type
::escape($offset, 'Integer') . ', ' . CRM_Utils_Type
::escape($rowCount, 'Integer');
442 while ($dao->fetch()) {
443 $url = CRM_Utils_System
::url('civicrm/contact/view',
444 "reset=1&cid={$dao->contact_id}"
447 'name' => "<a href=\"$url\">{$dao->display_name}</a>",
448 'email' => $dao->email
,
449 'date' => CRM_Utils_Date
::customFormat($dao->date
),