Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

don't create duplicate contacts when sending test emails #29212

Merged
merged 1 commit into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions api/v3/Mailing.php
Original file line number Diff line number Diff line change
Expand Up @@ -671,13 +671,9 @@ function civicrm_api3_mailing_send_test($params) {
$testEmailParams['emails'] = array_key_exists('test_email', $testEmailParams) ? explode(',', strtolower($testEmailParams['test_email'] ?? '')) : NULL;
if (!empty($params['test_email'])) {
$query = CRM_Utils_SQL_Select::from('civicrm_email e')
->select(['e.id', 'e.contact_id', 'e.email'])
->select(['e.id', 'e.contact_id', 'e.email', 'e.on_hold', 'c.is_opt_out', 'c.do_not_email', 'c.is_deceased'])
->join('c', 'INNER JOIN civicrm_contact c ON e.contact_id = c.id')
->where('e.email IN (@emails)', ['@emails' => $testEmailParams['emails']])
->where('e.on_hold = 0')
->where('c.is_opt_out = 0')
->where('c.do_not_email = 0')
->where('c.is_deceased = 0')
->where('c.is_deleted = 0')
->groupBy('e.id')
->orderBy(['e.is_bulkmail DESC', 'e.is_primary DESC'])
Expand All @@ -689,16 +685,22 @@ function civicrm_api3_mailing_send_test($params) {
$emailDetail[strtolower($dao->email)] = [
'contact_id' => $dao->contact_id,
'email_id' => $dao->id,
'is_opt_out' => $dao->is_opt_out,
'do_not_email' => $dao->do_not_email,
'is_deceased' => $dao->is_deceased,
];
}
foreach ($testEmailParams['emails'] as $key => $email) {
foreach ($testEmailParams['emails'] as $email) {
$email = trim($email);
$contactId = $emailId = NULL;
if (array_key_exists($email, $emailDetail)) {
if ($emailDetail[$email]['is_opt_out'] || $emailDetail[$email]['do_not_email'] || $emailDetail[$email]['is_deceased']) {
continue;
}
$emailId = $emailDetail[$email]['email_id'];
$contactId = $emailDetail[$email]['contact_id'];
}
if (!$contactId && CRM_Core_Permission::check('add contacts')) {
elseif (!$contactId && CRM_Core_Permission::check('add contacts')) {
//create new contact.
$contact = civicrm_api3('Contact', 'create',
[
Expand Down
2 changes: 1 addition & 1 deletion ext/civi_mail/ang/crmMailing/services.js
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@
.then(function (deliveryInfos) {
var count = Object.keys(deliveryInfos).length;
if (count === 0) {
CRM.alert(ts('Could not identify any recipients. Perhaps your test group is empty, or you tried sending to contacts that do not exist and you have no permission to add contacts.'));
CRM.alert(ts('Could not identify any recipients. Perhaps your test group is empty, all contacts are set to deceased/opt out/do_not_email, or you tried sending to contacts that do not exist and you have no permission to add contacts.'));
}
})
;
Expand Down
8 changes: 7 additions & 1 deletion tests/phpunit/api/v3/MailingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -460,10 +460,16 @@ public function testMailerSendTest_email(): void {
'first_name' => 'Alice',
'last_name' => 'Person',
]);
$contactIDs['bob'] = $this->individualCreate([
'email' => '[email protected]',
'first_name' => 'Bob',
'last_name' => 'Person',
'do_not_email' => 1,
]);

$mail = $this->callAPISuccess('mailing', 'create', $this->_params);

$params = ['mailing_id' => $mail['id'], 'test_email' => '[email protected]', 'test_group' => NULL];
$params = ['mailing_id' => $mail['id'], 'test_email' => '[email protected],[email protected]', 'test_group' => NULL];
// Per https://lab.civicrm.org/dev/core/issues/229 ensure this is not passed through!
// Per https://lab.civicrm.org/dev/mail/issues/32 test non-lowercase email
$params['id'] = $mail['id'];
Expand Down