Skip to content

Commit

Permalink
Merge pull request #2610 from roadster31/mailer-factory-send-excption…
Browse files Browse the repository at this point in the history
…-fix

MailerFactory::send() is now wrapped in an exception handler
  • Loading branch information
gillesbourgeat committed Jan 13, 2019
2 parents 5c7d8aa + 16be910 commit 0fdf35f
Showing 1 changed file with 64 additions and 57 deletions.
121 changes: 64 additions & 57 deletions core/lib/Thelia/Mailer/MailerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,18 @@ class MailerFactory
* @var \Swift_Mailer
*/
protected $swiftMailer;

protected $dispatcher;
protected $parser;

public function __construct(EventDispatcherInterface $dispatcher, ParserInterface $parser)
{
$this->dispatcher = $dispatcher;
$this->parser = $parser;

$transporterEvent = new MailTransporterEvent();
$this->dispatcher->dispatch(TheliaEvents::MAILTRANSPORTER_CONFIG, $transporterEvent);

if ($transporterEvent->hasTransporter()) {
$transporter = $transporterEvent->getTransporter();
} else {
Expand All @@ -56,14 +56,14 @@ public function __construct(EventDispatcherInterface $dispatcher, ParserInterfac
$transporter = \Swift_MailTransport::newInstance();
}
}

$this->swiftMailer = new \Swift_Mailer($transporter);
}

private function configureSmtp()
{
$smtpTransporter = \Swift_SmtpTransport::newInstance(ConfigQuery::getSmtpHost(), ConfigQuery::getSmtpPort());

if (ConfigQuery::getSmtpEncryption()) {
$smtpTransporter->setEncryption(ConfigQuery::getSmtpEncryption());
}
Expand All @@ -82,10 +82,10 @@ private function configureSmtp()
if (ConfigQuery::getSmtpSourceIp()) {
$smtpTransporter->setSourceIp(ConfigQuery::getSmtpSourceIp());
}

return $smtpTransporter;
}

/**
* @param \Swift_Mime_Message $message
* @param null $failedRecipients
Expand All @@ -95,15 +95,15 @@ public function send(\Swift_Mime_Message $message, &$failedRecipients = null)
{
return $this->swiftMailer->send($message, $failedRecipients);
}

/**
* @return \Swift_Mailer
*/
public function getSwiftMailer()
{
return $this->swiftMailer;
}

