Skip to content

Commit

Permalink
Do not send at all if contact is bounced
Browse files Browse the repository at this point in the history
  • Loading branch information
mzagmajster committed Apr 2, 2024
1 parent 7e482b7 commit cc77785
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
34 changes: 34 additions & 0 deletions app/bundles/EmailBundle/Entity/EmailRepository.php
Expand Up @@ -14,6 +14,7 @@
*/
class EmailRepository extends CommonRepository
{

/**
* Get an array of do not email.
*
Expand Down Expand Up @@ -44,6 +45,39 @@ public function getDoNotEmailList($leadIds = []): array
return $dnc;
}

/**
* Get an array of do not email.
*
* @param array $leadIds
*/
public function getDoNotEmailWithReasonList($leadIds = []): array
{
$q = $this->getEntityManager()->getConnection()->createQueryBuilder();
$q->select('l.id, l.email, dnc.reason')
->from(MAUTIC_TABLE_PREFIX.'lead_donotcontact', 'dnc')
->leftJoin('dnc', MAUTIC_TABLE_PREFIX.'leads', 'l', 'l.id = dnc.lead_id')
->where('dnc.channel = "email"')
->andWhere($q->expr()->neq('l.email', $q->expr()->literal('')));

if ($leadIds) {
$q->andWhere(
$q->expr()->in('l.id', $leadIds)
);
}

$results = $q->executeQuery()->fetchAllAssociative();

$dnc = [];
foreach ($results as $r) {
$dnc[$r['id']] = [
'email' => strtolower($r['email']),
'reason' => intval($r['reason']),
];
}

return $dnc;
}

/**
* Check to see if an email is set as do not contact.
*
Expand Down
7 changes: 4 additions & 3 deletions app/bundles/EmailBundle/Model/EmailModel.php
Expand Up @@ -1318,13 +1318,14 @@ public function sendEmail(Email $email, $leads, $options = [])
// get email settings such as templates, weights, etc
$emailSettings = &$this->getEmailSettings($email);

if (!$ignoreDNC) {
$dnc = $emailRepo->getDoNotEmailList($leadIds);
$dnc = $emailRepo->getDoNotEmailWithReasonList($leadIds);
foreach ($dnc as $removeMeId => $removeMeDetails) {
if (!$ignoreDNC || $removeMeDetails['reason'] === DoNotContact::BOUNCED) {

foreach ($dnc as $removeMeId => $removeMeEmail) {
if ($dncAsError) {
$errors[$removeMeId] = $this->translator->trans('mautic.email.dnc');
}

unset($sendTo[$removeMeId]);
unset($leadIds[$removeMeId]);
}
Expand Down

0 comments on commit cc77785

Please sign in to comment.