Skip to content

Commit

Permalink
Merge pull request mautic#8768 from hluchas/m3-changed-to-flush-queue…
Browse files Browse the repository at this point in the history
…-before-cc-and-bcc-are-cleared

M3 changed to flush queue before cc and bcc are cleared
  • Loading branch information
alanhartless authored May 15, 2020
2 parents f4b4c31 + 3bca017 commit f4fc596
Showing 1 changed file with 62 additions and 32 deletions.
94 changes: 62 additions & 32 deletions app/bundles/EmailBundle/Model/EmailModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
use Mautic\EmailBundle\Event\EmailEvent;
use Mautic\EmailBundle\Event\EmailOpenEvent;
use Mautic\EmailBundle\Event\EmailSendEvent;
use Mautic\EmailBundle\Exception\EmailCouldNotBeSentException;
use Mautic\EmailBundle\Exception\FailedToSendToContactException;
use Mautic\EmailBundle\Form\Type\EmailType;
use Mautic\EmailBundle\Helper\MailHelper;
Expand Down Expand Up @@ -1557,26 +1558,34 @@ public function sendEmailToUser(

$errors = [];

$firstMail = true;
foreach ($to as $toAddress) {
$idHash = uniqid();
$mailer->setIdHash($idHash, $saveStat);

if (!$mailer->addTo($toAddress)) {
$errors[] = "{$toAddress}: ".$this->translator->trans('mautic.email.bounce.reason.bad_email');
} else {
if (!$mailer->queue(true)) {
$errorArray = $mailer->getErrors();
unset($errorArray['failures']);
$errors[] = "{$toAddress}: ".implode('; ', $errorArray);
}
continue;
}

if ($saveStat) {
$saveEntities[] = $mailer->createEmailStat(false, $toAddress);
}
if (!$mailer->queue(true)) {
$errorArray = $mailer->getErrors();
unset($errorArray['failures']);
$errors[] = "{$toAddress}: ".implode('; ', $errorArray);
}

// Clear CC and BCC to do not duplicate the send several times
$mailer->setCc([]);
$mailer->setBcc([]);
if ($saveStat) {
$saveEntities[] = $mailer->createEmailStat(false, $toAddress);
}

// If this is the first message, flush the queue. This process clears the cc and bcc.
if (true === $firstMail) {
try {
$this->flushQueue($mailer);
} catch (EmailCouldNotBeSentException $e) {
$errors[] = $e->getMessage();
}
$firstMail = false;
}
}

Expand Down Expand Up @@ -1605,28 +1614,34 @@ public function sendEmailToUser(

if (!$mailer->setTo($user['email'], $user['firstname'].' '.$user['lastname'])) {
$errors[] = "{$user['email']}: ".$this->translator->trans('mautic.email.bounce.reason.bad_email');
} else {
if (!$mailer->queue(true)) {
$errorArray = $mailer->getErrors();
unset($errorArray['failures']);
$errors[] = "{$user['email']}: ".implode('; ', $errorArray);
}
continue;
}

if ($saveStat) {
$saveEntities[] = $mailer->createEmailStat(false, $user['email']);
}
if (!$mailer->queue(true)) {
$errorArray = $mailer->getErrors();
unset($errorArray['failures']);
$errors[] = "{$user['email']}: ".implode('; ', $errorArray);
}

// Clear CC and BCC to do not duplicate the send several times
$mailer->setCc([]);
$mailer->setBcc([]);
if ($saveStat) {
$saveEntities[] = $mailer->createEmailStat(false, $user['email']);
}

// If this is the first message, flush the queue. This process clears the cc and bcc.
if (true === $firstMail) {
try {
$this->flushQueue($mailer);
} catch (EmailCouldNotBeSentException $e) {
$errors[] = $e->getMessage();
}
$firstMail = false;
}
}

//flush the message
if (!$mailer->flushQueue()) {
$errorArray = $mailer->getErrors();
unset($errorArray['failures']);
$errors[] = implode('; ', $errorArray);
try {
$this->flushQueue($mailer);
} catch (EmailCouldNotBeSentException $e) {
$errors[] = $e->getMessage();
}

if (isset($saveEntities)) {
Expand All @@ -1639,6 +1654,19 @@ public function sendEmailToUser(
return $errors;
}

/**
* @throws EmailCouldNotBeSentException
*/
private function flushQueue(MailHelper $mailer): void
{
if (!$mailer->flushQueue()) {
$errorArray = $mailer->getErrors();
unset($errorArray['failures']);

throw new EmailCouldNotBeSentException(implode('; ', $errorArray));
}
}

/**
* Dispatches EmailSendEvent so you could get tokens form it or tokenized content.
*
Expand Down Expand Up @@ -1773,9 +1801,11 @@ public function limitQueryToCreator(QueryBuilder &$q)
/**
* Get line chart data of emails sent and read.
*
* @param char $unit {@link php.net/manual/en/function.date.php#refsect1-function.date-parameters}
* @param string $dateFormat
* @param bool $canViewOthers
* @param $reason
* @param $canViewOthers
* @param int|null $companyId
* @param int|null $campaignId
* @param int|null $segmentId
*
* @return array
*/
Expand Down

0 comments on commit f4fc596

Please sign in to comment.