/legacy/function.wp_mail.php

https://gitlab.com/itinarraylab/ongage-custom-sender · PHP · 241 lines · 177 code · 30 blank · 34 comment · 39 complexity · 2faf02324371a8e036655a6495d0bb33 MD5 · raw file

  1. <?php
  2. // Compact the input, apply the filters, and extract them back out
  3. extract( apply_filters( 'wp_mail', compact( 'to', 'subject', 'message', 'headers', 'attachments' ) ) );
  4. if ( !is_array($attachments) )
  5. $attachments = explode( "\n", str_replace( "\r\n", "\n", $attachments ) );
  6. global $phpmailer;
  7. // (Re)create it, if it's gone missing
  8. if ( !is_object( $phpmailer ) || !is_a( $phpmailer, 'PHPMailer' ) ) {
  9. require_once ABSPATH . WPINC . '/class-phpmailer.php';
  10. require_once ABSPATH . WPINC . '/class-smtp.php';
  11. $phpmailer = new PHPMailer( true );
  12. }
  13. // Headers
  14. if ( empty( $headers ) ) {
  15. $headers = array();
  16. } else {
  17. if ( !is_array( $headers ) ) {
  18. // Explode the headers out, so this function can take both
  19. // string headers and an array of headers.
  20. $tempheaders = explode( "\n", str_replace( "\r\n", "\n", $headers ) );
  21. } else {
  22. $tempheaders = $headers;
  23. }
  24. $headers = array();
  25. $cc = array();
  26. $bcc = array();
  27. // If it's actually got contents
  28. if ( !empty( $tempheaders ) ) {
  29. // Iterate through the raw headers
  30. foreach ( (array) $tempheaders as $header ) {
  31. if ( strpos($header, ':') === false ) {
  32. if ( false !== stripos( $header, 'boundary=' ) ) {
  33. $parts = preg_split('/boundary=/i', trim( $header ) );
  34. $boundary = trim( str_replace( array( "'", '"' ), '', $parts[1] ) );
  35. }
  36. continue;
  37. }
  38. // Explode them out
  39. list( $name, $content ) = explode( ':', trim( $header ), 2 );
  40. // Cleanup crew
  41. $name = trim( $name );
  42. $content = trim( $content );
  43. switch ( strtolower( $name ) ) {
  44. // Mainly for legacy -- process a From: header if it's there
  45. case 'from':
  46. if ( strpos($content, '<' ) !== false ) {
  47. // So... making my life hard again?
  48. $from_name = substr( $content, 0, strpos( $content, '<' ) - 1 );
  49. $from_name = str_replace( '"', '', $from_name );
  50. $from_name = trim( $from_name );
  51. $from_email = substr( $content, strpos( $content, '<' ) + 1 );
  52. $from_email = str_replace( '>', '', $from_email );
  53. $from_email = trim( $from_email );
  54. } else {
  55. $from_email = trim( $content );
  56. }
  57. break;
  58. case 'content-type':
  59. if ( strpos( $content, ';' ) !== false ) {
  60. list( $type, $charset ) = explode( ';', $content );
  61. $content_type = trim( $type );
  62. if ( false !== stripos( $charset, 'charset=' ) ) {
  63. $charset = trim( str_replace( array( 'charset=', '"' ), '', $charset ) );
  64. } elseif ( false !== stripos( $charset, 'boundary=' ) ) {
  65. $boundary = trim( str_replace( array( 'BOUNDARY=', 'boundary=', '"' ), '', $charset ) );
  66. $charset = '';
  67. }
  68. } else {
  69. $content_type = trim( $content );
  70. }
  71. break;
  72. case 'cc':
  73. $cc = array_merge( (array) $cc, explode( ',', $content ) );
  74. break;
  75. case 'bcc':
  76. $bcc = array_merge( (array) $bcc, explode( ',', $content ) );
  77. break;
  78. default:
  79. // Add it to our grand headers array
  80. $headers[trim( $name )] = trim( $content );
  81. break;
  82. }
  83. }
  84. }
  85. }
  86. // Empty out the values that may be set
  87. $phpmailer->ClearAddresses();
  88. $phpmailer->ClearAllRecipients();
  89. $phpmailer->ClearAttachments();
  90. $phpmailer->ClearBCCs();/* $phpmailer->ClearCCs(); $phpmailer->ClearCustomHeaders(); $phpmailer->ClearReplyTos(); */
  91. // From email and name
  92. // If we don't have a name from the input headers
  93. if ( !isset( $from_name ) )
  94. $from_name = 'WordPress';
  95. /* If we don't have an email from the input headers default to wordpress@$sitename
  96. * Some hosts will block outgoing mail from this address if it doesn't exist but
  97. * there's no easy alternative. Defaulting to admin_email might appear to be another
  98. * option but some hosts may refuse to relay mail from an unknown domain. See
  99. * http://trac.wordpress.org/ticket/5007.
  100. */
  101. if ( !isset( $from_email ) ) {
  102. // Get the site domain and get rid of www.
  103. $sitename = strtolower( $_SERVER['SERVER_NAME'] );
  104. if ( substr( $sitename, 0, 4 ) == 'www.' ) {
  105. $sitename = substr( $sitename, 4 );
  106. }
  107. $from_email = 'wordpress@' . $sitename;
  108. }
  109. // Plugin authors can override the potentially troublesome default
  110. $phpmailer->From = apply_filters( 'wp_mail_from' , $from_email );
  111. $phpmailer->FromName = apply_filters( 'wp_mail_from_name', $from_name );
  112. // Set destination addresses
  113. if ( !is_array( $to ) )
  114. $to = explode( ',', $to );
  115. foreach ( (array) $to as $recipient ) {
  116. try {
  117. // Break $recipient into name and address parts if in the format "Foo <bar@baz.com>"
  118. $recipient_name = '';
  119. if( preg_match( '/(.*)<(.+)>/', $recipient, $matches ) ) {
  120. if ( count( $matches ) == 3 ) {
  121. $recipient_name = $matches[1];
  122. $recipient = $matches[2];
  123. }
  124. }
  125. $phpmailer->AddAddress( $recipient, $recipient_name);
  126. } catch ( phpmailerException $e ) {
  127. continue;
  128. }
  129. }
  130. // Set mail's subject and body
  131. $phpmailer->Subject = $subject;
  132. $phpmailer->Body = $message;
  133. // Add any CC and BCC recipients
  134. if ( !empty( $cc ) ) {
  135. foreach ( (array) $cc as $recipient ) {
  136. try {
  137. // Break $recipient into name and address parts if in the format "Foo <bar@baz.com>"
  138. $recipient_name = '';
  139. if( preg_match( '/(.*)<(.+)>/', $recipient, $matches ) ) {
  140. if ( count( $matches ) == 3 ) {
  141. $recipient_name = $matches[1];
  142. $recipient = $matches[2];
  143. }
  144. }
  145. $phpmailer->AddCc( $recipient, $recipient_name );
  146. } catch ( phpmailerException $e ) {
  147. continue;
  148. }
  149. }
  150. }
  151. if ( !empty( $bcc ) ) {
  152. foreach ( (array) $bcc as $recipient) {
  153. try {
  154. // Break $recipient into name and address parts if in the format "Foo <bar@baz.com>"
  155. $recipient_name = '';
  156. if( preg_match( '/(.*)<(.+)>/', $recipient, $matches ) ) {
  157. if ( count( $matches ) == 3 ) {
  158. $recipient_name = $matches[1];
  159. $recipient = $matches[2];
  160. }
  161. }
  162. $phpmailer->AddBcc( $recipient, $recipient_name );
  163. } catch ( phpmailerException $e ) {
  164. continue;
  165. }
  166. }
  167. }
  168. // Set to use PHP's mail()
  169. $phpmailer->IsMail();
  170. // Set Content-Type and charset
  171. // If we don't have a content-type from the input headers
  172. if ( !isset( $content_type ) )
  173. $content_type = 'text/plain';
  174. $content_type = apply_filters( 'wp_mail_content_type', $content_type );
  175. $phpmailer->ContentType = $content_type;
  176. // Set whether it's plaintext, depending on $content_type
  177. if ( 'text/html' == $content_type )
  178. $phpmailer->IsHTML( true );
  179. // If we don't have a charset from the input headers
  180. if ( !isset( $charset ) )
  181. $charset = get_bloginfo( 'charset' );
  182. // Set the content-type and charset
  183. $phpmailer->CharSet = apply_filters( 'wp_mail_charset', $charset );
  184. // Set custom headers
  185. if ( !empty( $headers ) ) {
  186. foreach( (array) $headers as $name => $content ) {
  187. $phpmailer->AddCustomHeader( sprintf( '%1$s: %2$s', $name, $content ) );
  188. }
  189. if ( false !== stripos( $content_type, 'multipart' ) && ! empty($boundary) )
  190. $phpmailer->AddCustomHeader( sprintf( "Content-Type: %s;\n\t boundary=\"%s\"", $content_type, $boundary ) );
  191. }
  192. if ( !empty( $attachments ) ) {
  193. foreach ( $attachments as $attachment ) {
  194. try {
  195. $phpmailer->AddAttachment($attachment);
  196. } catch ( phpmailerException $e ) {
  197. continue;
  198. }
  199. }
  200. }
  201. do_action_ref_array( 'phpmailer_init', array( &$phpmailer ) );
  202. // Send!
  203. try {
  204. $phpmailer->Send();
  205. } catch ( phpmailerException $e ) {
  206. return false;
  207. }
  208. return true;
  209. ?>