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';
40 class CRM_Mailing_Event_BAO_Reply
extends CRM_Mailing_Event_DAO_Reply
{
45 function __construct() {
46 parent
::__construct();
50 * Register a reply event.
52 * @param int $job_id The job ID of the reply
53 * @param int $queue_id The queue event id
54 * @param string $hash The hash
56 * @param null $replyto
58 * @return object|null The mailing object, or null on failure
62 public static function &reply($job_id, $queue_id, $hash, $replyto = NULL) {
63 /* 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 Queue event ID of the sender
103 * @param string $mailing The mailing object
104 * @param string $bodyTxt text part of the body (ignored if $fullEmail provided)
105 * @param string $replyto Reply-to of the incoming message
106 * @param string $bodyHTML HTML part of the body (ignored if $fullEmail provided)
107 * @param string $fullEmail whole email to forward in one string
113 public static function send($queue_id, &$mailing, &$bodyTxt, $replyto, &$bodyHTML = NULL, &$fullEmail = NULL) {
114 $domain = CRM_Core_BAO_Domain
::getDomain();
115 $emails = CRM_Core_BAO_Email
::getTableName();
116 $queue = CRM_Mailing_Event_BAO_Queue
::getTableName();
117 $contacts = CRM_Contact_BAO_Contact
::getTableName();
119 $eq = new CRM_Core_DAO();
120 $eq->query("SELECT $contacts.display_name as display_name,
121 $emails.email as email,
122 $queue.job_id as job_id,
126 ON $queue.contact_id = $contacts.id
128 ON $queue.email_id = $emails.id
129 WHERE $queue.id = " . CRM_Utils_Type
::escape($queue_id, 'Integer')
134 // parse the email and set a new destination
135 $parser = new ezcMailParser
;
136 $set = new ezcMailVariableSet($fullEmail);
137 $parsed = array_shift($parser->parseMail($set));
138 $parsed->to
= array(new ezcMailAddress($mailing->replyto_email
));
140 // CRM-5567: we need to set Reply-To: so that any response
141 // to the forward goes to the sender of the reply
142 $parsed->setHeader('Reply-To', $replyto instanceof ezcMailAddress ?
$replyto : $parsed->from
->__toString());
144 // $h must be an array, so we can't use generateHeaders()'s result,
145 // but we have to regenerate the headers because we changed To
146 $parsed->generateHeaders();
147 $h = $parsed->headers
->getCaseSensitiveArray();
148 $b = $parsed->generateBody();
150 // strip Return-Path of possible bounding brackets, CRM-4502
151 if (!empty($h['Return-Path'])) {
152 $h['Return-Path'] = trim($h['Return-Path'], '<>');
155 // FIXME: ugly hack - find the first MIME boundary in
156 // the body and make the boundary in the header match it
157 $ct = $h['Content-Type'];
158 if (substr_count($ct, 'boundary=')) {
160 preg_match('/^--(.*)$/m', $b, $matches);
161 $boundary = rtrim($matches[1]);
162 $parts = explode('boundary=', $ct);
163 $ct = "{$parts[0]} boundary=\"$boundary\"";
167 $emailDomain = CRM_Core_BAO_MailSettings
::defaultDomain();
169 if (empty($eq->display_name
)) {
173 $from = "\"{$eq->display_name}\" <{$eq->email}>";
176 $message = new Mail_mime("\n");
179 'Subject' => "Re: {$mailing->subject}",
180 'To' => $mailing->replyto_email
,
182 'Reply-To' => empty($replyto) ?
$eq->email
: $replyto,
183 'Return-Path' => "do-not-reply@{$emailDomain}",
186 $message->setTxtBody($bodyTxt);
187 $message->setHTMLBody($bodyHTML);
188 $b = CRM_Utils_Mail
::setMimeParams($message);
189 $h = $message->headers($headers);
192 CRM_Mailing_BAO_Mailing
::addMessageIdHeader($h, 'r', $eq->job_id
, $queue_id, $eq->hash
);
193 $config = CRM_Core_Config
::singleton();
194 $mailer = $config->getMailer();
196 if (is_object($mailer)) {
197 $errorScope = CRM_Core_TemporaryErrorScope
::ignoreException();
198 $mailer->send($mailing->replyto_email
, $h, $b);
204 * Send an automated response
206 * @param object $mailing The mailing object
207 * @param int $queue_id The queue ID
208 * @param string $replyto Optional reply-to from the reply
214 private static function autoRespond(&$mailing, $queue_id, $replyto) {
215 $config = CRM_Core_Config
::singleton();
217 $contacts = CRM_Contact_DAO_Contact
::getTableName();
218 $email = CRM_Core_DAO_Email
::getTableName();
219 $queue = CRM_Mailing_Event_DAO_Queue
::getTableName();
221 $eq = new CRM_Core_DAO();
223 "SELECT $contacts.preferred_mail_format as format,
224 $email.email as email,
225 $queue.job_id as job_id,
228 INNER JOIN $queue ON $queue.contact_id = $contacts.id
229 INNER JOIN $email ON $queue.email_id = $email.id
230 WHERE $queue.id = " . CRM_Utils_Type
::escape($queue_id, 'Integer')
234 $to = empty($replyto) ?
$eq->email
: $replyto;
236 $component = new CRM_Mailing_BAO_Component();
237 $component->id
= $mailing->reply_id
;
238 $component->find(TRUE);
240 $message = new Mail_Mime("\n");
242 $domain = CRM_Core_BAO_Domain
::getDomain();
243 list($domainEmailName, $_) = CRM_Core_BAO_Domain
::getNameAndEmail();
245 $emailDomain = CRM_Core_BAO_MailSettings
::defaultDomain();
248 'Subject' => $component->subject
,
250 'From' => "\"$domainEmailName\" <do-not-reply@$emailDomain>",
251 'Reply-To' => "do-not-reply@$emailDomain",
252 'Return-Path' => "do-not-reply@$emailDomain",
255 /* TODO: do we need reply tokens? */
257 $html = $component->body_html
;
258 if ($component->body_text
) {
259 $text = $component->body_text
;
262 $text = CRM_Utils_String
::htmlToText($component->body_html
);
265 $bao = new CRM_Mailing_BAO_Mailing();
266 $bao->body_text
= $text;
267 $bao->body_html
= $html;
268 $tokens = $bao->getTokens();
270 if ($eq->format
== 'HTML' ||
$eq->format
== 'Both') {
271 $html = CRM_Utils_Token
::replaceDomainTokens($html, $domain, TRUE, $tokens['html']);
272 $html = CRM_Utils_Token
::replaceMailingTokens($html, $mailing, NULL, $tokens['html']);
273 $message->setHTMLBody($html);
275 if (!$html ||
$eq->format
== 'Text' ||
$eq->format
== 'Both') {
276 $text = CRM_Utils_Token
::replaceDomainTokens($text, $domain, FALSE, $tokens['text']);
277 $text = CRM_Utils_Token
::replaceMailingTokens($text, $mailing, NULL, $tokens['text']);
278 $message->setTxtBody($text);
281 $b = CRM_Utils_Mail
::setMimeParams($message);
282 $h = $message->headers($headers);
283 CRM_Mailing_BAO_Mailing
::addMessageIdHeader($h, 'a', $eq->job_id
, queue_id
, $eq->hash
);
285 $mailer = $config->getMailer();
286 if (is_object($mailer)) {
287 $errorScope = CRM_Core_TemporaryErrorScope
::ignoreException();
288 $mailer->send($to, $h, $b);
294 * Get row count for the event selector
296 * @param int $mailing_id ID of the mailing
297 * @param int $job_id Optional ID of a job to filter on
298 * @param boolean $is_distinct Group by queue ID?
300 * @return int Number of rows in result set
304 public static function getTotalCount($mailing_id, $job_id = NULL,
307 $dao = new CRM_Core_DAO();
309 $reply = self
::getTableName();
310 $queue = CRM_Mailing_Event_BAO_Queue
::getTableName();
311 $mailing = CRM_Mailing_BAO_Mailing
::getTableName();
312 $job = CRM_Mailing_BAO_MailingJob
::getTableName();
315 SELECT COUNT($reply.id) as reply
318 ON $reply.event_queue_id = $queue.id
320 ON $queue.job_id = $job.id
322 ON $job.mailing_id = $mailing.id
324 WHERE $mailing.id = " . CRM_Utils_Type
::escape($mailing_id, 'Integer');
326 if (!empty($job_id)) {
327 $query .= " AND $job.id = " . CRM_Utils_Type
::escape($job_id, 'Integer');
331 $query .= " GROUP BY $queue.id ";
345 * Get rows for the event browser
347 * @param int $mailing_id ID of the mailing
348 * @param int $job_id optional ID of the job
349 * @param boolean $is_distinct Group by queue id?
350 * @param int $offset Offset
351 * @param int $rowCount Number of rows
352 * @param array $sort sort array
354 * @return array Result set
358 public static function &getRows($mailing_id, $job_id = NULL,
359 $is_distinct = FALSE, $offset = NULL, $rowCount = NULL, $sort = NULL
362 $dao = new CRM_Core_Dao();
364 $reply = self
::getTableName();
365 $queue = CRM_Mailing_Event_BAO_Queue
::getTableName();
366 $mailing = CRM_Mailing_BAO_Mailing
::getTableName();
367 $job = CRM_Mailing_BAO_MailingJob
::getTableName();
368 $contact = CRM_Contact_BAO_Contact
::getTableName();
369 $email = CRM_Core_BAO_Email
::getTableName();
372 SELECT $contact.display_name as display_name,
373 $contact.id as contact_id,
374 $email.email as email,
375 $reply.time_stamp as date
378 ON $queue.contact_id = $contact.id
380 ON $queue.email_id = $email.id
382 ON $reply.event_queue_id = $queue.id
384 ON $queue.job_id = $job.id
386 ON $job.mailing_id = $mailing.id
388 WHERE $mailing.id = " . CRM_Utils_Type
::escape($mailing_id, 'Integer');
390 if (!empty($job_id)) {
391 $query .= " AND $job.id = " . CRM_Utils_Type
::escape($job_id, 'Integer');
395 $query .= " GROUP BY $queue.id ";
398 $orderBy = "sort_name ASC, {$reply}.time_stamp DESC";
400 if (is_string($sort)) {
401 $sort = CRM_Utils_Type
::escape($sort, 'String');
405 $orderBy = trim($sort->orderBy());
409 $query .= " ORDER BY {$orderBy} ";
411 if ($offset ||
$rowCount) {
412 //Added "||$rowCount" to avoid displaying all records on first page
413 $query .= ' LIMIT ' . CRM_Utils_Type
::escape($offset, 'Integer') . ', ' . CRM_Utils_Type
::escape($rowCount, 'Integer');
420 while ($dao->fetch()) {
421 $url = CRM_Utils_System
::url('civicrm/contact/view',
422 "reset=1&cid={$dao->contact_id}"
425 'name' => "<a href=\"$url\">{$dao->display_name}</a>",
426 'email' => $dao->email
,
427 'date' => CRM_Utils_Date
::customFormat($dao->date
),