From ce4599912ea95f3d20dd2f91191746320b6dc173 Mon Sep 17 00:00:00 2001 From: leigh-ols Date: Thu, 21 Jun 2018 11:58:27 +0100 Subject: [PATCH 1/2] Create composer.json --- composer.json | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 composer.json diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..b4e9382 --- /dev/null +++ b/composer.json @@ -0,0 +1,15 @@ +{ + "name": "sendgrid/sendgrid-email-delivery-simplified", + "description": "", + "type": "wordpress-plugin", + "license": "proprietary", + "authors": [ + { + "name": "Leigh Bicknell", + "email": "leigh@orangeleaf.com" + } + ], + "require": { + "composer/installers": "^1.4" + } +} From c6678cf820859b385f4ff4ec9aca9699af32da8a Mon Sep 17 00:00:00 2001 From: Leigh Bicknell Date: Thu, 21 Jun 2018 12:39:49 +0100 Subject: [PATCH 2/2] Added SMTP retry 3 times and log to /var/log/sendgrid.log on fail --- lib/sendgrid/class-sendgrid-smtp.php | 34 +++++++++++++++++++--------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/lib/sendgrid/class-sendgrid-smtp.php b/lib/sendgrid/class-sendgrid-smtp.php index 6e29df2..ea84b1f 100644 --- a/lib/sendgrid/class-sendgrid-smtp.php +++ b/lib/sendgrid/class-sendgrid-smtp.php @@ -12,6 +12,7 @@ class Sendgrid_SMTP implements Sendgrid_Send { //the list of port instances, to be recycled private $swift_instances = array(); + protected $logger; private $port; private $username; private $password; @@ -51,14 +52,23 @@ public function send( SendGrid\Email $mail ) { $swift = $this->get_swift_instance($this->port); $message = $this->map_to_swift( $mail ); - - try - { - $sent = $swift->send( $message, $failures ); - } - catch(Exception $e) - { - return false; + $count = 0; + $sent = 0; + + while ( !$sent && $count < 3 ) { + $count++; + try + { + $sent = $swift->send( $message, $failures ); + } + catch( Exception $e ) + { + file_put_contents( + '/var/log/sendgrid.log', + date( 'Y-m-d H:i:s' ).' '.$e->getMessage().': '.$mail->getSubject().PHP_EOL.$this->logger->dump().PHP_EOL, + FILE_APPEND | LOCK_EX + ); + } } return ( $sent === 0 ) ? false : true; @@ -75,6 +85,8 @@ private function get_swift_instance( $port ) { $transport->setPassword( $this->password ); $swift = \Swift_Mailer::newInstance( $transport ); + $this->logger = new \Swift_Plugins_Loggers_ArrayLogger(); + $swift->registerPlugin(new \Swift_Plugins_LoggerPlugin($this->logger)); $this->swift_instances[$port] = $swift; } @@ -93,7 +105,7 @@ private function map_to_swift( SendGrid\Email $mail ) { /* * Since we're sending transactional email, we want the message to go to one person at a time, rather * than a bulk send on one message. In order to do this, we'll have to send the list of recipients through the headers - * but Swift still requires a 'to' address. So we'll falsify it with the from address, as it will be + * but Swift still requires a 'to' address. So we'll falsify it with the from address, as it will be * ignored anyway. */ $message->setTo( $mail->to ); @@ -111,7 +123,7 @@ private function map_to_swift( SendGrid\Email $mail ) { if ( ( $replyto = $mail->getReplyTo() ) ) { $message->setReplyTo( $replyto ); } - + $attachments = $mail->getAttachments(); //add any attachments that were added @@ -123,7 +135,7 @@ private function map_to_swift( SendGrid\Email $mail ) { $message_headers = $message->getHeaders(); $message_headers->addTextHeader( "x-smtpapi", $mail->smtpapi->jsonString() ); - + return $message; } }