$this->add('select', 'is_default', ts('Used For?'), $usedfor);
$this->addField('activity_status', ['placeholder' => FALSE]);
+ $this->add('checkbox', 'is_non_case_email_skipped', ts('Skip emails which do not have a Case ID or Case hash'));
$this->add('checkbox', 'is_contact_creation_disabled_if_no_match', ts('Do not create new contacts when filing emails'));
}
'is_ssl',
'is_default',
'activity_status',
+ 'is_non_case_email_skipped',
'is_contact_creation_disabled_if_no_match',
];
if (in_array($f, [
'is_default',
'is_ssl',
+ 'is_non_case_email_skipped',
'is_contact_creation_disabled_if_no_match',
])) {
$params[$f] = CRM_Utils_Array::value($f, $formValues, FALSE);
$params['domain_id'] = CRM_Core_Config::domainID();
// assign id only in update mode
- $status = ts('Your New Email Settings have been saved.');
+ $status = ts('Your New Email Settings have been saved.');
if ($this->_action & CRM_Core_Action::UPDATE) {
$params['id'] = $this->_id;
$status = ts('Your Email Settings have been updated.');
*/
public function upgrade_5_31_0($rev) {
$this->addTask(ts('Upgrade DB to %1: SQL', [1 => $rev]), 'runSql', $rev);
+ $this->addTask('Add is_non_case_email_skipped column to civicrm_mail_settings', 'addColumn',
+ 'civicrm_mail_settings', 'is_non_case_email_skipped', "TINYINT DEFAULT 0 NOT NULL COMMENT 'Skip emails which do not have a Case ID or Case hash'");
$this->addTask('Add is_contact_creation_disabled_if_no_match column to civicrm_mail_settings', 'addColumn',
'civicrm_mail_settings', 'is_contact_creation_disabled_if_no_match', "TINYINT DEFAULT 0 NOT NULL COMMENT 'If this option is enabled, CiviCRM will not create new contacts when filing emails'");
}
->invoke(['caseTypes'], $caseTypes, self::$_nullObject, self::$_nullObject, self::$_nullObject, self::$_nullObject, self::$_nullObject, 'civicrm_caseTypes');
}
+ /**
+ * This hook is called when getting case email subject patterns.
+ *
+ * All emails related to cases have case hash/id in the subject, e.g:
+ * [case #ab12efg] Magic moment
+ * [case #1234] Magic is here
+ *
+ * Using this hook you can replace/enrich default list with some other
+ * patterns, e.g. include case type categories (see CiviCase extension) like:
+ * [(case|project|policy initiative) #hash]
+ * [(case|project|policy initiative) #id]
+ *
+ * @param array $subjectPatterns
+ * Cases related email subject regexp patterns.
+ *
+ * @return mixed
+ */
+ public static function caseEmailSubjectPatterns(&$subjectPatterns) {
+ return self::singleton()
+ ->invoke(['caseEmailSubjectPatterns'], $subjectPatterns, self::$_nullObject, self::$_nullObject, self::$_nullObject, self::$_nullObject, self::$_nullObject, 'civicrm_caseEmailSubjectPatterns');
+ }
+
/**
* This hook is called soon after the CRM_Core_Config object has ben initialized.
* You can use this hook to modify the config object and hence behavior of CiviCRM dynamically.
--- /dev/null
+<?php
+/*
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC. All rights reserved. |
+ | |
+ | This work is published under the GNU AGPLv3 license with some |
+ | permitted exceptions and without any warranty. For full license |
+ | and copyright information, see https://civicrm.org/licensing |
+ +--------------------------------------------------------------------+
+ */
+
+/**
+ *
+ * @package CRM
+ * @copyright CiviCRM LLC https://civicrm.org/licensing
+ */
+
+/**
+ * Class CRM_Utils_Mail_CaseMail.
+ */
+class CRM_Utils_Mail_CaseMail {
+
+ /**
+ * A word that is used for cases by default (in email subject).
+ *
+ * @var string
+ */
+ private $caseLabel = 'case';
+
+ /**
+ * Default cases related email subject regexp patterns.
+ *
+ * All emails related to cases have case hash/id in the subject, e.g:
+ * [case #ab12efg] Magic moment
+ * [case #1234] Magic is here
+ * This variable is defined in constructor.
+ *
+ * @var array|string[]
+ */
+ private $subjectPatterns = [];
+
+ /**
+ * Cases related email subject regexp patterns extended by hooks.
+ *
+ * @var array|string[]
+ */
+ private $subjectPatternsHooked = [];
+
+ /**
+ * CRM_Utils_Mail_CaseMail constructor.
+ */
+ public function __construct() {
+ $this->subjectPatterns = [
+ '/\[' . $this->caseLabel . ' #([0-9a-f]{7})\]/i',
+ '/\[' . $this->caseLabel . ' #(\d+)\]/i',
+ ];
+ }
+
+ /**
+ * Checks if email is related to cases.
+ *
+ * @param string $subject
+ * Email subject.
+ *
+ * @return bool
+ * TRUE if email subject contains case ID or case hash, FALSE otherwise.
+ */
+ public function isCaseEmail ($subject) {
+ $subject = trim($subject);
+ $patterns = $this->getSubjectPatterns();
+ $res = FALSE;
+
+ for ($i = 0; !$res && $i < count($patterns); $i++) {
+ $res = preg_match($patterns[$i], $subject) === 1;
+ }
+
+ return $res;
+ }
+
+ /**
+ * Returns cases related email subject patterns.
+ *
+ * These patterns could be used to check if email is related to cases.
+ *
+ * @return array|string[]
+ */
+ public function getSubjectPatterns() {
+ // Allow others to change patterns using hook.
+ if (empty($this->subjectPatternsHooked)) {
+ $patterns = $this->subjectPatterns;
+ CRM_Utils_Hook::caseEmailSubjectPatterns($patterns);
+ $this->subjectPatternsHooked = $patterns;
+ }
+
+ return !empty($this->subjectPatternsHooked)
+ ? $this->subjectPatternsHooked
+ : $this->subjectPatterns;
+ }
+
+ /**
+ * Returns value of some class property.
+ *
+ * @param string $name
+ * Property name.
+ *
+ * @return mixed|null
+ * Property value or null if property does not exist.
+ */
+ public function get($name) {
+ return $this->{$name} ?? NULL;
+ }
+
+ /**
+ * Sets value of some class property.
+ *
+ * @param string $name
+ * Property name.
+ * @param mixed $value
+ * New property value.
+ */
+ public function set($name, $value) {
+ if (isset($this->{$name})) {
+ $this->{$name} = $value;
+ }
+ }
+
+}
// preseve backward compatibility
if ($usedfor == 0 || $is_create_activities) {
+ // Mail account may have 'Skip emails which do not have a Case ID
+ // or Case hash' option, if its enabled and email is not related
+ // to cases - then we need to put email to ignored folder.
+ $caseMailUtils = new CRM_Utils_Mail_CaseMail();
+ if (!empty($dao->is_non_case_email_skipped) && !$caseMailUtils->isCaseEmail($mail->subject)) {
+ $store->markIgnored($key);
+ continue;
+ }
+
// if its the activities that needs to be processed ..
try {
$createContact = !($dao->is_contact_creation_disabled_if_no_match ?? FALSE);
<tr class="crm-mail-settings-form-block-is_default"><td class="label">{$form.is_default.label}</td><td>{$form.is_default.html}</td></tr>
<tr><td class="label"> </td><td class="description">{ts}How this mail account will be used. Only one box may be used for bounce processing. It will also be used as the envelope email when sending mass mailings.{/ts}</td></tr>
- <tr class="crm-mail-settings-form-block-is_contact_creation_disabled_if_no_match"><td class="label"> </td><td>{$form.is_contact_creation_disabled_if_no_match.html}{$form.is_contact_creation_disabled_if_no_match.label}</td></tr>
+ <tr class="crm-mail-settings-form-block-is_non_case_email_skipped"><td class="label"> </td><td>{$form.is_non_case_email_skipped.html}{$form.is_non_case_email_skipped.label} {help id='is_non_case_email_skipped'}</td></tr>
+
+ <tr class="crm-mail-settings-form-block-is_contact_creation_disabled_if_no_match"><td class="label"> </td><td>{$form.is_contact_creation_disabled_if_no_match.html}{$form.is_contact_creation_disabled_if_no_match.label} {help id='is_contact_creation_disabled_if_no_match'}</td></tr>
<tr class="crm-mail-settings-form-block-activity_status"><td class="label"> </td><td>{$form.activity_status.label}<div>{$form.activity_status.html}</div></td></tr>
</table>
function showActivityFields() {
var fields = [
'.crm-mail-settings-form-block-activity_status',
+ '.crm-mail-settings-form-block-is_non_case_email_skipped',
'.crm-mail-settings-form-block-is_contact_creation_disabled_if_no_match',
];
-
$(fields.join(', '), $form).toggle($(this).val() === '0');
}
$('select[name="is_default"]').each(showActivityFields).change(showActivityFields);
--- /dev/null
+{*
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC. All rights reserved. |
+ | |
+ | This work is published under the GNU AGPLv3 license with some |
+ | permitted exceptions and without any warranty. For full license |
+ | and copyright information, see https://civicrm.org/licensing |
+ +--------------------------------------------------------------------+
+*}
+
+{htxt id="is_non_case_email_skipped-title"}
+ {ts}Skip emails which do not have a Case ID or Case hash{/ts}
+{/htxt}
+
+{htxt id="is_non_case_email_skipped"}
+ <p>{ts}CiviCRM has functionality to file emails which contain the Case ID or Case Hash in the subject line in the format [case #1234] against a case record.{/ts}</p>
+ <p>{ts}Where the Case ID or Case Hash is not included CiviCRM will file the email against the contact record, by matching the email addresses on the email with any email addresses of Contact records in CiviCRM.{/ts}</p>
+ <p>{ts}Enabling this option will have CiviCRM skip any emails that do not have the Case ID or Case Hash so that the system will only process emails that can be placed on case records.{/ts}</p>
+ <p>{ts}Any emails that are not processed will be moved to the ignored folder.{/ts}</p>
+ <p>{ts}If email is skipped, no activities or contacts ("from"/"to"/"cc"/"bcc") will be created.{/ts}</p>
+{/htxt}
+
+{htxt id="is_contact_creation_disabled_if_no_match-title"}
+ {ts}Do not create new contacts when filing emails{/ts}
+{/htxt}
+
+{htxt id="is_contact_creation_disabled_if_no_match"}
+ <p>{ts}If this option is enabled, CiviCRM will not create new contacts ("from"/"to"/"cc"/"bcc") when filing emails.{/ts}</p>
+ <p>{ts}If the email subject contains a valid Case ID or Case hash, the email will be filed against the case.{/ts}</p>
+{/htxt}
public function tearDown() {
CRM_Utils_File::cleanDir(__DIR__ . '/data/mail');
parent::tearDown();
- $this->quickCleanup(['civicrm_group', 'civicrm_group_contact', 'civicrm_mailing', 'civicrm_mailing_job', 'civicrm_mailing_event_bounce', 'civicrm_mailing_event_queue', 'civicrm_mailing_group', 'civicrm_mailing_recipients', 'civicrm_contact', 'civicrm_email']);
+ $this->quickCleanup([
+ 'civicrm_group',
+ 'civicrm_group_contact',
+ 'civicrm_mailing',
+ 'civicrm_mailing_job',
+ 'civicrm_mailing_event_bounce',
+ 'civicrm_mailing_event_queue',
+ 'civicrm_mailing_group',
+ 'civicrm_mailing_recipients',
+ 'civicrm_contact',
+ 'civicrm_email',
+ 'civicrm_activity',
+ ]);
}
/**
}
/**
- *
* Wrapper to check for mailing bounces.
*
* Normally we would call $this->callAPISuccessGetCount but there is not one & there is resistance to
$this->eventQueue = $this->callAPISuccess('MailingEventQueue', 'get', ['api.MailingEventQueue.create' => ['hash' => 'aaaaaaaaaaaaaaaa']]);
}
+ /**
+ * Set up mail account with 'Skip emails which do not have a Case ID or
+ * Case hash' option enabled.
+ */
+ public function setUpSkipNonCasesEmail() {
+ $this->callAPISuccess('MailSettings', 'get', [
+ 'api.MailSettings.create' => [
+ 'name' => 'mailbox',
+ 'protocol' => 'Localdir',
+ 'source' => __DIR__ . '/data/mail',
+ 'domain' => 'example.com',
+ 'is_default' => '0',
+ 'is_non_case_email_skipped' => TRUE,
+ ],
+ ]);
+ }
+
+ /**
+ * Test case email processing when is_non_case_email_skipped is enabled.
+ */
+ public function testInboundProcessingCaseEmail() {
+ $this->setUpSkipNonCasesEmail();
+ $mail = 'test_cases_email.eml';
+
+ copy(__DIR__ . '/data/inbound/' . $mail, __DIR__ . '/data/mail/' . $mail);
+ $this->callAPISuccess('job', 'fetch_activities', []);
+ $result = civicrm_api3('Activity', 'get', [
+ 'sequential' => 1,
+ 'subject' => ['LIKE' => "%[case #214bf6d]%"],
+ ]);
+ $this->assertNotEmpty($result['values'][0]['id']);
+ }
+
+ /**
+ * Test non case email processing when is_non_case_email_skipped is enabled.
+ */
+ public function testInboundProcessingNonCaseEmail() {
+ $this->setUpSkipNonCasesEmail();
+ $mail = 'test_non_cases_email.eml';
+
+ copy(__DIR__ . '/data/inbound/' . $mail, __DIR__ . '/data/mail/' . $mail);
+ $this->callAPISuccess('job', 'fetch_activities', []);
+ $result = civicrm_api3('Activity', 'get', [
+ 'sequential' => 1,
+ 'subject' => ['LIKE' => "%Love letter%"],
+ ]);
+ $this->assertEmpty($result['values']);
+ }
+
/**
* Set up mail account with 'Do not create new contacts when filing emails'
* option enabled.
--- /dev/null
+Delivered-To: to@test.test
+Received: by 10.2.13.84 with SMTP id 1234567890;
+ Wed, 19 Dec 2018 10:01:11 +0100 (CET)
+Return-Path: <>
+Message-ID: <abc.def.fhi@test.test>
+Date: Wed, 19 Dec 2018 10:01:07 +0100
+MIME-Version: 1.0
+Content-Type: text/plain; charset="utf-8"
+Content-Transfer-Encoding: 8bit
+Content-Disposition: inline
+From: from@test.test
+To: to@test.test
+Subject: [case #214bf6d] Magic is here
+
+This test case is full of fun.
--- /dev/null
+Delivered-To: to@test.test
+Received: by 10.2.13.84 with SMTP id 1234567890;
+ Wed, 19 Dec 2018 10:01:11 +0100 (CET)
+Return-Path: <>
+Message-ID: <abc.def.fhi@test.test>
+Date: Wed, 19 Dec 2018 10:01:07 +0100
+MIME-Version: 1.0
+Content-Type: text/plain; charset="utf-8"
+Content-Transfer-Encoding: 8bit
+Content-Disposition: inline
+From: from@test.test
+To: to@test.test
+Subject: Love letter
+
+I love you unit test, because you are not related to cases.
<type>Select</type>
</html>
</field>
+ <field>
+ <name>is_non_case_email_skipped</name>
+ <title>Skip emails which do not have a Case ID or Case hash</title>
+ <type>boolean</type>
+ <default>0</default>
+ <html>
+ <type>CheckBox</type>
+ </html>
+ <comment>Enabling this option will have CiviCRM skip any emails that do not have the Case ID or Case Hash so that the system will only process emails that can be placed on case records. Any emails that are not processed will be moved to the ignored folder.</comment>
+ <add>5.31</add>
+ </field>
<field>
<name>is_contact_creation_disabled_if_no_match</name>
<type>boolean</type>