* @copyright CiviCRM LLC https://civicrm.org/licensing
*/
+use Civi\Api4\Email;
+
/**
* This class provides the common functionality for tasks that send emails.
*/
*/
public $isSearchContext = TRUE;
+ public $contactEmails = [];
+
/**
* Getter for isSearchContext.
*
$this->assign('suppressForm', FALSE);
$this->assign('emailTask', TRUE);
- $toArray = $ccArray = $bccArray = [];
+ $toArray = $ccArray = [];
$suppressedEmails = 0;
//here we are getting logged in user id as array but we need target contact id. CRM-5988
$cid = $this->get('cid');
];
$to = $this->add('text', 'to', ts('To'), $emailAttributes, TRUE);
$cc = $this->add('text', 'cc_id', ts('CC'), $emailAttributes);
- $bcc = $this->add('text', 'bcc_id', ts('BCC'), $emailAttributes);
+
+ $this->addEntityRef('bcc_id', ts('BCC'), [
+ 'entity' => 'Email',
+ 'multiple' => TRUE,
+ ]);
if ($to->getValue()) {
$this->_toContactIds = $this->_contactIds = [];
$setDefaults = FALSE;
}
- $elements = ['to', 'cc', 'bcc'];
+ $elements = ['to', 'cc'];
$this->_allContactIds = $this->_toContactIds = $this->_contactIds;
foreach ($elements as $element) {
if ($$element->getValue()) {
case 'cc':
$this->_ccContactIds[] = $contactId;
break;
-
- case 'bcc':
- $this->_bccContactIds[] = $contactId;
- break;
}
$this->_allContactIds[] = $contactId;
'id' => "$contactId::{$email}",
];
}
- elseif (in_array($contactId, $this->_bccContactIds)) {
- $bccArray[] = [
- 'text' => '"' . $value['sort_name'] . '" <' . $email . '>',
- 'id' => "$contactId::{$email}",
- ];
- }
}
}
$this->assign('toContact', json_encode($toArray));
$this->assign('ccContact', json_encode($ccArray));
- $this->assign('bccContact', json_encode($bccArray));
$this->assign('suppressedEmails', $suppressedEmails);
* @throws \CRM_Core_Exception
* @throws \CiviCRM_API3_Exception
* @throws \Civi\API\Exception\UnauthorizedException
+ * @throws \API_Exception
*/
public function submit($formValues) {
$this->saveMessageTemplate($formValues);
$subject = $formValues['subject'];
// CRM-13378: Append CC and BCC information at the end of Activity Details and format cc and bcc fields
- $elements = ['cc_id', 'bcc_id'];
+ $elements = ['cc_id'];
$additionalDetails = NULL;
- $ccValues = $bccValues = [];
+ $ccValues = [];
foreach ($elements as $element) {
if (!empty($formValues[$element])) {
$allEmails = explode(',', $formValues[$element]);
$ccValues['details'][] = "<a href='{$contactURL}'>" . $this->_contactDetails[$contactId]['display_name'] . "</a>";
break;
- case 'bcc_id':
- $bccValues['email'][] = '"' . $this->_contactDetails[$contactId]['sort_name'] . '" <' . $email . '>';
- $bccValues['details'][] = "<a href='{$contactURL}'>" . $this->_contactDetails[$contactId]['display_name'] . "</a>";
- break;
}
}
}
}
+ $cc = '';
- $cc = $bcc = '';
if (!empty($ccValues)) {
$cc = implode(',', $ccValues['email']);
$additionalDetails .= "\ncc : " . implode(", ", $ccValues['details']);
}
- if (!empty($bccValues)) {
- $bcc = implode(',', $bccValues['email']);
- $additionalDetails .= "\nbcc : " . implode(", ", $bccValues['details']);
- }
+ $bccArray = explode(',', $formValues['bcc_id'] ?? '');
+ $bcc = $this->getEmailString($bccArray);
+ $additionalDetails .= empty($bccArray) ? '' : "\nbcc : " . $this->getEmailUrlString($bccArray);
// CRM-5916: prepend case id hash to CiviCase-originating emails’ subjects
if (isset($this->_caseId) && is_numeric($this->_caseId)) {
return $return;
}
+ /**
+ * Get the string for the email IDs.
+ *
+ * @param array $emailIDs
+ * Array of email IDs.
+ *
+ * @return string
+ * e.g. "Smith, Bob<bob.smith@example.com>".
+ *
+ * @throws \API_Exception
+ * @throws \Civi\API\Exception\UnauthorizedException
+ */
+ protected function getEmailString(array $emailIDs): string {
+ $emails = Email::get()
+ ->addWhere('id', 'IN', $emailIDs)
+ ->setCheckPermissions(FALSE)
+ ->setSelect(['contact_id', 'email', 'contact.sort_name', 'contact.display_name'])->execute();
+ $emailStrings = [];
+ foreach ($emails as $email) {
+ $this->contactEmails[$email['id']] = $email;
+ $emailStrings[] = '"' . $email['contact.sort_name'] . '" <' . $email['email'] . '>';
+ }
+ return implode(',', $emailStrings);
+ }
+
+ /**
+ * Get the url string.
+ *
+ * This is called after the contacts have been retrieved so we don't need to re-retrieve.
+ *
+ * @param array $emailIDs
+ *
+ * @return string
+ * e.g. <a href='{$contactURL}'>Bob Smith</a>'
+ */
+ protected function getEmailUrlString(array $emailIDs): string {
+ $urlString = '';
+ foreach ($emailIDs as $email) {
+ $contactURL = CRM_Utils_System::url('civicrm/contact/view', ['reset' => 1, 'force' => 1, 'cid' => $this->contactEmails[$email]['contact_id']], TRUE);
+ $urlString .= "<a href='{$contactURL}'>" . $this->contactEmails[$email]['contact.display_name'] . '</a>';
+ }
+ return $urlString;
+ }
+
}
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
- /**
- * Test class for CRM_Contact_Form_Task_EmailCommon.
- * @group headless
- */
+
+use Civi\Api4\Activity;
+
+/**
+ * Test class for CRM_Contact_Form_Task_EmailCommon.
+ * @group headless
+ */
class CRM_Contact_Form_Task_EmailCommonTest extends CiviUnitTestCase {
/**
]);
}
+ /**
+ * Cleanup after test class.
+ *
+ * Make sure the setting is returned to 'stock'.
+ */
+ public function tearDown() {
+ Civi::settings()->set('allow_mail_from_logged_in_contact', 0);
+ parent::tearDown();
+ }
+
/**
* Test generating domain emails
*
/**
* Test email uses signature.
*
+ * @throws \API_Exception
* @throws \CRM_Core_Exception
* @throws \CiviCRM_API3_Exception
* @throws \Civi\API\Exception\UnauthorizedException
*/
public function testPostProcessWithSignature() {
$mut = new CiviMailUtils($this, TRUE);
+ $bcc1 = $this->individualCreate(['email' => 'bcc1@example.com']);
+ $bcc2 = $this->individualCreate(['email' => 'bcc2@example.com']);
+ $emails = $this->callAPISuccess('Email', 'getlist', ['input' => 'bcc'])['values'];
+ $bcc = [];
+ foreach ($emails as $email) {
+ $bcc[] = $email['id'];
+ }
+ $bcc = implode(',', $bcc);
+
Civi::settings()->set('allow_mail_from_logged_in_contact', 1);
$loggedInContactID = $this->createLoggedInUser();
- $form = new CRM_Contact_Form_Task_Email();
- $_SERVER['REQUEST_METHOD'] = 'GET';
- $form->controller = new CRM_Core_Controller();
+ /* @var CRM_Contact_Form_Task_Email $form*/
+ $form = $this->getFormObject('CRM_Contact_Form_Task_Email');
+
for ($i = 0; $i < 27; $i++) {
$email = 'spy' . $i . '@secretsquirrels.com';
$contactID = $this->individualCreate(['email' => $email]);
$form->submit(array_merge($form->_defaultValues, [
'from_email_address' => $loggedInEmail['id'],
'subject' => 'Really interesting stuff',
+ 'bcc_id' => $bcc,
]));
$mut->checkMailLog([
'This is a test Signature',
]);
$mut->stop();
- Civi::settings()->set('allow_mail_from_logged_in_contact', 0);
+ $activity = Activity::get()->setCheckPermissions(FALSE)->setSelect(['details'])->execute()->first();
+ $bccUrl1 = CRM_Utils_System::url('civicrm/contact/view', ['reset' => 1, 'force' => 1, 'cid' => $bcc1], TRUE);
+ $bccUrl2 = CRM_Utils_System::url('civicrm/contact/view', ['reset' => 1, 'force' => 1, 'cid' => $bcc2], TRUE);
+ $this->assertContains("bcc : <a href='" . $bccUrl1 . "'>Mr. Anthony Anderson II</a><a href='" . $bccUrl2 . "'>Mr. Anthony Anderson II</a>", $activity['details']);
}
}