3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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-2019
34 require_once 'Mail/mime.php';
37 * Class CRM_Mailing_Event_BAO_Reply
39 class CRM_Mailing_Event_BAO_Reply
extends CRM_Mailing_Event_DAO_Reply
{
44 public function __construct() {
45 parent
::__construct();
49 * Register a reply event.
52 * The job ID of the reply.
53 * @param int $queue_id
58 * @param null $replyto
61 * The mailing object, or null on failure
63 public static function &reply($job_id, $queue_id, $hash, $replyto = NULL) {
64 // 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
103 * Queue event ID of the sender.
104 * @param string $mailing
105 * The mailing object.
106 * @param string $bodyTxt
107 * Text part of the body (ignored if $fullEmail provided).
108 * @param string $replyto
109 * Reply-to of the incoming message.
110 * @param string $bodyHTML
111 * HTML part of the body (ignored if $fullEmail provided).
112 * @param string $fullEmail
113 * Whole email to forward in one string.
115 public static function send($queue_id, &$mailing, &$bodyTxt, $replyto, &$bodyHTML = NULL, &$fullEmail = NULL) {
116 $domain = CRM_Core_BAO_Domain
::getDomain();
117 $emails = CRM_Core_BAO_Email
::getTableName();
118 $queue = CRM_Mailing_Event_BAO_Queue
::getTableName();
119 $contacts = CRM_Contact_BAO_Contact
::getTableName();
120 $domain_id = CRM_Core_Config
::domainID();
121 $domainValues = civicrm_api3('Domain', 'get', ['sequential' => 1, 'id' => $domain_id]);
122 $fromEmail = CRM_Core_BAO_Domain
::getNoReplyEmailAddress();
124 $eq = new CRM_Core_DAO();
125 $eq->query("SELECT $contacts.display_name as display_name,
126 $emails.email as email,
127 $queue.job_id as job_id,
131 ON $queue.contact_id = $contacts.id
133 ON $queue.email_id = $emails.id
134 WHERE $queue.id = " . CRM_Utils_Type
::escape($queue_id, 'Integer')
139 // parse the email and set a new destination
140 $parser = new ezcMailParser();
141 $set = new ezcMailVariableSet($fullEmail);
142 $parsed = array_shift($parser->parseMail($set));
143 $parsed->to
= [new ezcMailAddress($mailing->replyto_email
)];
145 // CRM-5567: we need to set Reply-To: so that any response
146 // to the forward goes to the sender of the reply
147 $parsed->setHeader('Reply-To', $replyto instanceof ezcMailAddress ?
$replyto : $parsed->from
->__toString());
149 // Using the original from address may not be permitted by the mailer.
150 $fromName = empty($parsed->from
->name
) ?
$parsed->from
->email
: "{$parsed->from->name} ({$parsed->from->email})";
151 $parsed->from
= new ezcMailAddress($fromEmail, $fromName);
153 // CRM-17754 Include re-sent headers to indicate that we have forwarded on the email
154 $domainEmail = $domainValues['values'][0]['from_email'];
155 $parsed->setHeader('Resent-From', $domainEmail);
156 $parsed->setHeader('Resent-Date', date('r'));
157 // Rewrite any invalid Return-Path headers.
158 $parsed->setHeader('Return-Path', $fromEmail);
160 // $h must be an array, so we can't use generateHeaders()'s result,
161 // but we have to regenerate the headers because we changed To
162 $parsed->generateHeaders();
163 $h = $parsed->headers
->getCaseSensitiveArray();
164 $b = $parsed->generateBody();
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 $fromName = empty($eq->display_name
) ?
$eq->email
: "{$eq->display_name} ({$eq->email})";
180 $message = new Mail_mime("\n");
183 'Subject' => "Re: {$mailing->subject}",
184 'To' => $mailing->replyto_email
,
185 'From' => "\"$fromName\" <$fromEmail>",
186 'Reply-To' => empty($replyto) ?
$eq->email
: $replyto,
187 'Return-Path' => CRM_Core_BAO_Domain
::getNoReplyEmailAddress(),
188 // CRM-17754 Include re-sent headers to indicate that we have forwarded on the email
189 'Resent-From' => $domainValues['values'][0]['from_email'],
190 'Resent-Date' => date('r'),
193 $message->setTxtBody($bodyTxt);
194 $message->setHTMLBody($bodyHTML);
195 $b = CRM_Utils_Mail
::setMimeParams($message);
196 $h = $message->headers($headers);
199 CRM_Mailing_BAO_Mailing
::addMessageIdHeader($h, 'r', $eq->job_id
, $queue_id, $eq->hash
);
200 $config = CRM_Core_Config
::singleton();
201 $mailer = \Civi
::service('pear_mail');
203 if (is_object($mailer)) {
204 $errorScope = CRM_Core_TemporaryErrorScope
::ignoreException();
205 $mailer->send($mailing->replyto_email
, $h, $b);
211 * Send an automated response.
213 * @param object $mailing
214 * The mailing object.
215 * @param int $queue_id
217 * @param string $replyto
218 * Optional reply-to from the reply.
220 private static function autoRespond(&$mailing, $queue_id, $replyto) {
221 $config = CRM_Core_Config
::singleton();
223 $contacts = CRM_Contact_DAO_Contact
::getTableName();
224 $email = CRM_Core_DAO_Email
::getTableName();
225 $queue = CRM_Mailing_Event_DAO_Queue
::getTableName();
227 $eq = new CRM_Core_DAO();
229 "SELECT $contacts.preferred_mail_format as format,
230 $email.email as email,
231 $queue.job_id as job_id,
234 INNER JOIN $queue ON $queue.contact_id = $contacts.id
235 INNER JOIN $email ON $queue.email_id = $email.id
236 WHERE $queue.id = " . CRM_Utils_Type
::escape($queue_id, 'Integer')
240 $to = empty($replyto) ?
$eq->email
: $replyto;
242 $component = new CRM_Mailing_BAO_MailingComponent();
243 $component->id
= $mailing->reply_id
;
244 $component->find(TRUE);
246 $message = new Mail_Mime("\n");
248 $domain = CRM_Core_BAO_Domain
::getDomain();
249 list($domainEmailName, $_) = CRM_Core_BAO_Domain
::getNameAndEmail();
252 'Subject' => $component->subject
,
254 'From' => "\"$domainEmailName\" <" . CRM_Core_BAO_Domain
::getNoReplyEmailAddress() . '>',
255 'Reply-To' => CRM_Core_BAO_Domain
::getNoReplyEmailAddress(),
256 'Return-Path' => CRM_Core_BAO_Domain
::getNoReplyEmailAddress(),
259 // TODO: do we need reply tokens?
260 $html = $component->body_html
;
261 if ($component->body_text
) {
262 $text = $component->body_text
;
265 $text = CRM_Utils_String
::htmlToText($component->body_html
);
268 $bao = new CRM_Mailing_BAO_Mailing();
269 $bao->body_text
= $text;
270 $bao->body_html
= $html;
271 $tokens = $bao->getTokens();
273 if ($eq->format
== 'HTML' ||
$eq->format
== 'Both') {
274 $html = CRM_Utils_Token
::replaceDomainTokens($html, $domain, TRUE, $tokens['html']);
275 $html = CRM_Utils_Token
::replaceMailingTokens($html, $mailing, NULL, $tokens['html']);
276 $message->setHTMLBody($html);
278 if (!$html ||
$eq->format
== 'Text' ||
$eq->format
== 'Both') {
279 $text = CRM_Utils_Token
::replaceDomainTokens($text, $domain, FALSE, $tokens['text']);
280 $text = CRM_Utils_Token
::replaceMailingTokens($text, $mailing, NULL, $tokens['text']);
281 $message->setTxtBody($text);
284 $b = CRM_Utils_Mail
::setMimeParams($message);
285 $h = $message->headers($headers);
286 CRM_Mailing_BAO_Mailing
::addMessageIdHeader($h, 'a', $eq->job_id
, queue_id
, $eq->hash
);
288 $mailer = \Civi
::service('pear_mail');
289 if (is_object($mailer)) {
290 $errorScope = CRM_Core_TemporaryErrorScope
::ignoreException();
291 $mailer->send($to, $h, $b);
297 * Get row count for the event selector.
299 * @param int $mailing_id
302 * Optional ID of a job to filter on.
303 * @param bool $is_distinct
304 * Group by queue ID?.
307 * Number of rows in result set
309 public static function getTotalCount(
310 $mailing_id, $job_id = NULL,
313 $dao = new CRM_Core_DAO();
315 $reply = self
::getTableName();
316 $queue = CRM_Mailing_Event_BAO_Queue
::getTableName();
317 $mailing = CRM_Mailing_BAO_Mailing
::getTableName();
318 $job = CRM_Mailing_BAO_MailingJob
::getTableName();
321 SELECT COUNT($reply.id) as reply
324 ON $reply.event_queue_id = $queue.id
326 ON $queue.job_id = $job.id
328 ON $job.mailing_id = $mailing.id
330 WHERE $mailing.id = " . CRM_Utils_Type
::escape($mailing_id, 'Integer');
332 if (!empty($job_id)) {
333 $query .= " AND $job.id = " . CRM_Utils_Type
::escape($job_id, 'Integer');
337 $query .= " GROUP BY $queue.id ";
351 * Get rows for the event browser.
353 * @param int $mailing_id
356 * Optional ID of the job.
357 * @param bool $is_distinct
358 * Group by queue id?.
361 * @param int $rowCount
369 public static function &getRows(
370 $mailing_id, $job_id = NULL,
371 $is_distinct = FALSE, $offset = NULL, $rowCount = NULL, $sort = NULL
374 $dao = new CRM_Core_DAO();
376 $reply = self
::getTableName();
377 $queue = CRM_Mailing_Event_BAO_Queue
::getTableName();
378 $mailing = CRM_Mailing_BAO_Mailing
::getTableName();
379 $job = CRM_Mailing_BAO_MailingJob
::getTableName();
380 $contact = CRM_Contact_BAO_Contact
::getTableName();
381 $email = CRM_Core_BAO_Email
::getTableName();
384 SELECT $contact.display_name as display_name,
385 $contact.id as contact_id,
386 $email.email as email,
387 $reply.time_stamp as date
390 ON $queue.contact_id = $contact.id
392 ON $queue.email_id = $email.id
394 ON $reply.event_queue_id = $queue.id
396 ON $queue.job_id = $job.id
398 ON $job.mailing_id = $mailing.id
400 WHERE $mailing.id = " . CRM_Utils_Type
::escape($mailing_id, 'Integer');
402 if (!empty($job_id)) {
403 $query .= " AND $job.id = " . CRM_Utils_Type
::escape($job_id, 'Integer');
407 $query .= " GROUP BY $queue.id, $contact.id, $reply.time_stamp ";
410 $orderBy = "sort_name ASC, {$reply}.time_stamp DESC";
412 if (is_string($sort)) {
413 $sort = CRM_Utils_Type
::escape($sort, 'String');
417 $orderBy = trim($sort->orderBy());
421 $query .= " ORDER BY {$orderBy} ";
423 if ($offset ||
$rowCount) {
424 //Added "||$rowCount" to avoid displaying all records on first page
425 $query .= ' LIMIT ' . CRM_Utils_Type
::escape($offset, 'Integer') . ', ' . CRM_Utils_Type
::escape($rowCount, 'Integer');
432 while ($dao->fetch()) {
433 $url = CRM_Utils_System
::url('civicrm/contact/view',
434 "reset=1&cid={$dao->contact_id}"
437 'name' => "<a href=\"$url\">{$dao->display_name}</a>",
438 'email' => $dao->email
,
439 'date' => CRM_Utils_Date
::customFormat($dao->date
),