3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
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 function __construct() {
50 parent
::__construct();
54 * Register a reply event.
56 * @param int $job_id The job ID of the reply
57 * @param int $queue_id The queue event id
58 * @param string $hash The hash
60 * @param null $replyto
62 * @return object|null The mailing object, or null on failure
66 public static function &reply($job_id, $queue_id, $hash, $replyto = NULL) {
67 /* First make sure there's a matching queue event */
69 $q = CRM_Mailing_Event_BAO_Queue
::verify($job_id, $queue_id, $hash);
77 $mailing = new CRM_Mailing_BAO_Mailing();
78 $mailings = CRM_Mailing_BAO_Mailing
::getTableName();
79 $jobs = CRM_Mailing_BAO_MailingJob
::getTableName();
81 "SELECT * FROM $mailings
83 ON $jobs.mailing_id = $mailings.id
84 WHERE $jobs.id = {$q->job_id}"
87 if ($mailing->auto_responder
) {
88 self
::autoRespond($mailing, $queue_id, $replyto);
91 $re = new CRM_Mailing_Event_BAO_Reply();
92 $re->event_queue_id
= $queue_id;
93 $re->time_stamp
= date('YmdHis');
96 if (!$mailing->forward_replies ||
empty($mailing->replyto_email
)) {
104 * Forward a mailing reply
106 * @param int $queue_id Queue event ID of the sender
107 * @param string $mailing The mailing object
108 * @param string $bodyTxt text part of the body (ignored if $fullEmail provided)
109 * @param string $replyto Reply-to of the incoming message
110 * @param string $bodyHTML HTML part of the body (ignored if $fullEmail provided)
111 * @param string $fullEmail whole email to forward in one string
117 public static function send($queue_id, &$mailing, &$bodyTxt, $replyto, &$bodyHTML = NULL, &$fullEmail = NULL) {
118 $domain = CRM_Core_BAO_Domain
::getDomain();
119 $emails = CRM_Core_BAO_Email
::getTableName();
120 $queue = CRM_Mailing_Event_BAO_Queue
::getTableName();
121 $contacts = CRM_Contact_BAO_Contact
::getTableName();
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 // $h must be an array, so we can't use generateHeaders()'s result,
149 // but we have to regenerate the headers because we changed To
150 $parsed->generateHeaders();
151 $h = $parsed->headers
->getCaseSensitiveArray();
152 $b = $parsed->generateBody();
154 // strip Return-Path of possible bounding brackets, CRM-4502
155 if (!empty($h['Return-Path'])) {
156 $h['Return-Path'] = trim($h['Return-Path'], '<>');
159 // FIXME: ugly hack - find the first MIME boundary in
160 // the body and make the boundary in the header match it
161 $ct = $h['Content-Type'];
162 if (substr_count($ct, 'boundary=')) {
164 preg_match('/^--(.*)$/m', $b, $matches);
165 $boundary = rtrim($matches[1]);
166 $parts = explode('boundary=', $ct);
167 $ct = "{$parts[0]} boundary=\"$boundary\"";
171 $emailDomain = CRM_Core_BAO_MailSettings
::defaultDomain();
173 if (empty($eq->display_name
)) {
177 $from = "\"{$eq->display_name}\" <{$eq->email}>";
180 $message = new Mail_mime("\n");
183 'Subject' => "Re: {$mailing->subject}",
184 'To' => $mailing->replyto_email
,
186 'Reply-To' => empty($replyto) ?
$eq->email
: $replyto,
187 'Return-Path' => "do-not-reply@{$emailDomain}",
190 $message->setTxtBody($bodyTxt);
191 $message->setHTMLBody($bodyHTML);
192 $b = CRM_Utils_Mail
::setMimeParams($message);
193 $h = $message->headers($headers);
196 CRM_Mailing_BAO_Mailing
::addMessageIdHeader($h, 'r', $eq->job_id
, $queue_id, $eq->hash
);
197 $config = CRM_Core_Config
::singleton();
198 $mailer = $config->getMailer();
200 if (is_object($mailer)) {
201 $errorScope = CRM_Core_TemporaryErrorScope
::ignoreException();
202 $mailer->send($mailing->replyto_email
, $h, $b);
208 * Send an automated response
210 * @param object $mailing The mailing object
211 * @param int $queue_id The queue ID
212 * @param string $replyto Optional reply-to from the reply
218 private static function autoRespond(&$mailing, $queue_id, $replyto) {
219 $config = CRM_Core_Config
::singleton();
221 $contacts = CRM_Contact_DAO_Contact
::getTableName();
222 $email = CRM_Core_DAO_Email
::getTableName();
223 $queue = CRM_Mailing_Event_DAO_Queue
::getTableName();
225 $eq = new CRM_Core_DAO();
227 "SELECT $contacts.preferred_mail_format as format,
228 $email.email as email,
229 $queue.job_id as job_id,
232 INNER JOIN $queue ON $queue.contact_id = $contacts.id
233 INNER JOIN $email ON $queue.email_id = $email.id
234 WHERE $queue.id = " . CRM_Utils_Type
::escape($queue_id, 'Integer')
238 $to = empty($replyto) ?
$eq->email
: $replyto;
240 $component = new CRM_Mailing_BAO_Component();
241 $component->id
= $mailing->reply_id
;
242 $component->find(TRUE);
244 $message = new Mail_Mime("\n");
246 $domain = CRM_Core_BAO_Domain
::getDomain();
247 list($domainEmailName, $_) = CRM_Core_BAO_Domain
::getNameAndEmail();
249 $emailDomain = CRM_Core_BAO_MailSettings
::defaultDomain();
252 'Subject' => $component->subject
,
254 'From' => "\"$domainEmailName\" <do-not-reply@$emailDomain>",
255 'Reply-To' => "do-not-reply@$emailDomain",
256 'Return-Path' => "do-not-reply@$emailDomain",
259 /* TODO: do we need reply tokens? */
261 $html = $component->body_html
;
262 if ($component->body_text
) {
263 $text = $component->body_text
;
266 $text = CRM_Utils_String
::htmlToText($component->body_html
);
269 $bao = new CRM_Mailing_BAO_Mailing();
270 $bao->body_text
= $text;
271 $bao->body_html
= $html;
272 $tokens = $bao->getTokens();
274 if ($eq->format
== 'HTML' ||
$eq->format
== 'Both') {
275 $html = CRM_Utils_Token
::replaceDomainTokens($html, $domain, TRUE, $tokens['html']);
276 $html = CRM_Utils_Token
::replaceMailingTokens($html, $mailing, NULL, $tokens['html']);
277 $message->setHTMLBody($html);
279 if (!$html ||
$eq->format
== 'Text' ||
$eq->format
== 'Both') {
280 $text = CRM_Utils_Token
::replaceDomainTokens($text, $domain, FALSE, $tokens['text']);
281 $text = CRM_Utils_Token
::replaceMailingTokens($text, $mailing, NULL, $tokens['text']);
282 $message->setTxtBody($text);
285 $b = CRM_Utils_Mail
::setMimeParams($message);
286 $h = $message->headers($headers);
287 CRM_Mailing_BAO_Mailing
::addMessageIdHeader($h, 'a', $eq->job_id
, queue_id
, $eq->hash
);
289 $mailer = $config->getMailer();
290 if (is_object($mailer)) {
291 $errorScope = CRM_Core_TemporaryErrorScope
::ignoreException();
292 $mailer->send($to, $h, $b);
298 * Get row count for the event selector
300 * @param int $mailing_id ID of the mailing
301 * @param int $job_id Optional ID of a job to filter on
302 * @param boolean $is_distinct Group by queue ID?
304 * @return int Number of rows in result set
308 public static function getTotalCount($mailing_id, $job_id = NULL,
311 $dao = new CRM_Core_DAO();
313 $reply = self
::getTableName();
314 $queue = CRM_Mailing_Event_BAO_Queue
::getTableName();
315 $mailing = CRM_Mailing_BAO_Mailing
::getTableName();
316 $job = CRM_Mailing_BAO_MailingJob
::getTableName();
319 SELECT COUNT($reply.id) as reply
322 ON $reply.event_queue_id = $queue.id
324 ON $queue.job_id = $job.id
326 ON $job.mailing_id = $mailing.id
328 WHERE $mailing.id = " . CRM_Utils_Type
::escape($mailing_id, 'Integer');
330 if (!empty($job_id)) {
331 $query .= " AND $job.id = " . CRM_Utils_Type
::escape($job_id, 'Integer');
335 $query .= " GROUP BY $queue.id ";
349 * Get rows for the event browser
351 * @param int $mailing_id ID of the mailing
352 * @param int $job_id optional ID of the job
353 * @param boolean $is_distinct Group by queue id?
354 * @param int $offset Offset
355 * @param int $rowCount Number of rows
356 * @param array $sort sort array
358 * @return array Result set
362 public static function &getRows($mailing_id, $job_id = NULL,
363 $is_distinct = FALSE, $offset = NULL, $rowCount = NULL, $sort = NULL
366 $dao = new CRM_Core_Dao();
368 $reply = self
::getTableName();
369 $queue = CRM_Mailing_Event_BAO_Queue
::getTableName();
370 $mailing = CRM_Mailing_BAO_Mailing
::getTableName();
371 $job = CRM_Mailing_BAO_MailingJob
::getTableName();
372 $contact = CRM_Contact_BAO_Contact
::getTableName();
373 $email = CRM_Core_BAO_Email
::getTableName();
376 SELECT $contact.display_name as display_name,
377 $contact.id as contact_id,
378 $email.email as email,
379 $reply.time_stamp as date
382 ON $queue.contact_id = $contact.id
384 ON $queue.email_id = $email.id
386 ON $reply.event_queue_id = $queue.id
388 ON $queue.job_id = $job.id
390 ON $job.mailing_id = $mailing.id
392 WHERE $mailing.id = " . CRM_Utils_Type
::escape($mailing_id, 'Integer');
394 if (!empty($job_id)) {
395 $query .= " AND $job.id = " . CRM_Utils_Type
::escape($job_id, 'Integer');
399 $query .= " GROUP BY $queue.id ";
402 $orderBy = "sort_name ASC, {$reply}.time_stamp DESC";
404 if (is_string($sort)) {
405 $sort = CRM_Utils_Type
::escape($sort, 'String');
409 $orderBy = trim($sort->orderBy());
413 $query .= " ORDER BY {$orderBy} ";
415 if ($offset ||
$rowCount) {
416 //Added "||$rowCount" to avoid displaying all records on first page
417 $query .= ' LIMIT ' . CRM_Utils_Type
::escape($offset, 'Integer') . ', ' . CRM_Utils_Type
::escape($rowCount, 'Integer');
424 while ($dao->fetch()) {
425 $url = CRM_Utils_System
::url('civicrm/contact/view',
426 "reset=1&cid={$dao->contact_id}"
429 'name' => "<a href=\"$url\">{$dao->display_name}</a>",
430 'email' => $dao->email
,
431 'date' => CRM_Utils_Date
::customFormat($dao->date
),