3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class provides the common functionality for sending email to
20 * one or a group of contact ids. This class is reused by all the search
21 * components in CiviCRM (since they all have send email as a task)
23 class CRM_Contact_Form_Task_EmailCommon
{
24 const MAX_EMAILS_KILL_SWITCH
= 50;
26 public $_contactDetails = array();
27 public $_allContactDetails = array();
28 public $_toContactEmails = array();
31 * @deprecated Generate an array of Domain email addresses.
32 * @return array $domainEmails;
34 public static function domainEmails() {
35 CRM_Core_Error
::deprecatedFunctionWarning('CRM_Core_BAO_Email::domainEmails()');
36 return CRM_Core_BAO_Email
::domainEmails();
40 * Pre Process Form Addresses to be used in Quickform
41 * @param CRM_Core_Form $form
42 * @param bool $bounce determine if we want to throw a status bounce.
44 public static function preProcessFromAddress(&$form, $bounce = TRUE) {
45 $form->_single
= FALSE;
46 $className = CRM_Utils_System
::getClassName($form);
47 if (property_exists($form, '_context') &&
48 $form->_context
!= 'search' &&
49 $className == 'CRM_Contact_Form_Task_Email'
51 $form->_single
= TRUE;
54 $form->_emails
= array();
56 // @TODO remove these line and to it somewhere more appropriate. Currently some classes (e.g Case
57 // are having to re-write contactIds afterwards due to this inappropriate variable setting
58 // If we don't have any contact IDs, use the logged in contact ID
59 $form->_contactIds
= $form->_contactIds ?
: [CRM_Core_Session
::getLoggedInContactID()];
61 $fromEmailValues = CRM_Core_BAO_Email
::getFromEmail();
63 $form->_noEmails
= FALSE;
64 if (empty($fromEmailValues)) {
65 $form->_noEmails
= TRUE;
67 $form->assign('noEmails', $form->_noEmails
);
70 if ($form->_noEmails
) {
71 CRM_Core_Error
::statusBounce(ts('Your user record does not have a valid email address and no from addresses have been configured.'));
75 $form->_emails
= $fromEmailValues;
77 $form->_fromEmails
= $fromEmailValues;
78 if (!Civi
::settings()->get('allow_mail_from_logged_in_contact')) {
79 $defaults['from_email_address'] = current(CRM_Core_BAO_Domain
::getNameAndEmail(FALSE, TRUE));
81 if (is_numeric(key($form->_fromEmails
))) {
83 $defaultEmail = civicrm_api3('email', 'getsingle', array('id' => key($form->_fromEmails
)));
85 if (!empty($defaultEmail['signature_html'])) {
86 $defaults['html_message'] = '<br/><br/>--' . $defaultEmail['signature_html'];
88 if (!empty($defaultEmail['signature_text'])) {
89 $defaults['text_message'] = "\n\n--\n" . $defaultEmail['signature_text'];
92 $form->setDefaults($defaults);
96 * Build the form object.
98 * @param CRM_Core_Form $form
100 public static function buildQuickForm(&$form) {
101 $toArray = $ccArray = $bccArray = array();
102 $suppressedEmails = 0;
103 //here we are getting logged in user id as array but we need target contact id. CRM-5988
104 $cid = $form->get('cid');
106 $form->_contactIds
= explode(',', $cid);
108 if (count($form->_contactIds
) > 1) {
109 $form->_single
= FALSE;
111 CRM_Contact_Form_Task_EmailCommon
::bounceIfSimpleMailLimitExceeded(count($form->_contactIds
));
113 $emailAttributes = array(
116 $to = $form->add('text', 'to', ts('To'), $emailAttributes, TRUE);
117 $cc = $form->add('text', 'cc_id', ts('CC'), $emailAttributes);
118 $bcc = $form->add('text', 'bcc_id', ts('BCC'), $emailAttributes);
121 if (property_exists($form, '_context') && $form->_context
== 'standalone') {
122 $setDefaults = FALSE;
125 $elements = array('to', 'cc', 'bcc');
126 $form->_allContactIds
= $form->_toContactIds
= $form->_contactIds
;
127 foreach ($elements as $element) {
128 if ($
$element->getValue()) {
129 $allEmails = explode(',', $
$element->getValue());
130 if ($element == 'to') {
131 $form->_toContactIds
= $form->_contactIds
= array();
134 foreach ($allEmails as $value) {
135 list($contactId, $email) = explode('::', $value);
139 $form->_contactIds
[] = $form->_toContactIds
[] = $contactId;
140 $form->_toContactEmails
[] = $email;
144 $form->_ccContactIds
[] = $contactId;
148 $form->_bccContactIds
[] = $contactId;
152 $form->_allContactIds
[] = $contactId;
160 //get the group of contacts as per selected by user in case of Find Activities
161 if (!empty($form->_activityHolderIds
)) {
162 $contact = $form->get('contacts');
163 $form->_allContactIds
= $form->_contactIds
= $contact;
166 // check if we need to setdefaults and check for valid contact emails / communication preferences
167 if (is_array($form->_allContactIds
) && $setDefaults) {
168 $returnProperties = array(
175 'preferred_mail_format' => 1,
178 // get the details for all selected contacts ( to, cc and bcc contacts )
179 list($form->_contactDetails
) = CRM_Utils_Token
::getTokenDetails($form->_allContactIds
,
185 // make a copy of all contact details
186 $form->_allContactDetails
= $form->_contactDetails
;
188 // perform all validations on unique contact Ids
189 foreach (array_unique($form->_allContactIds
) as $key => $contactId) {
190 $value = $form->_contactDetails
[$contactId];
191 if ($value['do_not_email'] ||
empty($value['email']) ||
!empty($value['is_deceased']) ||
$value['on_hold']) {
194 // unset contact details for contacts that we won't be sending email. This is prevent extra computation
195 // during token evaluation etc.
196 unset($form->_contactDetails
[$contactId]);
199 $email = $value['email'];
201 // build array's which are used to setdefaults
202 if (in_array($contactId, $form->_toContactIds
)) {
203 $form->_toContactDetails
[$contactId] = $form->_contactDetails
[$contactId];
204 // If a particular address has been specified as the default, use that instead of contact's primary email
205 if (!empty($form->_toEmail
) && $form->_toEmail
['contact_id'] == $contactId) {
206 $email = $form->_toEmail
['email'];
209 'text' => '"' . $value['sort_name'] . '" <' . $email . '>',
210 'id' => "$contactId::{$email}",
213 elseif (in_array($contactId, $form->_ccContactIds
)) {
215 'text' => '"' . $value['sort_name'] . '" <' . $email . '>',
216 'id' => "$contactId::{$email}",
219 elseif (in_array($contactId, $form->_bccContactIds
)) {
221 'text' => '"' . $value['sort_name'] . '" <' . $email . '>',
222 'id' => "$contactId::{$email}",
228 if (empty($toArray)) {
229 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.'));
233 $form->assign('toContact', json_encode($toArray));
234 $form->assign('ccContact', json_encode($ccArray));
235 $form->assign('bccContact', json_encode($bccArray));
237 $form->assign('suppressedEmails', $suppressedEmails);
239 $form->assign('totalSelectedContacts', count($form->_contactIds
));
241 $form->add('text', 'subject', ts('Subject'), 'size=50 maxlength=254', TRUE);
243 $form->add('select', 'from_email_address', ts('From'), $form->_fromEmails
, TRUE);
245 CRM_Mailing_BAO_Mailing
::commonCompose($form);
248 CRM_Core_BAO_File
::buildAttachment($form, NULL);
250 if ($form->_single
) {
251 // also fix the user context stack
252 if ($form->_caseId
) {
253 $ccid = CRM_Core_DAO
::getFieldValue('CRM_Case_DAO_CaseContact', $form->_caseId
,
254 'contact_id', 'case_id'
256 $url = CRM_Utils_System
::url('civicrm/contact/view/case',
257 "&reset=1&action=view&cid={$ccid}&id={$form->_caseId}"
260 elseif ($form->_context
) {
261 $url = CRM_Utils_System
::url('civicrm/dashboard', 'reset=1');
264 $url = CRM_Utils_System
::url('civicrm/contact/view',
265 "&show=1&action=browse&cid={$form->_contactIds[0]}&selectedChild=activity"
269 $session = CRM_Core_Session
::singleton();
270 $session->replaceUserContext($url);
271 $form->addDefaultButtons(ts('Send Email'), 'upload', 'cancel');
274 $form->addDefaultButtons(ts('Send Email'), 'upload');
278 'followup_assignee_contact_id' => array(
279 'type' => 'entityRef',
280 'label' => ts('Assigned to'),
281 'attributes' => array(
284 'api' => array('params' => array('is_deceased' => 0)),
287 'followup_activity_type_id' => array(
289 'label' => ts('Followup Activity'),
290 'attributes' => array('' => '- ' . ts('select activity') . ' -') + CRM_Core_PseudoConstant
::ActivityType(FALSE),
291 'extra' => array('class' => 'crm-select2'),
293 'followup_activity_subject' => array(
295 'label' => ts('Subject'),
296 'attributes' => CRM_Core_DAO
::getAttribute('CRM_Activity_DAO_Activity',
303 $form->add('datepicker', 'followup_date', ts('in'));
305 foreach ($fields as $field => $values) {
306 if (!empty($fields[$field])) {
307 $attribute = $values['attributes'] ??
NULL;
308 $required = !empty($values['required']);
310 if ($values['type'] == 'select' && empty($attribute)) {
311 $form->addSelect($field, array('entity' => 'activity'), $required);
313 elseif ($values['type'] == 'entityRef') {
314 $form->addEntityRef($field, $values['label'], $attribute, $required);
317 $form->add($values['type'], $field, $values['label'], $attribute, $required, CRM_Utils_Array
::value('extra', $values));
322 //Added for CRM-15984: Add campaign field
323 CRM_Campaign_BAO_Campaign
::addCampaign($form);
325 $form->addFormRule(array('CRM_Contact_Form_Task_EmailCommon', 'formRule'), $form);
326 CRM_Core_Resources
::singleton()->addScriptFile('civicrm', 'templates/CRM/Contact/Form/Task/EmailCommon.js', 0, 'html-header');
332 * @param array $fields
333 * The input form values.
334 * @param array $dontCare
336 * Additional values form 'this'.
339 * true if no errors, else array of errors
341 public static function formRule($fields, $dontCare, $self) {
343 $template = CRM_Core_Smarty
::singleton();
345 if (isset($fields['html_message'])) {
346 $htmlMessage = str_replace(array("\n", "\r"), ' ', $fields['html_message']);
347 $htmlMessage = str_replace('"', '\"', $htmlMessage);
348 $template->assign('htmlContent', $htmlMessage);
352 if (!empty($fields['saveTemplate']) && empty($fields['saveTemplateName'])) {
353 $errors['saveTemplateName'] = ts("Enter name to save message template");
356 return empty($errors) ?
TRUE : $errors;
360 * Process the form after the input has been submitted and validated.
362 * @param CRM_Core_Form $form
364 public static function postProcess(&$form) {
365 self
::bounceIfSimpleMailLimitExceeded(count($form->_contactIds
));
367 // check and ensure that
368 $formValues = $form->controller
->exportValues($form->getName());
369 self
::submit($form, $formValues);
373 * Submit the form values.
375 * This is also accessible for testing.
377 * @param CRM_Core_Form $form
378 * @param array $formValues
380 public static function submit(&$form, $formValues) {
381 self
::saveMessageTemplate($formValues);
383 $from = $formValues['from_email_address'] ??
NULL;
384 // dev/core#357 User Emails are keyed by their id so that the Signature is able to be added
385 // If we have had a contact email used here the value returned from the line above will be the
386 // numerical key where as $from for use in the sendEmail in Activity needs to be of format of "To Name" <toemailaddress>
387 $from = CRM_Utils_Mail
::formatFromAddress($from);
388 $subject = $formValues['subject'];
390 // CRM-13378: Append CC and BCC information at the end of Activity Details and format cc and bcc fields
391 $elements = array('cc_id', 'bcc_id');
392 $additionalDetails = NULL;
393 $ccValues = $bccValues = array();
394 foreach ($elements as $element) {
395 if (!empty($formValues[$element])) {
396 $allEmails = explode(',', $formValues[$element]);
397 foreach ($allEmails as $value) {
398 list($contactId, $email) = explode('::', $value);
399 $contactURL = CRM_Utils_System
::url('civicrm/contact/view', "reset=1&force=1&cid={$contactId}", TRUE);
402 $ccValues['email'][] = '"' . $form->_contactDetails
[$contactId]['sort_name'] . '" <' . $email . '>';
403 $ccValues['details'][] = "<a href='{$contactURL}'>" . $form->_contactDetails
[$contactId]['display_name'] . "</a>";
407 $bccValues['email'][] = '"' . $form->_contactDetails
[$contactId]['sort_name'] . '" <' . $email . '>';
408 $bccValues['details'][] = "<a href='{$contactURL}'>" . $form->_contactDetails
[$contactId]['display_name'] . "</a>";
416 if (!empty($ccValues)) {
417 $cc = implode(',', $ccValues['email']);
418 $additionalDetails .= "\ncc : " . implode(", ", $ccValues['details']);
420 if (!empty($bccValues)) {
421 $bcc = implode(',', $bccValues['email']);
422 $additionalDetails .= "\nbcc : " . implode(", ", $bccValues['details']);
425 // CRM-5916: prepend case id hash to CiviCase-originating emails’ subjects
426 if (isset($form->_caseId
) && is_numeric($form->_caseId
)) {
427 $hash = substr(sha1(CIVICRM_SITE_KEY
. $form->_caseId
), 0, 7);
428 $subject = "[case #$hash] $subject";
431 $attachments = array();
432 CRM_Core_BAO_File
::formatAttachment($formValues,
437 // format contact details array to handle multiple emails from same contact
438 $formattedContactDetails = array();
439 $tempEmails = array();
440 foreach ($form->_contactIds
as $key => $contactId) {
441 // if we dont have details on this contactID, we should ignore
442 // potentially this is due to the contact not wanting to receive email
443 if (!isset($form->_contactDetails
[$contactId])) {
446 $email = $form->_toContactEmails
[$key];
447 // prevent duplicate emails if same email address is selected CRM-4067
448 // we should allow same emails for different contacts
449 $emailKey = "{$contactId}::{$email}";
450 if (!in_array($emailKey, $tempEmails)) {
451 $tempEmails[] = $emailKey;
452 $details = $form->_contactDetails
[$contactId];
453 $details['email'] = $email;
454 unset($details['email_id']);
455 $formattedContactDetails[] = $details;
459 $contributionIds = array();
460 if ($form->getVar('_contributionIds')) {
461 $contributionIds = $form->getVar('_contributionIds');
465 list($sent, $activityId) = CRM_Activity_BAO_Activity
::sendEmail(
466 $formattedContactDetails,
468 $formValues['text_message'],
469 $formValues['html_message'],
476 array_keys($form->_toContactDetails
),
479 CRM_Utils_Array
::value('campaign_id', $formValues),
480 $form->getVar('_caseId')
483 $followupStatus = '';
485 $followupActivity = NULL;
486 if (!empty($formValues['followup_activity_type_id'])) {
487 $params['followup_activity_type_id'] = $formValues['followup_activity_type_id'];
488 $params['followup_activity_subject'] = $formValues['followup_activity_subject'];
489 $params['followup_date'] = $formValues['followup_date'];
490 $params['target_contact_id'] = $form->_contactIds
;
491 $params['followup_assignee_contact_id'] = explode(',', $formValues['followup_assignee_contact_id']);
492 $followupActivity = CRM_Activity_BAO_Activity
::createFollowupActivity($activityId, $params);
493 $followupStatus = ts('A followup activity has been scheduled.');
495 if (Civi
::settings()->get('activity_assignee_notification')) {
496 if ($followupActivity) {
497 $mailToFollowupContacts = array();
498 $assignee = array($followupActivity->id
);
499 $assigneeContacts = CRM_Activity_BAO_ActivityAssignment
::getAssigneeNames($assignee, TRUE, FALSE);
500 foreach ($assigneeContacts as $values) {
501 $mailToFollowupContacts[$values['email']] = $values;
504 $sentFollowup = CRM_Activity_BAO_Activity
::sendToAssignee($followupActivity, $mailToFollowupContacts);
506 $followupStatus .= '<br />' . ts("A copy of the follow-up activity has also been sent to follow-up assignee contacts(s).");
512 $count_success = count($form->_toContactDetails
);
513 CRM_Core_Session
::setStatus(ts('One message was sent successfully. ', array(
514 'plural' => '%count messages were sent successfully. ',
515 'count' => $count_success,
516 )) . $followupStatus, ts('Message Sent', array('plural' => 'Messages Sent', 'count' => $count_success)), 'success');
519 // Display the name and number of contacts for those email is not sent.
520 // php 5.4 throws out a notice since the values of these below arrays are arrays.
521 // the behavior is not documented in the php manual, but it does the right thing
522 // suppressing the notices to get things in good shape going forward
523 $emailsNotSent = @array_diff_assoc
($form->_allContactDetails
, $form->_contactDetails
);
525 if ($emailsNotSent) {
527 foreach ($emailsNotSent as $contactId => $values) {
528 $displayName = $values['display_name'];
529 $email = $values['email'];
530 $contactViewUrl = CRM_Utils_System
::url('civicrm/contact/view', "reset=1&cid=$contactId");
531 $not_sent[] = "<a href='$contactViewUrl' title='$email'>$displayName</a>" . ($values['on_hold'] ?
'(' . ts('on hold') . ')' : '');
533 $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>';
534 CRM_Core_Session
::setStatus($status, ts('One Message Not Sent', array(
535 'count' => count($emailsNotSent),
536 'plural' => '%count Messages Not Sent',
540 if (isset($form->_caseId
)) {
541 // if case-id is found in the url, create case activity record
542 $cases = explode(',', $form->_caseId
);
543 foreach ($cases as $key => $val) {
544 if (is_numeric($val)) {
546 'activity_id' => $activityId,
549 CRM_Case_BAO_Case
::processCaseActivity($caseParams);
556 * Save the template if update selected.
558 * @param array $formValues
560 protected static function saveMessageTemplate($formValues) {
561 if (!empty($formValues['saveTemplate']) ||
!empty($formValues['updateTemplate'])) {
562 $messageTemplate = array(
563 'msg_text' => $formValues['text_message'],
564 'msg_html' => $formValues['html_message'],
565 'msg_subject' => $formValues['subject'],
569 if (!empty($formValues['saveTemplate'])) {
570 $messageTemplate['msg_title'] = $formValues['saveTemplateName'];
571 CRM_Core_BAO_MessageTemplate
::add($messageTemplate);
574 if (!empty($formValues['template']) && !empty($formValues['updateTemplate'])) {
575 $messageTemplate['id'] = $formValues['template'];
576 unset($messageTemplate['msg_title']);
577 CRM_Core_BAO_MessageTemplate
::add($messageTemplate);
583 * Bounce if there are more emails than permitted.
586 * The number of emails the user is attempting to send
588 public static function bounceIfSimpleMailLimitExceeded($count) {
589 $limit = Civi
::settings()->get('simple_mail_limit');
590 if ($count > $limit) {
591 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.',