3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
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
66 * 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.
123 public static function send($queue_id, &$mailing, &$bodyTxt, $replyto, &$bodyHTML = NULL, &$fullEmail = NULL) {
124 $domain = CRM_Core_BAO_Domain
::getDomain();
125 $emails = CRM_Core_BAO_Email
::getTableName();
126 $queue = CRM_Mailing_Event_BAO_Queue
::getTableName();
127 $contacts = CRM_Contact_BAO_Contact
::getTableName();
129 $eq = new CRM_Core_DAO();
130 $eq->query("SELECT $contacts.display_name as display_name,
131 $emails.email as email,
132 $queue.job_id as job_id,
136 ON $queue.contact_id = $contacts.id
138 ON $queue.email_id = $emails.id
139 WHERE $queue.id = " . CRM_Utils_Type
::escape($queue_id, 'Integer')
144 // parse the email and set a new destination
145 $parser = new ezcMailParser();
146 $set = new ezcMailVariableSet($fullEmail);
147 $parsed = array_shift($parser->parseMail($set));
148 $parsed->to
= array(new ezcMailAddress($mailing->replyto_email
));
150 // CRM-5567: we need to set Reply-To: so that any response
151 // to the forward goes to the sender of the reply
152 $parsed->setHeader('Reply-To', $replyto instanceof ezcMailAddress ?
$replyto : $parsed->from
->__toString());
154 // $h must be an array, so we can't use generateHeaders()'s result,
155 // but we have to regenerate the headers because we changed To
156 $parsed->generateHeaders();
157 $h = $parsed->headers
->getCaseSensitiveArray();
158 $b = $parsed->generateBody();
160 // strip Return-Path of possible bounding brackets, CRM-4502
161 if (!empty($h['Return-Path'])) {
162 $h['Return-Path'] = trim($h['Return-Path'], '<>');
165 // FIXME: ugly hack - find the first MIME boundary in
166 // the body and make the boundary in the header match it
167 $ct = $h['Content-Type'];
168 if (substr_count($ct, 'boundary=')) {
170 preg_match('/^--(.*)$/m', $b, $matches);
171 $boundary = rtrim($matches[1]);
172 $parts = explode('boundary=', $ct);
173 $ct = "{$parts[0]} boundary=\"$boundary\"";
177 $emailDomain = CRM_Core_BAO_MailSettings
::defaultDomain();
179 if (empty($eq->display_name
)) {
183 $from = "\"{$eq->display_name}\" <{$eq->email}>";
186 $message = new Mail_mime("\n");
189 'Subject' => "Re: {$mailing->subject}",
190 'To' => $mailing->replyto_email
,
192 'Reply-To' => empty($replyto) ?
$eq->email
: $replyto,
193 'Return-Path' => "do-not-reply@{$emailDomain}",
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 = $config->getMailer();
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.
225 private static function autoRespond(&$mailing, $queue_id, $replyto) {
226 $config = CRM_Core_Config
::singleton();
228 $contacts = CRM_Contact_DAO_Contact
::getTableName();
229 $email = CRM_Core_DAO_Email
::getTableName();
230 $queue = CRM_Mailing_Event_DAO_Queue
::getTableName();
232 $eq = new CRM_Core_DAO();
234 "SELECT $contacts.preferred_mail_format as format,
235 $email.email as email,
236 $queue.job_id as job_id,
239 INNER JOIN $queue ON $queue.contact_id = $contacts.id
240 INNER JOIN $email ON $queue.email_id = $email.id
241 WHERE $queue.id = " . CRM_Utils_Type
::escape($queue_id, 'Integer')
245 $to = empty($replyto) ?
$eq->email
: $replyto;
247 $component = new CRM_Mailing_BAO_Component();
248 $component->id
= $mailing->reply_id
;
249 $component->find(TRUE);
251 $message = new Mail_Mime("\n");
253 $domain = CRM_Core_BAO_Domain
::getDomain();
254 list($domainEmailName, $_) = CRM_Core_BAO_Domain
::getNameAndEmail();
256 $emailDomain = CRM_Core_BAO_MailSettings
::defaultDomain();
259 'Subject' => $component->subject
,
261 'From' => "\"$domainEmailName\" <do-not-reply@$emailDomain>",
262 'Reply-To' => "do-not-reply@$emailDomain",
263 'Return-Path' => "do-not-reply@$emailDomain",
266 /* TODO: do we need reply tokens? */
268 $html = $component->body_html
;
269 if ($component->body_text
) {
270 $text = $component->body_text
;
273 $text = CRM_Utils_String
::htmlToText($component->body_html
);
276 $bao = new CRM_Mailing_BAO_Mailing();
277 $bao->body_text
= $text;
278 $bao->body_html
= $html;
279 $tokens = $bao->getTokens();
281 if ($eq->format
== 'HTML' ||
$eq->format
== 'Both') {
282 $html = CRM_Utils_Token
::replaceDomainTokens($html, $domain, TRUE, $tokens['html']);
283 $html = CRM_Utils_Token
::replaceMailingTokens($html, $mailing, NULL, $tokens['html']);
284 $message->setHTMLBody($html);
286 if (!$html ||
$eq->format
== 'Text' ||
$eq->format
== 'Both') {
287 $text = CRM_Utils_Token
::replaceDomainTokens($text, $domain, FALSE, $tokens['text']);
288 $text = CRM_Utils_Token
::replaceMailingTokens($text, $mailing, NULL, $tokens['text']);
289 $message->setTxtBody($text);
292 $b = CRM_Utils_Mail
::setMimeParams($message);
293 $h = $message->headers($headers);
294 CRM_Mailing_BAO_Mailing
::addMessageIdHeader($h, 'a', $eq->job_id
, queue_id
, $eq->hash
);
296 $mailer = $config->getMailer();
297 if (is_object($mailer)) {
298 $errorScope = CRM_Core_TemporaryErrorScope
::ignoreException();
299 $mailer->send($to, $h, $b);
305 * Get row count for the event selector.
307 * @param int $mailing_id
310 * Optional ID of a job to filter on.
311 * @param bool $is_distinct
312 * Group by queue ID?.
315 * Number of rows in result set
317 public static function getTotalCount(
318 $mailing_id, $job_id = NULL,
321 $dao = new CRM_Core_DAO();
323 $reply = self
::getTableName();
324 $queue = CRM_Mailing_Event_BAO_Queue
::getTableName();
325 $mailing = CRM_Mailing_BAO_Mailing
::getTableName();
326 $job = CRM_Mailing_BAO_MailingJob
::getTableName();
329 SELECT COUNT($reply.id) as reply
332 ON $reply.event_queue_id = $queue.id
334 ON $queue.job_id = $job.id
336 ON $job.mailing_id = $mailing.id
338 WHERE $mailing.id = " . CRM_Utils_Type
::escape($mailing_id, 'Integer');
340 if (!empty($job_id)) {
341 $query .= " AND $job.id = " . CRM_Utils_Type
::escape($job_id, 'Integer');
345 $query .= " GROUP BY $queue.id ";
359 * Get rows for the event browser.
361 * @param int $mailing_id
364 * Optional ID of the job.
365 * @param bool $is_distinct
366 * Group by queue id?.
369 * @param int $rowCount
377 public static function &getRows(
378 $mailing_id, $job_id = NULL,
379 $is_distinct = FALSE, $offset = NULL, $rowCount = NULL, $sort = NULL
382 $dao = new CRM_Core_Dao();
384 $reply = self
::getTableName();
385 $queue = CRM_Mailing_Event_BAO_Queue
::getTableName();
386 $mailing = CRM_Mailing_BAO_Mailing
::getTableName();
387 $job = CRM_Mailing_BAO_MailingJob
::getTableName();
388 $contact = CRM_Contact_BAO_Contact
::getTableName();
389 $email = CRM_Core_BAO_Email
::getTableName();
392 SELECT $contact.display_name as display_name,
393 $contact.id as contact_id,
394 $email.email as email,
395 $reply.time_stamp as date
398 ON $queue.contact_id = $contact.id
400 ON $queue.email_id = $email.id
402 ON $reply.event_queue_id = $queue.id
404 ON $queue.job_id = $job.id
406 ON $job.mailing_id = $mailing.id
408 WHERE $mailing.id = " . CRM_Utils_Type
::escape($mailing_id, 'Integer');
410 if (!empty($job_id)) {
411 $query .= " AND $job.id = " . CRM_Utils_Type
::escape($job_id, 'Integer');
415 $query .= " GROUP BY $queue.id ";
418 $orderBy = "sort_name ASC, {$reply}.time_stamp DESC";
420 if (is_string($sort)) {
421 $sort = CRM_Utils_Type
::escape($sort, 'String');
425 $orderBy = trim($sort->orderBy());
429 $query .= " ORDER BY {$orderBy} ";
431 if ($offset ||
$rowCount) {
432 //Added "||$rowCount" to avoid displaying all records on first page
433 $query .= ' LIMIT ' . CRM_Utils_Type
::escape($offset, 'Integer') . ', ' . CRM_Utils_Type
::escape($rowCount, 'Integer');
440 while ($dao->fetch()) {
441 $url = CRM_Utils_System
::url('civicrm/contact/view',
442 "reset=1&cid={$dao->contact_id}"
445 'name' => "<a href=\"$url\">{$dao->display_name}</a>",
446 'email' => $dao->email
,
447 'date' => CRM_Utils_Date
::customFormat($dao->date
),