Caldera_Forms_Email_SendGrid

The Caldera Forms Caldera Forms Email SendGrid class.

Defined (1)

The class is defined in the following location(s).

/classes/email/sendgrid.php  
  1. class Caldera_Forms_Email_SendGrid extends Caldera_Forms_Email_Client{ 
  2.  
  3. /** 
  4. * Track emails we are sending to 
  5. *  
  6. * SendGrid will not let you send to same address twice 
  7. *  
  8. * @since 1.4.0 
  9. *  
  10. * @var array 
  11. */ 
  12. protected $sent_to = array(); 
  13.  
  14. /** 
  15. * @inheritdoc 
  16. */ 
  17. public function include_sdk() { 
  18. if( class_exists('SendGrid') ) { 
  19. return; 
  20. include_once __DIR__ . '/sendgrid/sendgrid-php.php'; 
  21.  
  22. /** 
  23. * @inheritdoc 
  24. */ 
  25. public function set_api( array $keys ) { 
  26. $options = array( 
  27. 'raise_exceptions' => true 
  28. ); 
  29.  
  30. if( ! is_ssl() ) { 
  31. $options[ 'turn_off_ssl_verification' ] = true; 
  32.  
  33. $options = apply_filters( 'caldera_forms_sendgrid_options', $options ); 
  34.  
  35. $this->api = new \SendGrid( $keys[0], $options ); 
  36.  
  37. /** 
  38. * @inheritdoc 
  39. */ 
  40. public function send() { 
  41.  
  42. $mail = new Mail(); 
  43.  
  44. $email = $this->create_email( $this->message[ 'from' ], $this->message[ 'from_name' ], false ); 
  45. $mail->setFrom( $email ); 
  46.  
  47. $mail->setSubject( $this->message[ 'subject' ] ); 
  48. $personalization = new \SendGrid\Personalization(); 
  49.  
  50. foreach ( $this->message[ 'recipients' ] as $recipient ) { 
  51.  
  52. if( ! is_email( $recipient ) ) { 
  53. $open = strpos( $recipient, '<' ); 
  54. $close = strpos( $recipient, '>' ); 
  55. $length = strlen( $recipient ); 
  56. if( is_numeric( $open ) && $length - 1 == $close ) { 
  57. $recipient = substr( $recipient, $open + 1, $close); 
  58. $recipient = str_replace( array( '<', '>' ), '', $recipient ); 
  59.  
  60.  
  61. if ( is_email( $recipient ) ) { 
  62. $email = $this->create_email( $recipient ); 
  63. if ( is_object( $email ) ) { 
  64. $personalization->addTo( $email ); 
  65.  
  66. if( ! empty( $this->message[ 'bcc' ] ) && is_email( $this->message[ 'bcc' ] ) ) { 
  67. $email = $this->create_email( $this->message[ 'bcc' ] ); 
  68. if ( is_object( $email ) ) { 
  69. $personalization->addTo( $email ); 
  70.  
  71.  
  72. $mail->addPersonalization( $personalization ); 
  73.  
  74. $content = new Content("text/html", $this->message[ 'message' ] ); 
  75.  
  76. $mail->addContent( $content ); 
  77.  
  78. if( ! empty( $this->attachments ) ) { 
  79. /** @var Caldera_Forms_Email_Attachment $cf_attachment_obj */ 
  80. foreach ( $this->attachments as $cf_attachment_obj ) { 
  81. $attachment = new Attachment(); 
  82. $attachment->setContent( $cf_attachment_obj->get_encoded() ); 
  83. $attachment->setType( $cf_attachment_obj->type ); 
  84. $attachment->setFilename( $cf_attachment_obj->filename ); 
  85. $attachment->setDisposition( 'attachment' ); 
  86. $attachment->setContentId( md5( $cf_attachment_obj->filename ) ); 
  87. $mail->addAttachment( $attachment ); 
  88.  
  89.  
  90. $tracking_settings = new \SendGrid\TrackingSettings(); 
  91. $click_tracking = new ClickTracking(); 
  92. $click_tracking->setEnable( true ); 
  93. $click_tracking->setEnableText( true ); 
  94. $tracking_settings->setClickTracking( $click_tracking ); 
  95. $open_tracking = new OpenTracking(); 
  96. $open_tracking->setEnable(true); 
  97. $tracking_settings->setOpenTracking( $open_tracking ); 
  98. $mail->setTrackingSettings( $tracking_settings ); 
  99.  
  100. if( ! empty( $this->message[ 'replyto' ] ) && is_email( $this->message[ 'replyto' ] ) ) { 
  101. $reply_to = new ReplyTo( $this->message[ 'replyto' ] ); 
  102. $mail->setReplyTo ($reply_to ); 
  103.  
  104.  
  105. /** 
  106. * Modify SendGrid mail object before sending to remote API 
  107. * @since 1.4.0 
  108. * @param \SendGrid\Mail $mail SendGrid SDK email object 
  109. * @param array $message Raw message details 
  110. */ 
  111. $mail = apply_filters( 'caldera_forms_sendgrid_before', $mail, $this->message ); 
  112.  
  113. //@TODO Caldera Exception 
  114. try { 
  115. /** @var \SendGrid\Response $response */ 
  116. $response = $this->api->client->mail()->send()->post( $mail ); 
  117. return $response->statusCode(); 
  118. } catch(\SendGrid\Exception $e) { 
  119. $errors[] = $e->getCode(); 
  120. foreach($e->getErrors() as $er) { 
  121. $errors[] = $er; 
  122. return $errors; 
  123.  
  124. /** 
  125. * Creates SendGrid email object 
  126. *  
  127. * Main reason for this is to prevent adding duplicated send tos, which will cause API error. 
  128. *  
  129. * @since 1.4.0 
  130. *  
  131. * @param string $address Email address to send to. 
  132. * @param string|null $name Optional. Name of who sending to. Default is null. 
  133. * @param bool $prevent_double Optional. If true, prevent duplicates by returing false. Default is true. 
  134. * @return Email 
  135. */ 
  136. protected function create_email( $address, $name = null, $prevent_double = true ) { 
  137. if ( is_email( $address ) ) { 
  138. if ( false == $prevent_double || ! in_array( $address, $this->sent_to ) ) { 
  139. $this->sent_to[] = $address; 
  140. return new Email( $name, $address ); 
  141.  
  142.  
  143.  
  144.  
  145.