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
66 * The mailing object, or null on failure
69 public static function &reply($job_id, $queue_id, $hash, $replyto = NULL) {
70 /* First make sure there's a matching queue event */
72 $q = CRM_Mailing_Event_BAO_Queue
::verify($job_id, $queue_id, $hash);
80 $mailing = new CRM_Mailing_BAO_Mailing();
81 $mailings = CRM_Mailing_BAO_Mailing
::getTableName();
82 $jobs = CRM_Mailing_BAO_MailingJob
::getTableName();
84 "SELECT * FROM $mailings
86 ON $jobs.mailing_id = $mailings.id
87 WHERE $jobs.id = {$q->job_id}"
90 if ($mailing->auto_responder
) {
91 self
::autoRespond($mailing, $queue_id, $replyto);
94 $re = new CRM_Mailing_Event_BAO_Reply();
95 $re->event_queue_id
= $queue_id;
96 $re->time_stamp
= date('YmdHis');
99 if (!$mailing->forward_replies ||
empty($mailing->replyto_email
)) {
107 * Forward a mailing reply
109 * @param int $queue_id
110 * Queue event ID of the sender.
111 * @param string $mailing
112 * The mailing object.
113 * @param string $bodyTxt
114 * Text part of the body (ignored if $fullEmail provided).
115 * @param string $replyto
116 * Reply-to of the incoming message.
117 * @param string $bodyHTML
118 * HTML part of the body (ignored if $fullEmail provided).
119 * @param string $fullEmail
120 * Whole email to forward in one string.
125 public static function send($queue_id, &$mailing, &$bodyTxt, $replyto, &$bodyHTML = NULL, &$fullEmail = NULL) {
126 $domain = CRM_Core_BAO_Domain
::getDomain();
127 $emails = CRM_Core_BAO_Email
::getTableName();
128 $queue = CRM_Mailing_Event_BAO_Queue
::getTableName();
129 $contacts = CRM_Contact_BAO_Contact
::getTableName();
131 $eq = new CRM_Core_DAO();
132 $eq->query("SELECT $contacts.display_name as display_name,
133 $emails.email as email,
134 $queue.job_id as job_id,
138 ON $queue.contact_id = $contacts.id
140 ON $queue.email_id = $emails.id
141 WHERE $queue.id = " . CRM_Utils_Type
::escape($queue_id, 'Integer')
146 // parse the email and set a new destination
147 $parser = new ezcMailParser
;
148 $set = new ezcMailVariableSet($fullEmail);
149 $parsed = array_shift($parser->parseMail($set));
150 $parsed->to
= array(new ezcMailAddress($mailing->replyto_email
));
152 // CRM-5567: we need to set Reply-To: so that any response
153 // to the forward goes to the sender of the reply
154 $parsed->setHeader('Reply-To', $replyto instanceof ezcMailAddress ?
$replyto : $parsed->from
->__toString());
156 // $h must be an array, so we can't use generateHeaders()'s result,
157 // but we have to regenerate the headers because we changed To
158 $parsed->generateHeaders();
159 $h = $parsed->headers
->getCaseSensitiveArray();
160 $b = $parsed->generateBody();
162 // strip Return-Path of possible bounding brackets, CRM-4502
163 if (!empty($h['Return-Path'])) {
164 $h['Return-Path'] = trim($h['Return-Path'], '<>');
167 // FIXME: ugly hack - find the first MIME boundary in
168 // the body and make the boundary in the header match it
169 $ct = $h['Content-Type'];
170 if (substr_count($ct, 'boundary=')) {
172 preg_match('/^--(.*)$/m', $b, $matches);
173 $boundary = rtrim($matches[1]);
174 $parts = explode('boundary=', $ct);
175 $ct = "{$parts[0]} boundary=\"$boundary\"";
179 $emailDomain = CRM_Core_BAO_MailSettings
::defaultDomain();
181 if (empty($eq->display_name
)) {
185 $from = "\"{$eq->display_name}\" <{$eq->email}>";
188 $message = new Mail_mime("\n");
191 'Subject' => "Re: {$mailing->subject}",
192 'To' => $mailing->replyto_email
,
194 'Reply-To' => empty($replyto) ?
$eq->email
: $replyto,
195 'Return-Path' => "do-not-reply@{$emailDomain}",
198 $message->setTxtBody($bodyTxt);
199 $message->setHTMLBody($bodyHTML);
200 $b = CRM_Utils_Mail
::setMimeParams($message);
201 $h = $message->headers($headers);
204 CRM_Mailing_BAO_Mailing
::addMessageIdHeader($h, 'r', $eq->job_id
, $queue_id, $eq->hash
);
205 $config = CRM_Core_Config
::singleton();
206 $mailer = $config->getMailer();
208 if (is_object($mailer)) {
209 $errorScope = CRM_Core_TemporaryErrorScope
::ignoreException();
210 $mailer->send($mailing->replyto_email
, $h, $b);
216 * Send an automated response
218 * @param object $mailing
219 * The mailing object.
220 * @param int $queue_id
222 * @param string $replyto
223 * Optional reply-to from the reply.
228 private static function autoRespond(&$mailing, $queue_id, $replyto) {
229 $config = CRM_Core_Config
::singleton();
231 $contacts = CRM_Contact_DAO_Contact
::getTableName();
232 $email = CRM_Core_DAO_Email
::getTableName();
233 $queue = CRM_Mailing_Event_DAO_Queue
::getTableName();
235 $eq = new CRM_Core_DAO();
237 "SELECT $contacts.preferred_mail_format as format,
238 $email.email as email,
239 $queue.job_id as job_id,
242 INNER JOIN $queue ON $queue.contact_id = $contacts.id
243 INNER JOIN $email ON $queue.email_id = $email.id
244 WHERE $queue.id = " . CRM_Utils_Type
::escape($queue_id, 'Integer')
248 $to = empty($replyto) ?
$eq->email
: $replyto;
250 $component = new CRM_Mailing_BAO_Component();
251 $component->id
= $mailing->reply_id
;
252 $component->find(TRUE);
254 $message = new Mail_Mime("\n");
256 $domain = CRM_Core_BAO_Domain
::getDomain();
257 list($domainEmailName, $_) = CRM_Core_BAO_Domain
::getNameAndEmail();
259 $emailDomain = CRM_Core_BAO_MailSettings
::defaultDomain();
262 'Subject' => $component->subject
,
264 'From' => "\"$domainEmailName\" <do-not-reply@$emailDomain>",
265 'Reply-To' => "do-not-reply@$emailDomain",
266 'Return-Path' => "do-not-reply@$emailDomain",
269 /* TODO: do we need reply tokens? */
271 $html = $component->body_html
;
272 if ($component->body_text
) {
273 $text = $component->body_text
;
276 $text = CRM_Utils_String
::htmlToText($component->body_html
);
279 $bao = new CRM_Mailing_BAO_Mailing();
280 $bao->body_text
= $text;
281 $bao->body_html
= $html;
282 $tokens = $bao->getTokens();
284 if ($eq->format
== 'HTML' ||
$eq->format
== 'Both') {
285 $html = CRM_Utils_Token
::replaceDomainTokens($html, $domain, TRUE, $tokens['html']);
286 $html = CRM_Utils_Token
::replaceMailingTokens($html, $mailing, NULL, $tokens['html']);
287 $message->setHTMLBody($html);
289 if (!$html ||
$eq->format
== 'Text' ||
$eq->format
== 'Both') {
290 $text = CRM_Utils_Token
::replaceDomainTokens($text, $domain, FALSE, $tokens['text']);
291 $text = CRM_Utils_Token
::replaceMailingTokens($text, $mailing, NULL, $tokens['text']);
292 $message->setTxtBody($text);
295 $b = CRM_Utils_Mail
::setMimeParams($message);
296 $h = $message->headers($headers);
297 CRM_Mailing_BAO_Mailing
::addMessageIdHeader($h, 'a', $eq->job_id
, queue_id
, $eq->hash
);
299 $mailer = $config->getMailer();
300 if (is_object($mailer)) {
301 $errorScope = CRM_Core_TemporaryErrorScope
::ignoreException();
302 $mailer->send($to, $h, $b);
308 * Get row count for the event selector
310 * @param int $mailing_id
313 * Optional ID of a job to filter on.
314 * @param bool $is_distinct
315 * Group by queue ID?.
318 * Number of rows in result set
321 public static function getTotalCount(
322 $mailing_id, $job_id = NULL,
325 $dao = new CRM_Core_DAO();
327 $reply = self
::getTableName();
328 $queue = CRM_Mailing_Event_BAO_Queue
::getTableName();
329 $mailing = CRM_Mailing_BAO_Mailing
::getTableName();
330 $job = CRM_Mailing_BAO_MailingJob
::getTableName();
333 SELECT COUNT($reply.id) as reply
336 ON $reply.event_queue_id = $queue.id
338 ON $queue.job_id = $job.id
340 ON $job.mailing_id = $mailing.id
342 WHERE $mailing.id = " . CRM_Utils_Type
::escape($mailing_id, 'Integer');
344 if (!empty($job_id)) {
345 $query .= " AND $job.id = " . CRM_Utils_Type
::escape($job_id, 'Integer');
349 $query .= " GROUP BY $queue.id ";
363 * Get rows for the event browser
365 * @param int $mailing_id
368 * Optional ID of the job.
369 * @param bool $is_distinct
370 * Group by queue id?.
373 * @param int $rowCount
382 public static function &getRows(
383 $mailing_id, $job_id = NULL,
384 $is_distinct = FALSE, $offset = NULL, $rowCount = NULL, $sort = NULL
387 $dao = new CRM_Core_Dao();
389 $reply = self
::getTableName();
390 $queue = CRM_Mailing_Event_BAO_Queue
::getTableName();
391 $mailing = CRM_Mailing_BAO_Mailing
::getTableName();
392 $job = CRM_Mailing_BAO_MailingJob
::getTableName();
393 $contact = CRM_Contact_BAO_Contact
::getTableName();
394 $email = CRM_Core_BAO_Email
::getTableName();
397 SELECT $contact.display_name as display_name,
398 $contact.id as contact_id,
399 $email.email as email,
400 $reply.time_stamp as date
403 ON $queue.contact_id = $contact.id
405 ON $queue.email_id = $email.id
407 ON $reply.event_queue_id = $queue.id
409 ON $queue.job_id = $job.id
411 ON $job.mailing_id = $mailing.id
413 WHERE $mailing.id = " . CRM_Utils_Type
::escape($mailing_id, 'Integer');
415 if (!empty($job_id)) {
416 $query .= " AND $job.id = " . CRM_Utils_Type
::escape($job_id, 'Integer');
420 $query .= " GROUP BY $queue.id ";
423 $orderBy = "sort_name ASC, {$reply}.time_stamp DESC";
425 if (is_string($sort)) {
426 $sort = CRM_Utils_Type
::escape($sort, 'String');
430 $orderBy = trim($sort->orderBy());
434 $query .= " ORDER BY {$orderBy} ";
436 if ($offset ||
$rowCount) {
437 //Added "||$rowCount" to avoid displaying all records on first page
438 $query .= ' LIMIT ' . CRM_Utils_Type
::escape($offset, 'Integer') . ', ' . CRM_Utils_Type
::escape($rowCount, 'Integer');
445 while ($dao->fetch()) {
446 $url = CRM_Utils_System
::url('civicrm/contact/view',
447 "reset=1&cid={$dao->contact_id}"
450 'name' => "<a href=\"$url\">{$dao->display_name}</a>",
451 'email' => $dao->email
,
452 'date' => CRM_Utils_Date
::customFormat($dao->date
),