diff --git a/lib/Mailer/Methods/ErrorMappers/SMTPMapper.php b/lib/Mailer/Methods/ErrorMappers/SMTPMapper.php index cda221e1c1..0608e8c8d6 100644 --- a/lib/Mailer/Methods/ErrorMappers/SMTPMapper.php +++ b/lib/Mailer/Methods/ErrorMappers/SMTPMapper.php @@ -8,10 +8,25 @@ use MailPoet\Mailer\SubscriberError; class SMTPMapper { use ConnectionErrorMapperTrait; - function getErrorFromException(\Exception $e) { + /** + * @see https://swiftmailer.symfony.com/docs/sending.html + * @return MailerError + */ + function getErrorFromException(\Exception $e, $subscriber, $extra_params = []) { // remove redundant information appended by Swift logger to exception messages $message = explode(PHP_EOL, $e->getMessage()); - return new MailerError(MailerError::OPERATION_SEND, MailerError::LEVEL_HARD, $message[0]); + + $level = MailerError::LEVEL_HARD; + if($e instanceof \Swift_RfcComplianceException) { + $level = MailerError::LEVEL_SOFT; + } + + $subscriber_errors = []; + if(empty($extra_params['test_email'])) { + $subscriber_errors[] = new SubscriberError($subscriber, null); + } + + return new MailerError(MailerError::OPERATION_SEND, $level, $message[0], null, $subscriber_errors); } function getErrorFromLog($log, $subscriber, $extra_params = []) { diff --git a/lib/Mailer/Methods/SMTP.php b/lib/Mailer/Methods/SMTP.php index cb6945f83a..2395594e91 100644 --- a/lib/Mailer/Methods/SMTP.php +++ b/lib/Mailer/Methods/SMTP.php @@ -49,7 +49,7 @@ class SMTP { $result = $this->mailer->send($message); } catch(\Exception $e) { return Mailer::formatMailerErrorResult( - $this->error_mapper->getErrorFromException($e) + $this->error_mapper->getErrorFromException($e, $subscriber, $extra_params) ); } if($result === 1) { diff --git a/tests/unit/Mailer/Methods/ErrorMappers/SMTPMapperTest.php b/tests/unit/Mailer/Methods/ErrorMappers/SMTPMapperTest.php index 90b694147a..62785c1f32 100644 --- a/tests/unit/Mailer/Methods/ErrorMappers/SMTPMapperTest.php +++ b/tests/unit/Mailer/Methods/ErrorMappers/SMTPMapperTest.php @@ -2,6 +2,7 @@ namespace MailPoet\Test\Mailer\Methods\ErrorMappers; use MailPoet\Mailer\Mailer; +use MailPoet\Mailer\MailerError; use MailPoet\Mailer\Methods\ErrorMappers\SMTPMapper; class SMTPMapperTest extends \MailPoetTest { @@ -18,9 +19,17 @@ class SMTPMapperTest extends \MailPoetTest { . 'Log data:' . PHP_EOL . '++ Starting Swift_SmtpTransport' . PHP_EOL . '!! Connection could not be established with host localhost [Connection refused #111] (code: 0)'; - $error = $this->mapper->getErrorFromException(new \Exception($message)); + $error = $this->mapper->getErrorFromException(new \Exception($message), 'john@rambo.com'); expect($error->getMessage()) ->equals('Connection could not be established with host localhost [Connection refused #111]'); + expect($error->getLevel())->equals(MailerError::LEVEL_HARD); + expect($error->getSubscriberErrors()[0]->getEmail())->equals('john@rambo.com'); + } + + function testItCreatesSoftErrorForInvalidEmail() { + $message = 'Invalid email'; + $error = $this->mapper->getErrorFromException(new \Swift_RfcComplianceException($message), 'john@rambo.com'); + expect($error->getLevel())->equals(MailerError::LEVEL_SOFT); } function testItCanProcessLogMessageWhenOneExists() {