/**
* Return a new message instance
*
Expand All @@ -113,7 +113,7 @@ public function getMessageInstance()
{
return \Swift_Message::newInstance();
}

/**
* Send a message to the customer.
*
Expand All @@ -125,7 +125,7 @@ public function sendEmailToCustomer($messageCode, $customer, $messageParameters
{
// Always add the customer ID to the parameters
$messageParameters['customer_id'] = $customer->getId();

$this->sendEmailMessage(
$messageCode,
[ ConfigQuery::getStoreEmail() => ConfigQuery::getStoreName() ],
Expand All @@ -134,7 +134,7 @@ public function sendEmailToCustomer($messageCode, $customer, $messageParameters
$customer->getCustomerLang()->getLocale()
);
}

/**
* Send a message to the shop managers.
*
Expand All @@ -145,16 +145,16 @@ public function sendEmailToCustomer($messageCode, $customer, $messageParameters
public function sendEmailToShopManagers($messageCode, $messageParameters = [], $replyTo = [])
{
$storeName = ConfigQuery::getStoreName();

// Build the list of email recipients
$recipients = ConfigQuery::getNotificationEmailsList();

$to = [];

foreach ($recipients as $recipient) {
$to[$recipient] = $storeName;
}

$this->sendEmailMessage(
$messageCode,
[ConfigQuery::getStoreEmail() => $storeName],
Expand All @@ -166,7 +166,7 @@ public function sendEmailToShopManagers($messageCode, $messageParameters = [], $
$replyTo
);
}

/**
* Send a message to the customer.
*
Expand All @@ -182,25 +182,31 @@ public function sendEmailToShopManagers($messageCode, $messageParameters = [], $
public function sendEmailMessage($messageCode, $from, $to, $messageParameters = [], $locale = null, $cc = [], $bcc = [], $replyTo = [])
{
$store_email = ConfigQuery::getStoreEmail();

if (! empty($store_email)) {
if (! empty($to)) {
$instance = $this->createEmailMessage($messageCode, $from, $to, $messageParameters, $locale, $cc, $bcc, $replyTo);

$sentCount = $this->send($instance, $failedRecipients);

if ($sentCount == 0) {
try {
$instance = $this->createEmailMessage($messageCode, $from, $to, $messageParameters, $locale, $cc, $bcc, $replyTo);

$sentCount = $this->send($instance, $failedRecipients);

if ($sentCount == 0) {
Tlog::getInstance()->addError(
Translator::getInstance()->trans(
"Failed to send message %code. Failed recipients: %failed_addresses",
[
'%code' => $messageCode,
'%failed_addresses' => is_array($failedRecipients) ? implode(
',',
$failedRecipients
) : 'none'
]
)
);
}
} catch (\Exception $ex) {
Tlog::getInstance()->addError(
Translator::getInstance()->trans(
"Failed to send message %code. Failed recipients: %failed_addresses",
[
'%code' => $messageCode,
'%failed_addresses' => is_array($failedRecipients) ? implode(
',',
$failedRecipients
) : 'none'
]
)
"Error while sending email message $messageCode: " . $ex->getMessage()
);
}
} else {
Expand All @@ -210,7 +216,7 @@ public function sendEmailMessage($messageCode, $from, $to, $messageParameters =
Tlog::getInstance()->addError("Can't send email message $messageCode: store email address is not defined.");
}
}

/**
* Create a SwiftMessage instance from a given message code.
*
Expand All @@ -224,40 +230,41 @@ public function sendEmailMessage($messageCode, $from, $to, $messageParameters =
* @param array $replyTo Reply to addresses. An array of (email-address => name) [optional]
*
* @return \Swift_Message the generated and built message.
* @throws \SmartyException
*/
public function createEmailMessage($messageCode, $from, $to, $messageParameters = [], $locale = null, $cc = [], $bcc = [], $replyTo = [])
{
if (null !== $message = MessageQuery::getFromName($messageCode)) {
if ($locale === null) {
$locale = Lang::getDefaultLanguage()->getLocale();
}

$message->setLocale($locale);

// Assign parameters
foreach ($messageParameters as $name => $value) {
$this->parser->assign($name, $value);
}

$this->parser->assign('locale', $locale);

$instance = $this->getMessageInstance();

$this->setupMessageHeaders($instance, $from, $to, $cc, $bcc, $replyTo);

$message->buildMessage($this->parser, $instance);

return $instance;
}

throw new \RuntimeException(
Translator::getInstance()->trans(
"Failed to load message with code '%code%', propably because it does'nt exists.",
[ '%code%' => $messageCode ]
)
);
}

/**
* Create a SwiftMessage instance from text
*
Expand All @@ -275,28 +282,28 @@ public function createEmailMessage($messageCode, $from, $to, $messageParameters
public function createSimpleEmailMessage($from, $to, $subject, $htmlBody, $textBody, $cc = [], $bcc = [], $replyTo = [])
{
$instance = $this->getMessageInstance();

$this->setupMessageHeaders($instance, $from, $to, $cc, $bcc, $replyTo);

$instance->setSubject($subject);

// If we do not have an HTML message
if (empty($htmlMessage)) {
// Message body is the text message
$instance->setBody($textBody, 'text/plain');
} else {
// The main body is the HTML messahe
$instance->setBody($htmlBody, 'text/html');

// Use the text as a message part, if we have one.
if (! empty($textMessage)) {
$instance->addPart($textBody, 'text/plain');
}
}

return $instance;
}

/**
* @param array $from From addresses. An array of (email-address => name)
* @param array $to To addresses. An array of (email-address => name)
Expand All @@ -312,10 +319,10 @@ public function createSimpleEmailMessage($from, $to, $subject, $htmlBody, $textB
public function sendSimpleEmailMessage($from, $to, $subject, $htmlBody, $textBody, $cc = [], $bcc = [], $replyTo = [], &$failedRecipients = null)
{
$instance = $this->createSimpleEmailMessage($from, $to, $subject, $htmlBody, $textBody, $cc, $bcc, $replyTo);

return $this->send($instance, $failedRecipients);
}

/**
* @param \Swift_Message $instance
* @param array $from From addresses. An array of (email-address => name)
Expand All @@ -330,22 +337,22 @@ protected function setupMessageHeaders($instance, $from, $to, $cc = [], $bcc = [
foreach ($from as $address => $name) {
$instance->addFrom($address, $name);
}

// Add to addresses
foreach ($to as $address => $name) {
$instance->addTo($address, $name);
}

// Add cc addresses
foreach ($cc as $address => $name) {
$instance->addCc($address, $name);
}

// Add bcc addresses
foreach ($bcc as $address => $name) {
$instance->addBcc($address, $name);
}

// Add reply to addresses
foreach ($replyTo as $address => $name) {
$instance->addReplyTo($address, $name);
Expand Down

0 comments on commit 0fdf35f

Please sign in to comment.