summaryrefslogtreecommitdiff
path: root/Postman/Postman-Mail/PostmanSendGridMailEngine.php
diff options
context:
space:
mode:
authoryehudah <yehudah@b8457f37-d9ea-0310-8a92-e5e31aec5664>2018-03-04 22:43:20 +0000
committeryehudah <yehudah@b8457f37-d9ea-0310-8a92-e5e31aec5664>2018-03-04 22:43:20 +0000
commitbb0d1245239c19a8554794e7463635a1bd2eba76 (patch)
tree194e4d3ee68d587caec49a4c4672d4c57c2303a0 /Postman/Postman-Mail/PostmanSendGridMailEngine.php
parentf0ad4b1beec2b3354b028122bc89381a184ca89a (diff)
downloadPost-SMTP-bb0d1245239c19a8554794e7463635a1bd2eba76.zip
* sendgrid 6
* mail params * email log send to column * hostname insted of ip in emails * remove mailgun test folder ( virustotal issue) * added filters to from_name and from_email filters (local connection) * change hostname extrect loginc git-svn-id: https://plugins.svn.wordpress.org/post-smtp/trunk@1833308 b8457f37-d9ea-0310-8a92-e5e31aec5664
Diffstat (limited to 'Postman/Postman-Mail/PostmanSendGridMailEngine.php')
-rw-r--r--Postman/Postman-Mail/PostmanSendGridMailEngine.php136
1 files changed, 85 insertions, 51 deletions
diff --git a/Postman/Postman-Mail/PostmanSendGridMailEngine.php b/Postman/Postman-Mail/PostmanSendGridMailEngine.php
index 524955a..de6e5c3 100644
--- a/Postman/Postman-Mail/PostmanSendGridMailEngine.php
+++ b/Postman/Postman-Mail/PostmanSendGridMailEngine.php
@@ -1,7 +1,8 @@
<?php
+
if ( ! class_exists( 'PostmanSendGridMailEngine' ) ) {
- require_once 'sendgrid-php-3.2.0/sendgrid-php.php';
+ require_once 'sendgrid/sendgrid-php.php';
/**
* Sends mail with the SendGrid API
@@ -17,7 +18,7 @@ if ( ! class_exists( 'PostmanSendGridMailEngine' ) ) {
// the result
private $transcript;
- private $email;
+ private $personalization;
private $apiKey;
/**
@@ -33,7 +34,7 @@ if ( ! class_exists( 'PostmanSendGridMailEngine' ) ) {
$this->logger = new PostmanLogger( get_class( $this ) );
// create the Message
- $this->email = new SendGrid\Email();
+ $this->personalization = new SendGrid\Personalization();
}
/**
@@ -47,100 +48,111 @@ if ( ! class_exists( 'PostmanSendGridMailEngine' ) ) {
// add the Postman signature - append it to whatever the user may have set
if ( ! $options->isStealthModeEnabled() ) {
$pluginData = apply_filters( 'postman_get_plugin_metadata', null );
- $this->email->addHeader( 'X-Mailer', sprintf( 'Postman SMTP %s for WordPress (%s)', $pluginData ['version'], 'https://wordpress.org/plugins/post-smtp/' ) );
+ $this->personalization->addHeader( 'X-Mailer', sprintf( 'Postman SMTP %s for WordPress (%s)', $pluginData ['version'], 'https://wordpress.org/plugins/post-smtp/' ) );
}
// add the headers - see http://framework.zend.com/manual/1.12/en/zend.mail.additional-headers.html
foreach ( ( array ) $message->getHeaders() as $header ) {
$this->logger->debug( sprintf( 'Adding user header %s=%s', $header ['name'], $header ['content'] ) );
- $this->email->addHeader( $header ['name'], $header ['content'] );
+ $this->personalization->addHeader( $header ['name'], $header ['content'] );
}
// if the caller set a Content-Type header, use it
$contentType = $message->getContentType();
if ( ! empty( $contentType ) ) {
- $this->logger->debug( 'Adding content-type ' . $contentType );
- $this->email->addHeader( 'Content-Type', $contentType );
+ $this->logger->debug( 'Some header keys are reserved. You may not include any of the following reserved headers: x-sg-id, x-sg-eid, received, dkim-signature, Content-Type, Content-Transfer-Encoding, To, From, Subject, Reply-To, CC, BCC.' );
}
// add the From Header
$sender = $message->getFromAddress();
- {
- $senderEmail = $sender->getEmail();
- $senderName = $sender->getName();
- assert( ! empty( $senderEmail ) );
- $this->email->setFrom( $senderEmail );
- if ( ! empty( $senderName ) ) {
- $this->email->setFromName( $senderName );
- }
- // now log it
- $sender->log( $this->logger, 'From' );
- }
- // add the Sender Header, overriding what the user may have set
- $this->email->addHeader( 'Sender', $options->getEnvelopeSender() );
+ $senderEmail = ! empty( $sender->getEmail() ) ? $sender->getEmail() : $options->getMessageSenderEmail();
+ $senderName = ! empty( $sender->getName() ) ? $sender->getName() : $options->getMessageSenderName();
+
+ $from = new SendGrid\Email( $senderName, $senderEmail );
+
+ // now log it
+ $sender->log( $this->logger, 'From' );
// add the to recipients
+ $counter = 0;
foreach ( ( array ) $message->getToRecipients() as $recipient ) {
$recipient->log( $this->logger, 'To' );
- $this->email->addTo( $recipient->getEmail(), $recipient->getName() );
+ if ( $counter == 0 ) {
+ $to = new SendGrid\Email($recipient->getName(), $recipient->getEmail());
+ $this->personalization->addTo( $to );
+ } else {
+ $email = new SendGrid\Email($recipient->getName(), $recipient->getEmail());
+ $this->personalization->addTo( $email );
+ }
+
+ $counter++;
}
// add the cc recipients
foreach ( ( array ) $message->getCcRecipients() as $recipient ) {
$recipient->log( $this->logger, 'Cc' );
- $this->email->addCc( $recipient->getEmail(), $recipient->getName() );
+ $this->personalization->addCc( $recipient->getEmail(), $recipient->getName() );
}
// add the bcc recipients
foreach ( ( array ) $message->getBccRecipients() as $recipient ) {
$recipient->log( $this->logger, 'Bcc' );
- $this->email->addBcc( $recipient->getEmail(), $recipient->getName() );
- }
-
- // add the reply-to
- $replyTo = $message->getReplyTo();
- // $replyTo is null or a PostmanEmailAddress object
- if ( isset( $replyTo ) ) {
- $this->email->setReplyTo( $replyTo->format() );
- }
-
- // add the date
- $date = $message->getDate();
- if ( ! empty( $date ) ) {
- $this->email->setDate( $message->getDate() );
+ $this->personalization->addBcc( $recipient->getEmail(), $recipient->getName() );
}
// add the messageId
$messageId = $message->getMessageId();
if ( ! empty( $messageId ) ) {
- $this->email->addHeader( 'message-id', $messageId );
+ $this->personalization->addHeader( 'message-id', $messageId );
}
// add the subject
if ( null !== $message->getSubject() ) {
- $this->email->setSubject( $message->getSubject() );
+ $subject = $message->getSubject();
}
// add the message content
- {
- $textPart = $message->getBodyTextPart();
+
+ $textPart = $message->getBodyTextPart();
if ( ! empty( $textPart ) ) {
$this->logger->debug( 'Adding body as text' );
- $this->email->setText( $textPart );
+ $content = new SendGrid\Content("text/plain", $textPart);
}
- $htmlPart = $message->getBodyHtmlPart();
+
+ $htmlPart = $message->getBodyHtmlPart();
if ( ! empty( $htmlPart ) ) {
$this->logger->debug( 'Adding body as html' );
- $this->email->setHtml( $htmlPart );
- }
+ $content = new SendGrid\Content("text/html", $htmlPart);
}
// add attachments
$this->logger->debug( 'Adding attachments' );
- $this->addAttachmentsToMail( $message );
- $result = array();
+ $mail = new SendGrid\Mail($from, $subject, $to, $content);
+ $mail->addPersonalization($this->personalization);
+
+
+ // add the reply-to
+ $replyTo = $message->getReplyTo();
+ // $replyTo is null or a PostmanEmailAddress object
+ if ( isset( $replyTo ) ) {
+ $reply_to = new SendGrid\ReplyTo( $replyTo->getEmail(), $replyTo->getName() );
+ $mail->setReplyTo($reply_to);
+ }
+
+ $attachments = $this->addAttachmentsToMail( $message );
+
+ foreach ( $attachments as $index => $attachment ) {
+ $attach = new SendGrid\Attachment();
+ $attach->setContent($attachment['content']);
+ $attach->setType($attachment['type']);
+ $attach->setFilename($attachment['file_name']);
+ $attach->setDisposition("attachment");
+ $attach->setContentId($attachment['id']);
+ $mail->addAttachment($attach);
+ }
+
try {
if ( $this->logger->isDebug() ) {
@@ -152,17 +164,27 @@ if ( ! class_exists( 'PostmanSendGridMailEngine' ) ) {
if ( $this->logger->isDebug() ) {
$this->logger->debug( 'Sending mail' );
}
- $result = $sendgrid->send( $this->email );
+
+ $response = $sendgrid->client->mail()->send()->post($mail);
if ( $this->logger->isInfo() ) {
$this->logger->info( );
}
- $this->transcript = print_r( $result, true );
+
+ $response_body = json_decode( $response->body() );
+
+ if ( isset( $response_body->errors[0]->message ) ) {
+ $this->transcript = $response_body->errors[0]->message;
+ $this->transcript .= PostmanModuleTransport::RAW_MESSAGE_FOLLOWS;
+ $this->transcript .= print_r( $mail, true );
+ throw new Exception( $response_body->errors[0]->message );
+ }
+ $this->transcript = print_r( $response->body(), true );
$this->transcript .= PostmanModuleTransport::RAW_MESSAGE_FOLLOWS;
- $this->transcript .= print_r( $this->email, true );
+ $this->transcript .= print_r( $mail, true );
} catch ( SendGrid\Exception $e ) {
$this->transcript = $e->getMessage();
$this->transcript .= PostmanModuleTransport::RAW_MESSAGE_FOLLOWS;
- $this->transcript .= print_r( $this->email, true );
+ $this->transcript .= print_r( $mail, true );
throw $e;
}
}
@@ -181,12 +203,24 @@ if ( ! class_exists( 'PostmanSendGridMailEngine' ) ) {
$attArray = $attachments;
}
// otherwise WordPress sends an array
+ $attachments = array();
foreach ( $attArray as $file ) {
if ( ! empty( $file ) ) {
$this->logger->debug( 'Adding attachment: ' . $file );
- $this->email->addAttachment( $file );
+
+ $file_name = basename( $file );
+ $file_parts = explode( '.', $file_name );
+ $attachments[] = array(
+ 'content' => base64_encode( file_get_contents( $file ) ),
+ 'type' => mime_content_type( $file ),
+ 'file_name' => $file_name,
+ 'disposition' => 'attachment',
+ 'id' => $file_parts[0],
+ );
}
}
+
+ return $attachments;
}
// return the SMTP session transcript