PageRenderTime 29ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/wp-mail-smtp/src/Providers/Gmail/Mailer.php

https://bitbucket.org/RickCalder/durawp_new
PHP | 209 lines | 106 code | 38 blank | 65 comment | 14 complexity | 8b9b468f57ca13d8f691cfcdef5691f4 MD5 | raw file
Possible License(s): GPL-3.0, BSD-3-Clause, Apache-2.0, 0BSD
  1. <?php
  2. namespace WPMailSMTP\Providers\Gmail;
  3. use WPMailSMTP\Debug;
  4. use WPMailSMTP\MailCatcher;
  5. use WPMailSMTP\Providers\MailerAbstract;
  6. /**
  7. * Class Mailer.
  8. *
  9. * @since 1.0.0
  10. */
  11. class Mailer extends MailerAbstract {
  12. /**
  13. * URL to make an API request to.
  14. * Not used for Gmail, as we are using its API.
  15. *
  16. * @since 1.0.0
  17. *
  18. * @var string
  19. */
  20. protected $url = 'https://www.googleapis.com/upload/gmail/v1/users/{userId}/messages/send';
  21. /**
  22. * Gmail message.
  23. *
  24. * @since 1.0.0
  25. *
  26. * @var \Google_Service_Gmail_Message
  27. */
  28. protected $message;
  29. /**
  30. * Mailer constructor.
  31. *
  32. * @since 1.0.0
  33. *
  34. * @param \WPMailSMTP\MailCatcher $phpmailer
  35. */
  36. public function __construct( $phpmailer ) {
  37. parent::__construct( $phpmailer );
  38. if ( ! $this->is_php_compatible() ) {
  39. return;
  40. }
  41. }
  42. /**
  43. * Re-use the MailCatcher class methods and properties.
  44. *
  45. * @since 1.2.0
  46. *
  47. * @param \WPMailSMTP\MailCatcher $phpmailer
  48. */
  49. public function process_phpmailer( $phpmailer ) {
  50. // Make sure that we have access to MailCatcher class methods.
  51. if (
  52. ! $phpmailer instanceof MailCatcher &&
  53. ! $phpmailer instanceof \PHPMailer
  54. ) {
  55. return;
  56. }
  57. $this->phpmailer = $phpmailer;
  58. }
  59. /**
  60. * Use Google API Services to send emails.
  61. *
  62. * @since 1.0.0
  63. */
  64. public function send() {
  65. // Include the Google library.
  66. require_once wp_mail_smtp()->plugin_path . '/vendor/autoload.php';
  67. $auth = new Auth();
  68. $message = new \Google_Service_Gmail_Message();
  69. // Get the raw MIME email using \MailCatcher data.
  70. // We need here to make base64URL-safe string.
  71. $base64 = str_replace(
  72. array( '+', '/', '=' ),
  73. array( '-', '_', '' ),
  74. base64_encode( $this->phpmailer->getSentMIMEMessage() )
  75. );
  76. $message->setRaw( $base64 );
  77. $service = new \Google_Service_Gmail( $auth->get_client() );
  78. try {
  79. $response = $service->users_messages->send( 'me', $message );
  80. $this->process_response( $response );
  81. } catch ( \Exception $e ) {
  82. Debug::set(
  83. 'Mailer: Gmail' . "\r\n" .
  84. $e->getMessage()
  85. );
  86. return;
  87. }
  88. }
  89. /**
  90. * Save response from the API to use it later.
  91. *
  92. * @since 1.0.0
  93. * @since 1.5.0 Added action "wp_mail_smtp_providers_gmail_mailer_process_response" with $response.
  94. *
  95. * @param \Google_Service_Gmail_Message $response
  96. */
  97. protected function process_response( $response ) {
  98. $this->response = $response;
  99. do_action( 'wp_mail_smtp_providers_gmail_mailer_process_response', $this->response, $this->phpmailer );
  100. }
  101. /**
  102. * Check whether the email was sent.
  103. *
  104. * @since 1.0.0
  105. *
  106. * @return bool
  107. */
  108. public function is_email_sent() {
  109. $is_sent = false;
  110. if ( method_exists( $this->response, 'getId' ) ) {
  111. $message_id = $this->response->getId();
  112. if ( ! empty( $message_id ) ) {
  113. $is_sent = true;
  114. }
  115. }
  116. // Clear debug messages if email is successfully sent.
  117. if ( $is_sent ) {
  118. Debug::clear();
  119. }
  120. return $is_sent;
  121. }
  122. /**
  123. * @inheritdoc
  124. */
  125. public function get_debug_info() {
  126. $gmail_text = array();
  127. $options = new \WPMailSMTP\Options();
  128. $gmail = $options->get_group( 'gmail' );
  129. $curl_ver = 'No';
  130. if ( function_exists( 'curl_version' ) ) {
  131. $curl = curl_version(); // phpcs:ignore
  132. $curl_ver = $curl['version'];
  133. }
  134. $gmail_text[] = '<strong>Client ID/Secret:</strong> ' . ( ! empty( $gmail['client_id'] ) && ! empty( $gmail['client_secret'] ) ? 'Yes' : 'No' );
  135. $gmail_text[] = '<strong>Auth Code:</strong> ' . ( ! empty( $gmail['auth_code'] ) ? 'Yes' : 'No' );
  136. $gmail_text[] = '<strong>Access Token:</strong> ' . ( ! empty( $gmail['access_token'] ) ? 'Yes' : 'No' );
  137. $gmail_text[] = '<br><strong>Server:</strong>';
  138. $gmail_text[] = '<strong>OpenSSL:</strong> ' . ( extension_loaded( 'openssl' ) && defined( 'OPENSSL_VERSION_TEXT' ) ? OPENSSL_VERSION_TEXT : 'No' );
  139. $gmail_text[] = '<strong>PHP.allow_url_fopen:</strong> ' . ( ini_get( 'allow_url_fopen' ) ? 'Yes' : 'No' );
  140. $gmail_text[] = '<strong>PHP.stream_socket_client():</strong> ' . ( function_exists( 'stream_socket_client' ) ? 'Yes' : 'No' );
  141. $gmail_text[] = '<strong>PHP.fsockopen():</strong> ' . ( function_exists( 'fsockopen' ) ? 'Yes' : 'No' );
  142. $gmail_text[] = '<strong>PHP.curl_version():</strong> ' . $curl_ver; // phpcs:ignore
  143. if ( function_exists( 'apache_get_modules' ) ) {
  144. $modules = apache_get_modules();
  145. $gmail_text[] = '<strong>Apache.mod_security:</strong> ' . ( in_array( 'mod_security', $modules, true ) || in_array( 'mod_security2', $modules, true ) ? 'Yes' : 'No' );
  146. }
  147. if ( function_exists( 'selinux_is_enabled' ) ) {
  148. $gmail_text[] = '<strong>OS.SELinux:</strong> ' . ( selinux_is_enabled() ? 'Yes' : 'No' );
  149. }
  150. if ( function_exists( 'grsecurity_is_enabled' ) ) {
  151. $gmail_text[] = '<strong>OS.grsecurity:</strong> ' . ( grsecurity_is_enabled() ? 'Yes' : 'No' );
  152. }
  153. return implode( '<br>', $gmail_text );
  154. }
  155. /**
  156. * @inheritdoc
  157. */
  158. public function is_mailer_complete() {
  159. if ( ! $this->is_php_compatible() ) {
  160. return false;
  161. }
  162. $auth = new Auth();
  163. if (
  164. $auth->is_clients_saved() &&
  165. ! $auth->is_auth_required()
  166. ) {
  167. return true;
  168. }
  169. return false;
  170. }
  171. }