3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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-2018
35 * This class provides the common functionality for sending email to
36 * one or a group of contact ids. This class is reused by all the search
37 * components in CiviCRM (since they all have send email as a task)
39 class CRM_Contact_Form_Task_EmailCommon
{
40 const MAX_EMAILS_KILL_SWITCH
= 50;
42 public $_contactDetails = array();
43 public $_allContactDetails = array();
44 public $_toContactEmails = array();
47 * @deprecated Generate an array of Domain email addresses.
48 * @return array $domainEmails;
50 public static function domainEmails() {
51 Civi
::log()->warning('Deprecated function, use CRM_Core_BAO_Email::domainEmails()', array('civi.tag' => 'deprecated'));
52 return CRM_Core_BAO_Email
::domainEmails();
56 * Pre Process Form Addresses to be used in Quickform
57 * @param CRM_Core_Form $form
58 * @param bool $bounce determine if we want to throw a status bounce.
60 public static function preProcessFromAddress(&$form, $bounce = TRUE) {
61 $form->_single
= FALSE;
62 $className = CRM_Utils_System
::getClassName($form);
63 if (property_exists($form, '_context') &&
64 $form->_context
!= 'search' &&
65 $className == 'CRM_Contact_Form_Task_Email'
67 $form->_single
= TRUE;
70 $form->_emails
= array();
72 // @TODO remove these line and to it somewhere more appropriate. Currently some classes (e.g Case
73 // are having to re-write contactIds afterwards due to this inappropriate variable setting
74 // If we don't have any contact IDs, use the logged in contact ID
75 $form->_contactIds
= $form->_contactIds ?
: [CRM_Core_Session
::getLoggedInContactID()];
77 $fromEmailValues = CRM_Core_BAO_Email
::getFromEmail();
79 $form->_noEmails
= FALSE;
80 if (empty($fromEmailValues)) {
81 $form->_noEmails
= TRUE;
83 $form->assign('noEmails', $form->_noEmails
);
86 if ($form->_noEmails
) {
87 CRM_Core_Error
::statusBounce(ts('Your user record does not have a valid email address and no from addresses have been configured.'));
91 $form->_emails
= $fromEmailValues;
92 $form->_fromEmails
= $fromEmailValues;
93 if (is_numeric(key($form->_fromEmails
))) {
95 $defaultEmail = civicrm_api3('email', 'getsingle', array('id' => key($form->_fromEmails
)));
97 if (!empty($defaultEmail['signature_html'])) {
98 $defaults['html_message'] = '<br/><br/>--' . $defaultEmail['signature_html'];
100 if (!empty($defaultEmail['signature_text'])) {
101 $defaults['text_message'] = "\n\n--\n" . $defaultEmail['signature_text'];
103 $form->setDefaults($defaults);
108 * Build the form object.
110 * @param CRM_Core_Form $form
112 public static function buildQuickForm(&$form) {
113 $toArray = $ccArray = $bccArray = array();
114 $suppressedEmails = 0;
115 //here we are getting logged in user id as array but we need target contact id. CRM-5988
116 $cid = $form->get('cid');
118 $form->_contactIds
= explode(',', $cid);
120 if (count($form->_contactIds
) > 1) {
121 $form->_single
= FALSE;
124 $emailAttributes = array(
127 $to = $form->add('text', 'to', ts('To'), $emailAttributes, TRUE);
128 $cc = $form->add('text', 'cc_id', ts('CC'), $emailAttributes);
129 $bcc = $form->add('text', 'bcc_id', ts('BCC'), $emailAttributes);
132 if (property_exists($form, '_context') && $form->_context
== 'standalone') {
133 $setDefaults = FALSE;
136 $elements = array('to', 'cc', 'bcc');
137 $form->_allContactIds
= $form->_toContactIds
= $form->_contactIds
;
138 foreach ($elements as $element) {
139 if ($
$element->getValue()) {
140 $allEmails = explode(',', $
$element->getValue());
141 if ($element == 'to') {
142 $form->_toContactIds
= $form->_contactIds
= array();
145 foreach ($allEmails as $value) {
146 list($contactId, $email) = explode('::', $value);
150 $form->_contactIds
[] = $form->_toContactIds
[] = $contactId;
151 $form->_toContactEmails
[] = $email;
155 $form->_ccContactIds
[] = $contactId;
159 $form->_bccContactIds
[] = $contactId;
163 $form->_allContactIds
[] = $contactId;
171 //get the group of contacts as per selected by user in case of Find Activities
172 if (!empty($form->_activityHolderIds
)) {
173 $contact = $form->get('contacts');
174 $form->_allContactIds
= $form->_contactIds
= $contact;
177 // check if we need to setdefaults and check for valid contact emails / communication preferences
178 if (is_array($form->_allContactIds
) && $setDefaults) {
179 $returnProperties = array(
186 'preferred_mail_format' => 1,
189 // get the details for all selected contacts ( to, cc and bcc contacts )
190 list($form->_contactDetails
) = CRM_Utils_Token
::getTokenDetails($form->_allContactIds
,
196 // make a copy of all contact details
197 $form->_allContactDetails
= $form->_contactDetails
;
199 // perform all validations on unique contact Ids
200 foreach (array_unique($form->_allContactIds
) as $key => $contactId) {
201 $value = $form->_contactDetails
[$contactId];
202 if ($value['do_not_email'] ||
empty($value['email']) ||
!empty($value['is_deceased']) ||
$value['on_hold']) {
205 // unset contact details for contacts that we won't be sending email. This is prevent extra computation
206 // during token evaluation etc.
207 unset($form->_contactDetails
[$contactId]);
210 $email = $value['email'];
212 // build array's which are used to setdefaults
213 if (in_array($contactId, $form->_toContactIds
)) {
214 $form->_toContactDetails
[$contactId] = $form->_contactDetails
[$contactId];
215 // If a particular address has been specified as the default, use that instead of contact's primary email
216 if (!empty($form->_toEmail
) && $form->_toEmail
['contact_id'] == $contactId) {
217 $email = $form->_toEmail
['email'];
220 'text' => '"' . $value['sort_name'] . '" <' . $email . '>',
221 'id' => "$contactId::{$email}",
224 elseif (in_array($contactId, $form->_ccContactIds
)) {
226 'text' => '"' . $value['sort_name'] . '" <' . $email . '>',
227 'id' => "$contactId::{$email}",
230 elseif (in_array($contactId, $form->_bccContactIds
)) {
232 'text' => '"' . $value['sort_name'] . '" <' . $email . '>',
233 'id' => "$contactId::{$email}",
239 if (empty($toArray)) {
240 CRM_Core_Error
::statusBounce(ts('Selected contact(s) do not have a valid email address, or communication preferences specify DO NOT EMAIL, or they are deceased or Primary email address is On Hold.'));
244 $form->assign('toContact', json_encode($toArray));
245 $form->assign('ccContact', json_encode($ccArray));
246 $form->assign('bccContact', json_encode($bccArray));
248 $form->assign('suppressedEmails', $suppressedEmails);
250 $form->assign('totalSelectedContacts', count($form->_contactIds
));
252 $form->add('text', 'subject', ts('Subject'), 'size=50 maxlength=254', TRUE);
254 $form->add('select', 'from_email_address', ts('From'), $form->_fromEmails
, TRUE);
256 CRM_Mailing_BAO_Mailing
::commonCompose($form);
259 CRM_Core_BAO_File
::buildAttachment($form, NULL);
261 if ($form->_single
) {
262 // also fix the user context stack
263 if ($form->_caseId
) {
264 $ccid = CRM_Core_DAO
::getFieldValue('CRM_Case_DAO_CaseContact', $form->_caseId
,
265 'contact_id', 'case_id'
267 $url = CRM_Utils_System
::url('civicrm/contact/view/case',
268 "&reset=1&action=view&cid={$ccid}&id={$form->_caseId}"
271 elseif ($form->_context
) {
272 $url = CRM_Utils_System
::url('civicrm/dashboard', 'reset=1');
275 $url = CRM_Utils_System
::url('civicrm/contact/view',
276 "&show=1&action=browse&cid={$form->_contactIds[0]}&selectedChild=activity"
280 $session = CRM_Core_Session
::singleton();
281 $session->replaceUserContext($url);
282 $form->addDefaultButtons(ts('Send Email'), 'upload', 'cancel');
285 $form->addDefaultButtons(ts('Send Email'), 'upload');
289 'followup_assignee_contact_id' => array(
290 'type' => 'entityRef',
291 'label' => ts('Assigned to'),
292 'attributes' => array(
295 'api' => array('params' => array('is_deceased' => 0)),
298 'followup_activity_type_id' => array(
300 'label' => ts('Followup Activity'),
301 'attributes' => array('' => '- ' . ts('select activity') . ' -') + CRM_Core_PseudoConstant
::ActivityType(FALSE),
302 'extra' => array('class' => 'crm-select2'),
304 'followup_activity_subject' => array(
306 'label' => ts('Subject'),
307 'attributes' => CRM_Core_DAO
::getAttribute('CRM_Activity_DAO_Activity',
314 $form->addDateTime('followup_date', ts('in'), FALSE, array('formatType' => 'activityDateTime'));
316 foreach ($fields as $field => $values) {
317 if (!empty($fields[$field])) {
318 $attribute = CRM_Utils_Array
::value('attributes', $values);
319 $required = !empty($values['required']);
321 if ($values['type'] == 'select' && empty($attribute)) {
322 $form->addSelect($field, array('entity' => 'activity'), $required);
324 elseif ($values['type'] == 'entityRef') {
325 $form->addEntityRef($field, $values['label'], $attribute, $required);
328 $form->add($values['type'], $field, $values['label'], $attribute, $required, CRM_Utils_Array
::value('extra', $values));
333 //Added for CRM-15984: Add campaign field
334 CRM_Campaign_BAO_Campaign
::addCampaign($form);
336 $form->addFormRule(array('CRM_Contact_Form_Task_EmailCommon', 'formRule'), $form);
337 CRM_Core_Resources
::singleton()->addScriptFile('civicrm', 'templates/CRM/Contact/Form/Task/EmailCommon.js', 0, 'html-header');
343 * @param array $fields
344 * The input form values.
345 * @param array $dontCare
347 * Additional values form 'this'.
350 * true if no errors, else array of errors
352 public static function formRule($fields, $dontCare, $self) {
354 $template = CRM_Core_Smarty
::singleton();
356 if (isset($fields['html_message'])) {
357 $htmlMessage = str_replace(array("\n", "\r"), ' ', $fields['html_message']);
358 $htmlMessage = str_replace('"', '\"', $htmlMessage);
359 $template->assign('htmlContent', $htmlMessage);
363 if (!empty($fields['saveTemplate']) && empty($fields['saveTemplateName'])) {
364 $errors['saveTemplateName'] = ts("Enter name to save message template");
367 return empty($errors) ?
TRUE : $errors;
371 * Process the form after the input has been submitted and validated.
373 * @param CRM_Core_Form $form
375 public static function postProcess(&$form) {
376 self
::bounceIfSimpleMailLimitExceeded(count($form->_contactIds
));
378 // check and ensure that
379 $formValues = $form->controller
->exportValues($form->getName());
380 self
::submit($form, $formValues);
384 * Submit the form values.
386 * This is also accessible for testing.
388 * @param CRM_Core_Form $form
389 * @param array $formValues
391 public static function submit(&$form, $formValues) {
392 self
::saveMessageTemplate($formValues);
394 $from = CRM_Utils_Array
::value('from_email_address', $formValues);
395 $subject = $formValues['subject'];
397 // CRM-13378: Append CC and BCC information at the end of Activity Details and format cc and bcc fields
398 $elements = array('cc_id', 'bcc_id');
399 $additionalDetails = NULL;
400 $ccValues = $bccValues = array();
401 foreach ($elements as $element) {
402 if (!empty($formValues[$element])) {
403 $allEmails = explode(',', $formValues[$element]);
404 foreach ($allEmails as $value) {
405 list($contactId, $email) = explode('::', $value);
406 $contactURL = CRM_Utils_System
::url('civicrm/contact/view', "reset=1&force=1&cid={$contactId}", TRUE);
409 $ccValues['email'][] = '"' . $form->_contactDetails
[$contactId]['sort_name'] . '" <' . $email . '>';
410 $ccValues['details'][] = "<a href='{$contactURL}'>" . $form->_contactDetails
[$contactId]['display_name'] . "</a>";
414 $bccValues['email'][] = '"' . $form->_contactDetails
[$contactId]['sort_name'] . '" <' . $email . '>';
415 $bccValues['details'][] = "<a href='{$contactURL}'>" . $form->_contactDetails
[$contactId]['display_name'] . "</a>";
423 if (!empty($ccValues)) {
424 $cc = implode(',', $ccValues['email']);
425 $additionalDetails .= "\ncc : " . implode(", ", $ccValues['details']);
427 if (!empty($bccValues)) {
428 $bcc = implode(',', $bccValues['email']);
429 $additionalDetails .= "\nbcc : " . implode(", ", $bccValues['details']);
432 // CRM-5916: prepend case id hash to CiviCase-originating emails’ subjects
433 if (isset($form->_caseId
) && is_numeric($form->_caseId
)) {
434 $hash = substr(sha1(CIVICRM_SITE_KEY
. $form->_caseId
), 0, 7);
435 $subject = "[case #$hash] $subject";
438 $attachments = array();
439 CRM_Core_BAO_File
::formatAttachment($formValues,
444 // format contact details array to handle multiple emails from same contact
445 $formattedContactDetails = array();
446 $tempEmails = array();
447 foreach ($form->_contactIds
as $key => $contactId) {
448 // if we dont have details on this contactID, we should ignore
449 // potentially this is due to the contact not wanting to receive email
450 if (!isset($form->_contactDetails
[$contactId])) {
453 $email = $form->_toContactEmails
[$key];
454 // prevent duplicate emails if same email address is selected CRM-4067
455 // we should allow same emails for different contacts
456 $emailKey = "{$contactId}::{$email}";
457 if (!in_array($emailKey, $tempEmails)) {
458 $tempEmails[] = $emailKey;
459 $details = $form->_contactDetails
[$contactId];
460 $details['email'] = $email;
461 unset($details['email_id']);
462 $formattedContactDetails[] = $details;
466 $contributionIds = array();
467 if ($form->getVar('_contributionIds')) {
468 $contributionIds = $form->getVar('_contributionIds');
472 list($sent, $activityId) = CRM_Activity_BAO_Activity
::sendEmail(
473 $formattedContactDetails,
475 $formValues['text_message'],
476 $formValues['html_message'],
483 array_keys($form->_toContactDetails
),
486 CRM_Utils_Array
::value('campaign_id', $formValues)
489 $followupStatus = '';
491 $followupActivity = NULL;
492 if (!empty($formValues['followup_activity_type_id'])) {
493 $params['followup_activity_type_id'] = $formValues['followup_activity_type_id'];
494 $params['followup_activity_subject'] = $formValues['followup_activity_subject'];
495 $params['followup_date'] = $formValues['followup_date'];
496 $params['followup_date_time'] = $formValues['followup_date_time'];
497 $params['target_contact_id'] = $form->_contactIds
;
498 $params['followup_assignee_contact_id'] = explode(',', $formValues['followup_assignee_contact_id']);
499 $followupActivity = CRM_Activity_BAO_Activity
::createFollowupActivity($activityId, $params);
500 $followupStatus = ts('A followup activity has been scheduled.');
502 if (Civi
::settings()->get('activity_assignee_notification')) {
503 if ($followupActivity) {
504 $mailToFollowupContacts = array();
505 $assignee = array($followupActivity->id
);
506 $assigneeContacts = CRM_Activity_BAO_ActivityAssignment
::getAssigneeNames($assignee, TRUE, FALSE);
507 foreach ($assigneeContacts as $values) {
508 $mailToFollowupContacts[$values['email']] = $values;
511 $sentFollowup = CRM_Activity_BAO_Activity
::sendToAssignee($followupActivity, $mailToFollowupContacts);
513 $followupStatus .= '<br />' . ts("A copy of the follow-up activity has also been sent to follow-up assignee contacts(s).");
519 $count_success = count($form->_toContactDetails
);
520 CRM_Core_Session
::setStatus(ts('One message was sent successfully. ', array(
521 'plural' => '%count messages were sent successfully. ',
522 'count' => $count_success,
523 )) . $followupStatus, ts('Message Sent', array('plural' => 'Messages Sent', 'count' => $count_success)), 'success');
526 // Display the name and number of contacts for those email is not sent.
527 // php 5.4 throws out a notice since the values of these below arrays are arrays.
528 // the behavior is not documented in the php manual, but it does the right thing
529 // suppressing the notices to get things in good shape going forward
530 $emailsNotSent = @array_diff_assoc
($form->_allContactDetails
, $form->_contactDetails
);
532 if ($emailsNotSent) {
534 foreach ($emailsNotSent as $contactId => $values) {
535 $displayName = $values['display_name'];
536 $email = $values['email'];
537 $contactViewUrl = CRM_Utils_System
::url('civicrm/contact/view', "reset=1&cid=$contactId");
538 $not_sent[] = "<a href='$contactViewUrl' title='$email'>$displayName</a>" . ($values['on_hold'] ?
'(' . ts('on hold') . ')' : '');
540 $status = '(' . ts('because no email address on file or communication preferences specify DO NOT EMAIL or Contact is deceased or Primary email address is On Hold') . ')<ul><li>' . implode('</li><li>', $not_sent) . '</li></ul>';
541 CRM_Core_Session
::setStatus($status, ts('One Message Not Sent', array(
542 'count' => count($emailsNotSent),
543 'plural' => '%count Messages Not Sent',
547 if (isset($form->_caseId
)) {
548 // if case-id is found in the url, create case activity record
549 $cases = explode(',', $form->_caseId
);
550 foreach ($cases as $key => $val) {
551 if (is_numeric($val)) {
553 'activity_id' => $activityId,
556 CRM_Case_BAO_Case
::processCaseActivity($caseParams);
563 * Save the template if update selected.
565 * @param array $formValues
567 protected static function saveMessageTemplate($formValues) {
568 if (!empty($formValues['saveTemplate']) ||
!empty($formValues['updateTemplate'])) {
569 $messageTemplate = array(
570 'msg_text' => $formValues['text_message'],
571 'msg_html' => $formValues['html_message'],
572 'msg_subject' => $formValues['subject'],
576 if (!empty($formValues['saveTemplate'])) {
577 $messageTemplate['msg_title'] = $formValues['saveTemplateName'];
578 CRM_Core_BAO_MessageTemplate
::add($messageTemplate);
581 if (!empty($formValues['template']) && !empty($formValues['updateTemplate'])) {
582 $messageTemplate['id'] = $formValues['template'];
583 unset($messageTemplate['msg_title']);
584 CRM_Core_BAO_MessageTemplate
::add($messageTemplate);
590 * Bounce if there are more emails than permitted.
593 * The number of emails the user is attempting to send
595 public static function bounceIfSimpleMailLimitExceeded($count) {
596 $limit = Civi
::settings()->get('simple_mail_limit');
597 if ($count > $limit) {
598 CRM_Core_Error
::statusBounce(ts('Please do not use this task to send a lot of emails (greater than %1). Many countries have legal requirements when sending bulk emails and the CiviMail framework has opt out functionality and domain tokens to help meet these.',