Skip to content
This repository was archived by the owner on May 6, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "sendgrid/sendgrid-email-delivery-simplified",
"description": "",
"type": "wordpress-plugin",
"license": "proprietary",
"authors": [
{
"name": "Leigh Bicknell",
"email": "[email protected]"
}
],
"require": {
"composer/installers": "^1.4"
}
}
34 changes: 23 additions & 11 deletions lib/sendgrid/class-sendgrid-smtp.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -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 );
Expand All @@ -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
Expand All @@ -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;
}
}