/** * @param $fromEmail * @param BatchEntryMail $batchMail * @param $doSend */ private function sendBatchMail($fromEmail, BatchEntryMail $batchMail, $doSend) { $receivers = $batchMail->getReceiverEntries($this->em); $this->writeOutput('Sending "' . $batchMail->getName() . '" mail to ' . count($receivers) . ' people from ' . $fromEmail); $spool = $this->mailer->getTransport()->getSpool(); foreach ($receivers as $receiver) { $this->writeOutput(' -> ' . $receiver->getEmail()); $htmlTemplate = $batchMail->getHtmlTemplate($receiver); $plainTextTemplate = $batchMail->getPlainTextTemplate($receiver); $templateData = $batchMail->getTemplateData($receiver, $this->em); $this->translator->setLocale($receiver->getPool()->getLocale()); $plainTextBody = $this->twig->render($plainTextTemplate, $templateData); $htmlBody = $this->twig->render($htmlTemplate, $templateData); $message = \Swift_Message::newInstance()->setSubject($batchMail->getSubject($receiver, $this->translator))->setFrom($fromEmail, $batchMail->getFrom($receiver, $this->translator))->setTo($receiver->getEmail())->setBody($plainTextBody)->addPart($htmlBody, 'text/html'); if ($doSend) { $this->mailer->send($message); $spool->flushQueue($this->transport); $batchMail->handleMailSent($receiver, $this->em); } } }
/** * @param $fromEmail * @param BatchEntryMail $batchMail * @param $doSend */ private function sendBatchMail($fromEmail, BatchEntryMail $batchMail, $doSend) { $receivers = $batchMail->getReceiverEntries($this->em); $this->writeOutput('Sending "' . $batchMail->getName() . '" mail to ' . count($receivers) . ' people from ' . $fromEmail); $spool = $this->mailer->getTransport()->getSpool(); foreach ($receivers as $receiver) { try { $this->writeOutput(' -> ' . $receiver->getEmail()); $htmlTemplate = $batchMail->getHtmlTemplate($receiver); $plainTextTemplate = $batchMail->getPlainTextTemplate($receiver); $templateData = $batchMail->getTemplateData($receiver, $this->em); $this->translator->setLocale($receiver->getPool()->getLocale()); $plainTextBody = $this->twig->render($plainTextTemplate, $templateData); $htmlBody = $this->twig->render($htmlTemplate, $templateData); $message = \Swift_Message::newInstance()->setSubject($batchMail->getSubject($receiver, $this->translator))->setFrom($fromEmail, $batchMail->getFrom($receiver, $this->translator))->setTo($receiver->getEmail())->setBody($plainTextBody)->addPart($htmlBody, 'text/html'); if ($doSend) { $this->mailer->send($message); $batchMail->handleMailSent($receiver, $this->em); } } catch (\Exception $e) { $this->writeOutput(sprintf('<error>An error occurred while sending mail for email "%s"</error>', $receiver->getEmail())); // mark as handled, as otherwise the system will keep retrying over and over again $batchMail->handleMailSent($receiver, $this->em); } } if ($doSend) { // only flush queue at the end of a batch $spool->flushQueue($this->transport); } }