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

M3 changed to flush queue before cc and bcc are cleared #8768

Merged
merged 4 commits into from May 15, 2020
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
81 changes: 49 additions & 32 deletions app/bundles/EmailBundle/Model/EmailModel.php
Expand Up @@ -1557,26 +1557,30 @@ 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);
}

if ($saveStat) {
$saveEntities[] = $mailer->createEmailStat(false, $toAddress);
}

// Clear CC and BCC to do not duplicate the send several times
$mailer->setCc([]);
$mailer->setBcc([]);
// If this is the first message, flush the queue. This process clears the cc and bcc.
if (true === $firstMail) {
$errors[] = $this->flushQueue($mailer);
$firstMail = false;
}
}

Expand Down Expand Up @@ -1605,29 +1609,28 @@ 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);
}

if ($saveStat) {
$saveEntities[] = $mailer->createEmailStat(false, $user['email']);
}

// Clear CC and BCC to do not duplicate the send several times
$mailer->setCc([]);
$mailer->setBcc([]);
// If this is the first message, flush the queue. This process clears the cc and bcc.
if (true === $firstMail) {
$this->flushQueue($mailer);
$firstMail = false;
}
}

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

if (isset($saveEntities)) {
$this->getStatRepository()->saveEntities($saveEntities);
Expand All @@ -1639,6 +1642,18 @@ public function sendEmailToUser(
return $errors;
}

private function flushQueue(MailHelper $mailer): array
{
if (!$mailer->flushQueue()) {
$errorArray = $mailer->getErrors();
unset($errorArray['failures']);

return implode('; ', $errorArray);
}

return [];
}

/**
* Dispatches EmailSendEvent so you could get tokens form it or tokenized content.
*
Expand Down Expand Up @@ -1773,9 +1788,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