From ab58d036d01cd25072f730bce3104f2ba56bbe08 Mon Sep 17 00:00:00 2001 From: Maxime Hermouet Date: Wed, 6 Jun 2018 18:04:48 +0200 Subject: [PATCH 01/13] Fix compatibility with external dependencies --- .travis.yml | 2 +- composer.json | 6 ++++-- src/Services/SendGridTransport.php | 13 +++++++++---- tests/manual-test.sh | 2 +- 4 files changed, 15 insertions(+), 8 deletions(-) diff --git a/.travis.yml b/.travis.yml index 926ec08..8c4bb83 100644 --- a/.travis.yml +++ b/.travis.yml @@ -38,7 +38,7 @@ matrix: before_install: - if [[ $COVERAGE != true ]]; then phpenv config-rm xdebug.ini || true; fi - if ! [ -z "$STABILITY" ]; then composer config minimum-stability ${STABILITY}; fi; - - composer require --no-update symfony/phpunit-bridge:* ${DEPENDENCIES} + - composer require --no-update symfony/phpunit-bridge:~4.0 ${DEPENDENCIES} install: # To be removed when this issue will be resolved: https://github.com/composer/composer/issues/5355 diff --git a/composer.json b/composer.json index 79dabf4..fa9fa00 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,9 @@ "require": { "php": ">=5.6", "sendgrid/sendgrid": "^7.0", - "symfony/swiftmailer-bundle": "~3.2" + "symfony/swiftmailer-bundle": "~3.2", + "symfony/framework-bundle": "~2.7|~3.3|~4.0", + "symfony/yaml": "~2.7|~3.3|~4.0" }, "require-dev": { "nyholm/symfony-bundle-test": "^1.3" @@ -37,4 +39,4 @@ "ExpertCoder\\Swiftmailer\\SendGridBundle\\Tests\\": "tests/" } } -} \ No newline at end of file +} diff --git a/src/Services/SendGridTransport.php b/src/Services/SendGridTransport.php index 4d919ed..19eb62e 100644 --- a/src/Services/SendGridTransport.php +++ b/src/Services/SendGridTransport.php @@ -6,7 +6,7 @@ use SendGrid; use Swift_Events_EventListener; use Swift_Mime_Attachment; -use Swift_Mime_Message; +use Swift_Mime_SimpleMessage; use Swift_Transport; use Psr\Log\LoggerInterface; @@ -81,12 +81,12 @@ public function setLogger(LoggerInterface $logger) /** * WARNING : $failedRecipients and return value are faked. * - * @param Swift_Mime_Message $message - * @param array $failedRecipients + * @param Swift_Mime_SimpleMessage $message + * @param array $failedRecipients * * @return int */ - public function send(Swift_Mime_Message $message, &$failedRecipients = null) + public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = null) { // prepare fake data. $sent = 0; @@ -184,6 +184,11 @@ public function send(Swift_Mime_Message $message, &$failedRecipients = null) return $sent; } + public function ping() + { + return true; + } + public function registerPlugin(Swift_Events_EventListener $plugin) { // unused diff --git a/tests/manual-test.sh b/tests/manual-test.sh index f3b7f16..bbe5a2e 100644 --- a/tests/manual-test.sh +++ b/tests/manual-test.sh @@ -8,7 +8,7 @@ if [ $# -eq 0 ] # check if argument exists exit 0; fi -composer require --dev --no-update symfony/phpunit-bridge:* dunglas/symfony-lock:^$1 +composer require --dev --no-update symfony/phpunit-bridge:~4.0 dunglas/symfony-lock:^$1 composer update --prefer-dist --no-interaction --prefer-stable --quiet composer update --prefer-stable --prefer-lowest --prefer-dist --no-interaction ./vendor/bin/simple-phpunit install From 7ae10803967f545b0dc08ded6e787f53de0169d5 Mon Sep 17 00:00:00 2001 From: Maxime Hermouet Date: Wed, 6 Jun 2018 18:10:44 +0200 Subject: [PATCH 02/13] Drop php 5 support --- .travis.yml | 3 +-- composer.json | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 8c4bb83..8bc13fa 100644 --- a/.travis.yml +++ b/.travis.yml @@ -14,11 +14,10 @@ matrix: # Minimum supported dependencies with the latest and oldest PHP version - php: 7.2 env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest" SYMFONY_DEPRECATIONS_HELPER="weak_vendors" - - php: 5.6 + - php: 7.0 env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest" SYMFONY_DEPRECATIONS_HELPER="weak_vendors" # Test the latest stable release - - php: 5.6 - php: 7.0 - php: 7.1 - php: 7.2 diff --git a/composer.json b/composer.json index fa9fa00..66f9d4b 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ } ], "require": { - "php": ">=5.6", + "php": ">=7.0.0", "sendgrid/sendgrid": "^7.0", "symfony/swiftmailer-bundle": "~3.2", "symfony/framework-bundle": "~2.7|~3.3|~4.0", From 52e6a1d17f7307942102fb8d53a3dbfa12136439 Mon Sep 17 00:00:00 2001 From: hydralom Date: Fri, 27 Jul 2018 14:21:26 +0200 Subject: [PATCH 03/13] fix sendgrid namespace for v7.0 --- src/Services/SendGridTransport.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Services/SendGridTransport.php b/src/Services/SendGridTransport.php index 19eb62e..7ac8bf5 100644 --- a/src/Services/SendGridTransport.php +++ b/src/Services/SendGridTransport.php @@ -97,27 +97,27 @@ public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = nul $fromName = reset($fromArray); $fromEmail = key($fromArray); - $mail = new SendGrid\Mail(); //Intentionally not using constructor arguments as they are tedious to work with + $mail = new SendGrid\Mail\Mail(); //Intentionally not using constructor arguments as they are tedious to work with // categories can be useful if you use them like tags to, for example, distinguish different applications. foreach ($this->sendGridCategories as $category) { $mail->addCategory($category); } - $mail->setFrom(new SendGrid\Email($fromName, $fromEmail)); + $mail->setFrom(new SendGrid\Mail\From($fromEmail, $fromName)); $mail->setSubject($message->getSubject()); // extract content type from body to prevent multi-part content-type error $finfo = new finfo(FILEINFO_MIME_TYPE); $contentType = $finfo->buffer($message->getBody()); - $mail->addContent(new SendGrid\Content($contentType, $message->getBody())); + $mail->addContent(new SendGrid\Mail\Content($contentType, $message->getBody())); - $personalization = new SendGrid\Personalization(); + $personalization = new SendGrid\Mail\Personalization(); // process TO if ($toArr = $message->getTo()) { foreach ($toArr as $email => $name) { - $personalization->addTo(new SendGrid\Email($name, $email)); + $personalization->addTo(new SendGrid\Mail\To($email, $name)); ++$sent; $prepareFailedRecipients[] = $email; } @@ -126,7 +126,7 @@ public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = nul // process CC if ($ccArr = $message->getCc()) { foreach ($ccArr as $email => $name) { - $personalization->addCc(new SendGrid\Email($name, $email)); + $personalization->addCc(new SendGrid\Mail\Cc($email, $name)); ++$sent; $prepareFailedRecipients[] = $email; } @@ -135,7 +135,7 @@ public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = nul // process BCC if ($bccArr = $message->getBcc()) { foreach ($bccArr as $email => $name) { - $personalization->addBcc(new SendGrid\Email($name, $email)); + $personalization->addBcc(new SendGrid\Mail\Bcc($email, $name)); ++$sent; $prepareFailedRecipients[] = $email; } @@ -145,7 +145,7 @@ public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = nul if ($attachments = $message->getChildren()) { foreach ($attachments as $attachment) { if ($attachment instanceof Swift_Mime_Attachment) { - $sAttachment = new SendGrid\Attachment(); + $sAttachment = new SendGrid\Mail\Attachment(); $sAttachment->setContent(base64_encode($attachment->getBody())); $sAttachment->setType($attachment->getContentType()); $sAttachment->setFilename($attachment->getFilename()); @@ -154,7 +154,7 @@ public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = nul $mail->addAttachment($sAttachment); } elseif (in_array($attachment->getContentType(), ['text/plain', 'text/html'])) { // add part if any is defined, to avoid error please set body as text and part as html - $mail->addContent(new SendGrid\Content($attachment->getContentType(), $attachment->getBody())); + $mail->addContent(new SendGrid\Mail\Content($attachment->getContentType(), $attachment->getBody())); } } } From 06204bc3fe42ed52db09f7ab454daa8820e9a914 Mon Sep 17 00:00:00 2001 From: Maxime Hermouet Date: Fri, 27 Jul 2018 15:36:15 +0200 Subject: [PATCH 04/13] Fix minimum requirement and add Test --- composer.json | 1 + src/Services/SendGridTransport.php | 22 +++++++++++++++++++++- tests/BundleInitializationTest.php | 25 +++++++++++++++++++++---- 3 files changed, 43 insertions(+), 5 deletions(-) diff --git a/composer.json b/composer.json index 66f9d4b..0758e94 100644 --- a/composer.json +++ b/composer.json @@ -17,6 +17,7 @@ "require": { "php": ">=7.0.0", "sendgrid/sendgrid": "^7.0", + "sendgrid/php-http-client": "~3.9.4", "symfony/swiftmailer-bundle": "~3.2", "symfony/framework-bundle": "~2.7|~3.3|~4.0", "symfony/yaml": "~2.7|~3.3|~4.0" diff --git a/src/Services/SendGridTransport.php b/src/Services/SendGridTransport.php index 7ac8bf5..aedb01c 100644 --- a/src/Services/SendGridTransport.php +++ b/src/Services/SendGridTransport.php @@ -51,10 +51,18 @@ class SendGridTransport implements Swift_Transport */ private $logger; + /** + * Http client options. + * + * @var array + */ + private $httpClientOptions; + public function __construct($sendGridApiKey, $sendGridCategories) { $this->sendGridApiKey = $sendGridApiKey; $this->sendGridCategories = $sendGridCategories; + $this->httpClientOptions = []; } public function isStarted() @@ -161,7 +169,7 @@ public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = nul $mail->addPersonalization($personalization); - $sendGrid = new SendGrid($this->sendGridApiKey); + $sendGrid = new SendGrid($this->sendGridApiKey, $this->httpClientOptions); $response = $sendGrid->client->mail()->send()->post($mail); @@ -193,4 +201,16 @@ public function registerPlugin(Swift_Events_EventListener $plugin) { // unused } + + /** + * @param array $httpClientOptions + * + * @return self + */ + public function setHttpClientOptions(array $httpClientOptions) + { + $this->httpClientOptions = $httpClientOptions; + + return $this; + } } diff --git a/tests/BundleInitializationTest.php b/tests/BundleInitializationTest.php index d0e49fd..496aaef 100644 --- a/tests/BundleInitializationTest.php +++ b/tests/BundleInitializationTest.php @@ -20,10 +20,6 @@ protected function setUp() // Make services public that have an idea that matches a regex $this->addCompilerPass(new PublicServicePass('|swiftmailer.mailer.transport.expertcoder_swift_mailer.*|')); - } - - public function testInitBundle() - { // Create a new Kernel $kernel = $this->createKernel(); @@ -32,7 +28,10 @@ public function testInitBundle() // Boot the kernel. $this->bootKernel(); + } + public function testInitBundle() + { // Get the container $container = $this->getContainer(); @@ -45,4 +44,22 @@ public function testInitBundle() $this->assertTrue($container->hasParameter('expertcoder_swiftmailer_sendgrid.api_key')); $this->assertTrue($container->hasParameter('expertcoder_swiftmailer_sendgrid.categories')); } + + // This help us ensure the API is well used + public function testSimpleMail() + { + $message = (new \Swift_Message('[Test] SwiftSendGrid')) + ->setFrom('noreply@swiftsendgrid.bundle') + ->setTo('nobody@send.grid') + ->setBody('Test body.', 'text/plain') + ; + $transport = $this->getContainer()->get('swiftmailer.mailer.transport.expertcoder_swift_mailer.send_grid'); + $transport->setHttpClientOptions([ + 'curl' => [CURLOPT_TIMEOUT => 1], + ]); + $mailer = new \Swift_Mailer($transport); + $result = $mailer->send($message); + + $this->assertEquals(0, $mailer->send($message)); // This should gives us 0 for no email sent + } } From feefef8e9723a1d91adfc6dcee7050f9be0c7784 Mon Sep 17 00:00:00 2001 From: Maxime Hermouet Date: Mon, 30 Jul 2018 10:24:14 +0200 Subject: [PATCH 05/13] Fix personalization object --- src/Services/SendGridTransport.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/Services/SendGridTransport.php b/src/Services/SendGridTransport.php index aedb01c..68893da 100644 --- a/src/Services/SendGridTransport.php +++ b/src/Services/SendGridTransport.php @@ -120,7 +120,13 @@ public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = nul $contentType = $finfo->buffer($message->getBody()); $mail->addContent(new SendGrid\Mail\Content($contentType, $message->getBody())); - $personalization = new SendGrid\Mail\Personalization(); + // personalization + if (!empty($mail->getPersonalizations())) { + $personalization = $mail->getPersonalizations()[0]; + } else { + $personalization = new SendGrid\Mail\Personalization(); + $mail->addPersonalization($personalization); + } // process TO if ($toArr = $message->getTo()) { @@ -167,8 +173,6 @@ public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = nul } } - $mail->addPersonalization($personalization); - $sendGrid = new SendGrid($this->sendGridApiKey, $this->httpClientOptions); $response = $sendGrid->client->mail()->send()->post($mail); From 0880f2a71ed72298c4b0f7d8ed0b01a03ad7bf90 Mon Sep 17 00:00:00 2001 From: Maxime Hermouet Date: Thu, 9 Aug 2018 11:49:09 +0200 Subject: [PATCH 06/13] Fixes #14 Update readme --- README.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/README.md b/README.md index f02f979..e33d4b7 100644 --- a/README.md +++ b/README.md @@ -60,6 +60,24 @@ expert_coder_swiftmailer_send_grid: api_key: %sendgrid_api_key% categories: [my_category] # optional, will be added to all mails sent (can be seen on sendgrid dashboard) ``` +Symfony 3 +--------- + +Since Symfony 3.2, you must name the custom transport service swiftmailer.mailer.transport.< name > so you can use one of the solutions below: + +*services.yml* +```yml +services: + swiftmailer.mailer.transport.expertcoder_swift_mailer.send_grid.transport: + alias: expertcoder_swift_mailer.send_grid.transport +``` +**OR** + +*config.yml* +```yml +swiftmailer: + transport: 'swiftmailer.mailer.transport.expertcoder_swift_mailer.send_grid' +``` ## Important ! From 1be0341f1090bb393fdb822706ab03895fd7dc97 Mon Sep 17 00:00:00 2001 From: Pavel Dubinin Date: Thu, 30 Aug 2018 12:47:54 +0300 Subject: [PATCH 07/13] Added support for SwiftMailer plugins --- src/Resources/config/services.xml | 1 + src/Services/SendGridTransport.php | 74 +++++++++++++++++++++++++++--- 2 files changed, 69 insertions(+), 6 deletions(-) diff --git a/src/Resources/config/services.xml b/src/Resources/config/services.xml index 06c1451..9920e20 100644 --- a/src/Resources/config/services.xml +++ b/src/Resources/config/services.xml @@ -8,6 +8,7 @@ + %expertcoder_swiftmailer_sendgrid.api_key% %expertcoder_swiftmailer_sendgrid.categories% diff --git a/src/Services/SendGridTransport.php b/src/Services/SendGridTransport.php index 68893da..0e7e6cf 100644 --- a/src/Services/SendGridTransport.php +++ b/src/Services/SendGridTransport.php @@ -8,6 +8,8 @@ use Swift_Mime_Attachment; use Swift_Mime_SimpleMessage; use Swift_Transport; +use Swift_Events_EventDispatcher; +use Swift_Events_SendEvent; use Psr\Log\LoggerInterface; class SendGridTransport implements Swift_Transport @@ -58,8 +60,17 @@ class SendGridTransport implements Swift_Transport */ private $httpClientOptions; - public function __construct($sendGridApiKey, $sendGridCategories) + /** Connection status */ + protected $started = false; + + /** + * @var Swift_Events_EventDispatcher + */ + private $eventDispatcher; + + public function __construct(Swift_Events_EventDispatcher $eventDispatcher, $sendGridApiKey, $sendGridCategories) { + $this->eventDispatcher = $eventDispatcher; $this->sendGridApiKey = $sendGridApiKey; $this->sendGridCategories = $sendGridCategories; $this->httpClientOptions = []; @@ -67,18 +78,47 @@ public function __construct($sendGridApiKey, $sendGridCategories) public function isStarted() { - //Not used - return true; + return $this->started; } public function start() { - //Not used + if (!$this->started) { + if ($evt = $this->eventDispatcher->createTransportChangeEvent($this)) { + $this->eventDispatcher->dispatchEvent($evt, 'beforeTransportStarted'); + if ($evt->bubbleCancelled()) { + return; + } + } + + //noop (transport does not need initialization) + + if ($evt) { + $this->eventDispatcher->dispatchEvent($evt, 'transportStarted'); + } + + $this->started = true; + } } public function stop() { - //Not used + if ($this->started) { + if ($evt = $this->eventDispatcher->createTransportChangeEvent($this)) { + $this->eventDispatcher->dispatchEvent($evt, 'beforeTransportStopped'); + if ($evt->bubbleCancelled()) { + return; + } + } + + //noop (transport does not need to termintated) + + if ($evt) { + $this->eventDispatcher->dispatchEvent($evt, 'transportStopped'); + } + } + + $this->started = false; } public function setLogger(LoggerInterface $logger) @@ -96,6 +136,13 @@ public function setLogger(LoggerInterface $logger) */ public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = null) { + if ($evt = $this->eventDispatcher->createSendEvent($this, $message)) { + $this->eventDispatcher->dispatchEvent($evt, 'beforeSendPerformed'); + if ($evt->bubbleCancelled()) { + return 0; + } + } + // prepare fake data. $sent = 0; $prepareFailedRecipients = []; @@ -193,6 +240,18 @@ public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = nul $sent = 0; } + if ($evt) { + if ($sent == count($toArr) + count($ccArr) + count($bccArr)) { + $evt->setResult(Swift_Events_SendEvent::RESULT_SUCCESS); + } elseif ($sent > 0) { + $evt->setResult(Swift_Events_SendEvent::RESULT_TENTATIVE); + } else { + $evt->setResult(Swift_Events_SendEvent::RESULT_FAILED); + } + $evt->setFailedRecipients($failedRecipients); + $this->eventDispatcher->dispatchEvent($evt, 'sendPerformed'); + } + return $sent; } @@ -201,9 +260,12 @@ public function ping() return true; } + /** + * @param Swift_Events_EventListener $plugin + */ public function registerPlugin(Swift_Events_EventListener $plugin) { - // unused + $this->eventDispatcher->bindEventListener($plugin); } /** From 7a6325a5370e7e332928d6a79ff7cfc78eb53613 Mon Sep 17 00:00:00 2001 From: Pavel Dubinin Date: Thu, 30 Aug 2018 13:31:02 +0300 Subject: [PATCH 08/13] Added support for custom headers --- src/Services/SendGridTransport.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/Services/SendGridTransport.php b/src/Services/SendGridTransport.php index 0e7e6cf..3206a5a 100644 --- a/src/Services/SendGridTransport.php +++ b/src/Services/SendGridTransport.php @@ -68,6 +68,15 @@ class SendGridTransport implements Swift_Transport */ private $eventDispatcher; + /** + * Some header keys are reserved. You may not include any of the following reserved keys + * (From SendGrid docs) + */ + const RESERVED_KEYWORDS = [ + 'x-sg-id', 'x-sg-eid', 'received', 'dkim-signature', 'Content-Type', 'Content-Transfer-Encoding', + 'To', 'From', 'Subject', 'Reply-To', 'CC', 'BCC' + ]; + public function __construct(Swift_Events_EventDispatcher $eventDispatcher, $sendGridApiKey, $sendGridCategories) { $this->eventDispatcher = $eventDispatcher; @@ -220,6 +229,16 @@ public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = nul } } + // add headers + if ($headers = $message->getHeaders()->getAll()) { + foreach ($headers as $header) { + $headerName = $header->getFieldName(); + if (!in_array($headerName, self::RESERVED_KEYWORDS)) { + $mail->addHeader($headerName, $header->getFieldBody()); + } + } + } + $sendGrid = new SendGrid($this->sendGridApiKey, $this->httpClientOptions); $response = $sendGrid->client->mail()->send()->post($mail); From 4943f6e88ca3a293644e199fa6c199f8519e0b1e Mon Sep 17 00:00:00 2001 From: Pavel Dubinin Date: Fri, 31 Aug 2018 19:01:35 +0300 Subject: [PATCH 09/13] Case-insensitive headers filtering --- src/Services/SendGridTransport.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Services/SendGridTransport.php b/src/Services/SendGridTransport.php index 3206a5a..fa6c0f7 100644 --- a/src/Services/SendGridTransport.php +++ b/src/Services/SendGridTransport.php @@ -73,8 +73,8 @@ class SendGridTransport implements Swift_Transport * (From SendGrid docs) */ const RESERVED_KEYWORDS = [ - 'x-sg-id', 'x-sg-eid', 'received', 'dkim-signature', 'Content-Type', 'Content-Transfer-Encoding', - 'To', 'From', 'Subject', 'Reply-To', 'CC', 'BCC' + 'X-SG-ID', 'X-SG-EID', 'RECEIVED', 'DKIM-SIGNATURE', 'CONTENT-TYPE', 'CONTENT-TRANSFER-ENCODING', + 'TO', 'FROM', 'SUBJECT', 'REPLY-TO', 'CC', 'BCC' ]; public function __construct(Swift_Events_EventDispatcher $eventDispatcher, $sendGridApiKey, $sendGridCategories) @@ -233,7 +233,7 @@ public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = nul if ($headers = $message->getHeaders()->getAll()) { foreach ($headers as $header) { $headerName = $header->getFieldName(); - if (!in_array($headerName, self::RESERVED_KEYWORDS)) { + if (!in_array(strtoupper($headerName), self::RESERVED_KEYWORDS)) { $mail->addHeader($headerName, $header->getFieldBody()); } } From a49bfbf01ec28c9bdc4f9a0797083cbb7ed98f5b Mon Sep 17 00:00:00 2001 From: Maxime Hermouet Date: Mon, 22 Oct 2018 11:59:33 +0200 Subject: [PATCH 10/13] Fix count error when no 'cc', 'bcc' or 'to' fields are set --- src/Services/SendGridTransport.php | 25 ++++++++++++++++++------- tests/BundleInitializationTest.php | 8 +++++--- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/Services/SendGridTransport.php b/src/Services/SendGridTransport.php index fa6c0f7..03cbf04 100644 --- a/src/Services/SendGridTransport.php +++ b/src/Services/SendGridTransport.php @@ -3,14 +3,14 @@ namespace ExpertCoder\Swiftmailer\SendGridBundle\Services; use finfo; +use Psr\Log\LoggerInterface; use SendGrid; +use Swift_Events_EventDispatcher; use Swift_Events_EventListener; +use Swift_Events_SendEvent; use Swift_Mime_Attachment; use Swift_Mime_SimpleMessage; use Swift_Transport; -use Swift_Events_EventDispatcher; -use Swift_Events_SendEvent; -use Psr\Log\LoggerInterface; class SendGridTransport implements Swift_Transport { @@ -70,11 +70,22 @@ class SendGridTransport implements Swift_Transport /** * Some header keys are reserved. You may not include any of the following reserved keys - * (From SendGrid docs) + * + * @see https://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/errors.html#-Headers-Errors */ const RESERVED_KEYWORDS = [ - 'X-SG-ID', 'X-SG-EID', 'RECEIVED', 'DKIM-SIGNATURE', 'CONTENT-TYPE', 'CONTENT-TRANSFER-ENCODING', - 'TO', 'FROM', 'SUBJECT', 'REPLY-TO', 'CC', 'BCC' + 'X-SG-ID', + 'X-SG-EID', + 'RECEIVED', + 'DKIM-SIGNATURE', + 'CONTENT-TYPE', + 'CONTENT-TRANSFER-ENCODING', + 'TO', + 'FROM', + 'SUBJECT', + 'REPLY-TO', + 'CC', + 'BCC', ]; public function __construct(Swift_Events_EventDispatcher $eventDispatcher, $sendGridApiKey, $sendGridCategories) @@ -260,7 +271,7 @@ public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = nul } if ($evt) { - if ($sent == count($toArr) + count($ccArr) + count($bccArr)) { + if ($sent == count($toArr ?? []) + count($ccArr ?? []) + count($bccArr ?? [])) { $evt->setResult(Swift_Events_SendEvent::RESULT_SUCCESS); } elseif ($sent > 0) { $evt->setResult(Swift_Events_SendEvent::RESULT_TENTATIVE); diff --git a/tests/BundleInitializationTest.php b/tests/BundleInitializationTest.php index 496aaef..b3ac9a2 100644 --- a/tests/BundleInitializationTest.php +++ b/tests/BundleInitializationTest.php @@ -2,10 +2,11 @@ namespace ExpertCoder\Swiftmailer\SendGridBundle\Tests; -use Nyholm\BundleTest\BaseBundleTestCase; -use Nyholm\BundleTest\CompilerPass\PublicServicePass; use ExpertCoder\Swiftmailer\SendGridBundle\ExpertCoderSwiftmailerSendGridBundle; use ExpertCoder\Swiftmailer\SendGridBundle\Services\SendGridTransport; +use Nyholm\BundleTest\BaseBundleTestCase; +use Nyholm\BundleTest\CompilerPass\PublicServicePass; +use Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle; class BundleInitializationTest extends BaseBundleTestCase { @@ -22,6 +23,7 @@ protected function setUp() $this->addCompilerPass(new PublicServicePass('|swiftmailer.mailer.transport.expertcoder_swift_mailer.*|')); // Create a new Kernel $kernel = $this->createKernel(); + $kernel->addBundle(SwiftmailerBundle::class); // Add some configuration $kernel->addConfigFile(__DIR__.'/config_test.yml'); @@ -60,6 +62,6 @@ public function testSimpleMail() $mailer = new \Swift_Mailer($transport); $result = $mailer->send($message); - $this->assertEquals(0, $mailer->send($message)); // This should gives us 0 for no email sent + $this->assertSame(0, $mailer->send($message)); // This should gives us 0 for no email sent } } From cc76077005081bbcf33ed4808f78afc2c0ceffda Mon Sep 17 00:00:00 2001 From: Maxime Hermouet Date: Mon, 22 Oct 2018 12:47:06 +0200 Subject: [PATCH 11/13] Fixes #24 Added a tests section in README file --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index e33d4b7..1d2a66b 100644 --- a/README.md +++ b/README.md @@ -79,6 +79,14 @@ swiftmailer: transport: 'swiftmailer.mailer.transport.expertcoder_swift_mailer.send_grid' ``` +## Tests + +This bundle provide a little helper to launch tests locally and here is a quick explanation: + +* The `config_test.yml` file contains configuration associated with the bundle. +* The `manual-test.sh` helps you test against a specific symfony version and is the main helper to execute tests. You just have to do `sh tests/manual-test.sh versionNumber` where versionNumber is the main Symfony version you want to test (ex: 4) +* The last file contains PhpUnit tests with help of `nyholm/symfony-bundle-test`. Note that we set a timeout of 1sec when sending mail to sendgrid as we do not test the actual sendgrid API with a real key. + ## Important ! Following RFC 1341, section 7.2, if either `text/html` or `text/plain` are to be sent in your email: `text/plain` needs to be first, followed by `text/html`, followed by any other content. From 2cd46b17d9349d96c881101687fd184cc8ceed8b Mon Sep 17 00:00:00 2001 From: Albert Casademont Date: Mon, 12 Aug 2019 19:09:31 +0200 Subject: [PATCH 12/13] Remove Symfony 4.2 deprecation notice (#25) * Remove Symfony 4.2 deprecation notice * Bad reference --- src/DependencyInjection/Configuration.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 472d3b2..f9d8b56 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -18,8 +18,14 @@ class Configuration implements ConfigurationInterface */ public function getConfigTreeBuilder() { - $treeBuilder = new TreeBuilder(); - $rootNode = $treeBuilder->root('expert_coder_swiftmailer_send_grid'); + $treeBuilder = new TreeBuilder('expert_coder_swiftmailer_send_grid'); + + if (method_exists($treeBuilder, 'getRootNode')) { + $rootNode = $treeBuilder->getRootNode(); + } else { + // BC layer for symfony/config 4.1 and older + $rootNode = $treeBuilder->root('expert_coder_swiftmailer_send_grid'); + } $children = $rootNode->isRequired()->fixXmlConfig('category')->children(); $this->configureApiKey($children); From 44047bf4ba7745f24f9267493c6e0737aa43f002 Mon Sep 17 00:00:00 2001 From: Evert Harmeling Date: Thu, 28 Nov 2019 15:11:08 +0100 Subject: [PATCH 13/13] Allow to use Symfony 5 (#26) --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 0758e94..ee4db6d 100644 --- a/composer.json +++ b/composer.json @@ -19,8 +19,8 @@ "sendgrid/sendgrid": "^7.0", "sendgrid/php-http-client": "~3.9.4", "symfony/swiftmailer-bundle": "~3.2", - "symfony/framework-bundle": "~2.7|~3.3|~4.0", - "symfony/yaml": "~2.7|~3.3|~4.0" + "symfony/framework-bundle": "~2.7|~3.3|~4.0|~5.0", + "symfony/yaml": "~2.7|~3.3|~4.0|~5.0" }, "require-dev": { "nyholm/symfony-bundle-test": "^1.3"