PageRenderTime 41ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/wp-sendgrid/includes/wp-mail.php

https://gitlab.com/gregtyka/helloworld1234
PHP | 222 lines | 130 code | 22 blank | 70 comment | 28 complexity | d49e543e81c3f12f608f133c65faf892 MD5 | raw file
  1. <?php
  2. /**
  3. * Send mail, similar to PHP's mail
  4. *
  5. * A true return value does not automatically mean that the user received the
  6. * email successfully. It just only means that the method used was able to
  7. * process the request without any errors.
  8. *
  9. * Using the two 'wp_mail_from' and 'wp_mail_from_name' hooks allow from
  10. * creating a from address like 'Name <email@address.com>' when both are set. If
  11. * just 'wp_mail_from' is set, then just the email address will be used with no
  12. * name.
  13. *
  14. * The default content type is 'text/plain' which does not allow using HTML.
  15. * However, you can set the content type of the email by using the
  16. * 'wp_mail_content_type' filter.
  17. *
  18. * The default charset is based on the charset used on the blog. The charset can
  19. * be set using the 'wp_mail_charset' filter.
  20. *
  21. * @since 1.2.1
  22. * @uses apply_filters() Calls 'wp_mail' hook on an array of all of the parameters.
  23. * @uses apply_filters() Calls 'wp_mail_from' hook to get the from email address.
  24. * @uses apply_filters() Calls 'wp_mail_from_name' hook to get the from address name.
  25. * @uses apply_filters() Calls 'wp_mail_content_type' hook to get the email content type.
  26. * @uses apply_filters() Calls 'wp_mail_charset' hook to get the email charset
  27. *
  28. * @param string|array $to Array or comma-separated list of email addresses to send message.
  29. * @param string $subject Email subject
  30. * @param string $message Message contents
  31. * @param string|array $headers Optional. Additional headers.
  32. * @param string|array $attachments Optional. Files to attach.
  33. * @return bool Whether the email contents were sent successfully.
  34. */
  35. function wp_mail( $to, $subject, $message, $headers = '', $attachments = array() ) {
  36. $options = WP_SendGrid_Settings::get_settings();
  37. // Compact the input, apply the filters, and extract them back out
  38. extract( apply_filters( 'wp_mail', compact( 'to', 'subject', 'message', 'headers', 'attachments' ) ) );
  39. // If message just has \n SendGrid doesn't get newlines
  40. // Might only be a problem on Windows systems
  41. $message = str_replace( "\n", "\r\n", $message );
  42. if ( !is_array($attachments) )
  43. $attachments = explode( "\n", str_replace( "\r\n", "\n", $attachments ) );
  44. // Headers
  45. if ( empty( $headers ) ) {
  46. $headers = array();
  47. } else {
  48. if ( !is_array( $headers ) ) {
  49. // Explode the headers out, so this function can take both
  50. // string headers and an array of headers.
  51. $tempheaders = explode( "\n", str_replace( "\r\n", "\n", $headers ) );
  52. } else {
  53. $tempheaders = $headers;
  54. }
  55. $headers = array();
  56. $cc = array();
  57. $bcc = array();
  58. // If it's actually got contents
  59. if ( !empty( $tempheaders ) ) {
  60. // Iterate through the raw headers
  61. foreach ( (array) $tempheaders as $header ) {
  62. if ( strpos($header, ':') === false ) {
  63. if ( false !== stripos( $header, 'boundary=' ) ) {
  64. $parts = preg_split('/boundary=/i', trim( $header ) );
  65. $boundary = trim( str_replace( array( "'", '"' ), '', $parts[1] ) );
  66. }
  67. continue;
  68. }
  69. // Explode them out
  70. list( $name, $content ) = explode( ':', trim( $header ), 2 );
  71. // Cleanup crew
  72. $name = trim( $name );
  73. $content = trim( $content );
  74. switch ( strtolower( $name ) ) {
  75. // Mainly for legacy -- process a From: header if it's there
  76. case 'from':
  77. if ( strpos($content, '<' ) !== false ) {
  78. // So... making my life hard again?
  79. $from_name = substr( $content, 0, strpos( $content, '<' ) - 1 );
  80. $from_name = str_replace( '"', '', $from_name );
  81. $from_name = trim( $from_name );
  82. $from_email = substr( $content, strpos( $content, '<' ) + 1 );
  83. $from_email = str_replace( '>', '', $from_email );
  84. $from_email = trim( $from_email );
  85. } else {
  86. $from_email = trim( $content );
  87. }
  88. break;
  89. case 'content-type':
  90. if ( strpos( $content, ';' ) !== false ) {
  91. list( $type, $charset ) = explode( ';', $content );
  92. $content_type = trim( $type );
  93. if ( false !== stripos( $charset, 'charset=' ) ) {
  94. $charset = trim( str_replace( array( 'charset=', '"' ), '', $charset ) );
  95. } elseif ( false !== stripos( $charset, 'boundary=' ) ) {
  96. $boundary = trim( str_replace( array( 'BOUNDARY=', 'boundary=', '"' ), '', $charset ) );
  97. $charset = '';
  98. }
  99. } else {
  100. $content_type = trim( $content );
  101. }
  102. break;
  103. case 'cc':
  104. $cc = array_merge( (array) $cc, explode( ',', $content ) );
  105. break;
  106. case 'bcc':
  107. $bcc = array_merge( (array) $bcc, explode( ',', $content ) );
  108. break;
  109. default:
  110. // Add it to our grand headers array
  111. $headers[trim( $name )] = trim( $content );
  112. break;
  113. }
  114. }
  115. }
  116. }
  117. // From email and name
  118. // If we don't have a name from the input headers
  119. if ( !isset( $from_name ) )
  120. $from_name = 'WordPress';
  121. /* If we don't have an email from the input headers default to wordpress@$sitename
  122. * Some hosts will block outgoing mail from this address if it doesn't exist but
  123. * there's no easy alternative. Defaulting to admin_email might appear to be another
  124. * option but some hosts may refuse to relay mail from an unknown domain. See
  125. * http://trac.wordpress.org/ticket/5007.
  126. */
  127. if ( !isset( $from_email ) ) {
  128. // Get the site domain and get rid of www.
  129. $sitename = strtolower( $_SERVER['SERVER_NAME'] );
  130. if ( substr( $sitename, 0, 4 ) == 'www.' ) {
  131. $sitename = substr( $sitename, 4 );
  132. }
  133. $from_email = 'wordpress@' . $sitename;
  134. }
  135. // Plugin authors can override the potentially troublesome default
  136. $from_email = apply_filters( 'wp_mail_from' , $from_email );
  137. $from_name = apply_filters( 'wp_mail_from_name', $from_name );
  138. // Set destination addresses
  139. if ( !is_array( $to ) )
  140. $to = explode( ',', $to );
  141. // Set Content-Type and charset
  142. // If we don't have a content-type from the input headers
  143. if ( !isset( $content_type ) )
  144. $content_type = 'text/plain';
  145. $content_type = apply_filters( 'wp_mail_content_type', $content_type );
  146. /* TODO
  147. // Set whether it's plaintext, depending on $content_type
  148. $phpmailer->IsHTML( true ); */
  149. // TODO: I don't know that SendGrid supports different charsets. Shouldn't be a problem in most cases
  150. /*
  151. * // If we don't have a charset from the input headers
  152. * if ( !isset( $charset ) )
  153. * $charset = get_bloginfo( 'charset' );
  154. *
  155. *
  156. * // Set the content-type and charset
  157. * $charset = apply_filters( 'wp_mail_charset', $charset );
  158. */
  159. $attachment_array = array();
  160. if ( !empty( $attachments ) ) {
  161. if ( is_string( $attachments ) ) {
  162. $attachments = preg_split( '/,\s*/', $attachments );
  163. }
  164. if ( is_array( $attachments) ) {
  165. foreach ( $attachments as $attachment ) {
  166. if ( file_exists( $attachment ) ) {
  167. $contents = file_get_contents( $attachment );
  168. $file = basename( $attachment );
  169. $attachment_array[$file] = $contents;
  170. }
  171. }
  172. }
  173. }
  174. $args = array(
  175. 'to' => $to,
  176. 'subject' => $subject,
  177. 'text' => $message,
  178. 'api_user' => $options['username'],
  179. 'api_key' => $options['password'],
  180. 'files' => $attachment_array,
  181. 'from' => $from_email,
  182. 'fromname' => $from_name,
  183. );
  184. $xsmtpapi = apply_filters( 'wp_sendgrid_xsmtpapi', array(), $to, $subject, $message, $headers, $attachment_array );
  185. if ( !empty( $xsmtpapi ) ) {
  186. $args['x-smtpapi'] = json_encode( $xsmtpapi );
  187. }
  188. if ( 'text/plain' !== $content_type ) {
  189. $args['html'] = $message;
  190. }
  191. $args = apply_filters( 'wp_sendgrid_args', $args, $to, $subject, $message, $headers, $attachment_array );
  192. $response = json_decode( wp_remote_retrieve_body( wp_remote_post( 'https://sendgrid.com/api/mail.send.json', array( 'sslverify' => false, 'body' => $args ) ) ) );
  193. if ( isset( $response->message ) && $response->message == 'success' ) {
  194. return true;
  195. }
  196. return false;
  197. }