bb_mail

Send mail, similar to PHP's mail.

Description

bb_mail( (string) $to, (string) $subject, (string) $message, (string) $headers = '', (array) $attachments = array() ); 

A true return value does not automatically mean that the user received the email successfully. It just only means that the method used was able to process the request without any errors.

Using the two bb_mail_from and bb_mail_from_name hooks allow from creating a from address like Name when both are set. If just bb_mail_from is set, then just the email address will be used with no name.

The default content type is text/plain which does not allow using HTML. However, you can set the content type of the email by using the 'bb_mail_content_type' filter.

The default charset is based on the charset used on the blog. The charset can be set using the bb_mail_charset filter.

Parameters (5)

0. $to (string)
Email address to send message
1. $subject (string)
Email subject
2. $message (string)
Message contents
3. $headers — Optional. (string) => ''
Additional headers.
4. $attachments — Optional. (array) => array()
Files to attach.

Usage

  1. if ( !function_exists( 'bb_mail' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-forums/bbpress/bb-includes/functions.bb-pluggable.php'; 
  3.  
  4. // Email address to send message 
  5. $to = ''; 
  6.  
  7. // Email subject 
  8. $subject = ''; 
  9.  
  10. // Message contents 
  11. $message = ''; 
  12.  
  13. // Optional. Additional headers. 
  14. $headers = ''; 
  15.  
  16. // Optional. Files to attach. 
  17. $attachments = array(); 
  18.  
  19. // NOTICE! Understand what this does before running. 
  20. $result = bb_mail($to, $subject, $message, $headers, $attachments); 
  21.  

Defined (1)

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

/bp-forums/bbpress/bb-includes/functions.bb-pluggable.php  
  1. function bb_mail( $to, $subject, $message, $headers = '', $attachments = array() ) { 
  2. // Compact the input, apply the filters, and extract them back out 
  3. extract( apply_filters( 'bb_mail', compact( 'to', 'subject', 'message', 'headers', 'attachments' ) ) ); 
  4.  
  5. if ( !is_array($attachments) ) 
  6. $attachments = explode( "\n", $attachments ); 
  7.  
  8. global $bb_phpmailer; 
  9.  
  10. // (Re)create it, if it's gone missing 
  11. if ( !is_object( $bb_phpmailer ) || !is_a( $bb_phpmailer, 'PHPMailer' ) ) { 
  12. require_once BACKPRESS_PATH . 'class.mailer.php'; 
  13. require_once BACKPRESS_PATH . 'class.mailer-smtp.php'; 
  14. $bb_phpmailer = new PHPMailer(); 
  15.  
  16. // Headers 
  17. if ( empty( $headers ) ) { 
  18. $headers = array(); 
  19. } else { 
  20. if ( !is_array( $headers ) ) { 
  21. // Explode the headers out, so this function can take both 
  22. // string headers and an array of headers. 
  23. $tempheaders = (array) explode( "\n", $headers ); 
  24. } else { 
  25. $tempheaders = $headers; 
  26. $headers = array(); 
  27.  
  28. // If it's actually got contents 
  29. if ( !empty( $tempheaders ) ) { 
  30. // Iterate through the raw headers 
  31. foreach ( (array) $tempheaders as $header ) { 
  32. if ( strpos($header, ':') === false ) { 
  33. if ( false !== stripos( $header, 'boundary=' ) ) { 
  34. $parts = preg_split('/boundary=/i', trim( $header ) ); 
  35. $boundary = trim( str_replace( array( "'", '"' ), '', $parts[1] ) ); 
  36. continue; 
  37. // Explode them out 
  38. list( $name, $content ) = explode( ':', trim( $header ), 2 ); 
  39.  
  40. // Cleanup crew 
  41. $name = trim( $name ); 
  42. $content = trim( $content ); 
  43.  
  44. // Mainly for legacy -- process a From: header if it's there 
  45. if ( 'from' == strtolower($name) ) { 
  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.  
  52. $from_email = substr( $content, strpos( $content, '<' ) + 1 ); 
  53. $from_email = str_replace( '>', '', $from_email ); 
  54. $from_email = trim( $from_email ); 
  55. } else { 
  56. $from_email = trim( $content ); 
  57. } elseif ( 'content-type' == strtolower($name) ) { 
  58. if ( strpos( $content, ';' ) !== false ) { 
  59. list( $type, $charset ) = explode( ';', $content ); 
  60. $content_type = trim( $type ); 
  61. if ( false !== stripos( $charset, 'charset=' ) ) { 
  62. $charset = trim( str_replace( array( 'charset=', '"' ), '', $charset ) ); 
  63. } elseif ( false !== stripos( $charset, 'boundary=' ) ) { 
  64. $boundary = trim( str_replace( array( 'BOUNDARY=', 'boundary=', '"' ), '', $charset ) ); 
  65. $charset = ''; 
  66. } else { 
  67. $content_type = trim( $content ); 
  68. } elseif ( 'cc' == strtolower($name) ) { 
  69. $cc = explode(", ", $content); 
  70. } elseif ( 'bcc' == strtolower($name) ) { 
  71. $bcc = explode(", ", $content); 
  72. } else { 
  73. // Add it to our grand headers array 
  74. $headers[trim( $name )] = trim( $content ); 
  75.  
  76. // Empty out the values that may be set 
  77. $bb_phpmailer->ClearAddresses(); 
  78. $bb_phpmailer->ClearAllRecipients(); 
  79. $bb_phpmailer->ClearAttachments(); 
  80. $bb_phpmailer->ClearBCCs(); 
  81. $bb_phpmailer->ClearCCs(); 
  82. $bb_phpmailer->ClearCustomHeaders(); 
  83. $bb_phpmailer->ClearReplyTos(); 
  84.  
  85. // From email and name 
  86. // If we don't have a name from the input headers 
  87. if ( !isset( $from_name ) ) { 
  88. $from_name = bb_get_option('name'); 
  89.  
  90. // If we don't have an email from the input headers 
  91. if ( !isset( $from_email ) ) { 
  92. $from_email = bb_get_option('from_email'); 
  93.  
  94. // If there is still no email address 
  95. if ( !$from_email ) { 
  96. // Get the site domain and get rid of www. 
  97. $sitename = strtolower( $_SERVER['SERVER_NAME'] ); 
  98. if ( substr( $sitename, 0, 4 ) == 'www.' ) { 
  99. $sitename = substr( $sitename, 4 ); 
  100.  
  101. $from_email = 'bbpress@' . $sitename; 
  102.  
  103. // Plugin authors can override the potentially troublesome default 
  104. $bb_phpmailer->From = apply_filters( 'bb_mail_from', $from_email ); 
  105. $bb_phpmailer->FromName = apply_filters( 'bb_mail_from_name', $from_name ); 
  106.  
  107. // Set destination address 
  108. $bb_phpmailer->AddAddress( $to ); 
  109.  
  110. // Set mail's subject and body 
  111. $bb_phpmailer->Subject = $subject; 
  112. $bb_phpmailer->Body = $message; 
  113.  
  114. // Add any CC and BCC recipients 
  115. if ( !empty($cc) ) { 
  116. foreach ( (array) $cc as $recipient ) { 
  117. $bb_phpmailer->AddCc( trim($recipient) ); 
  118. if ( !empty($bcc) ) { 
  119. foreach ( (array) $bcc as $recipient) { 
  120. $bb_phpmailer->AddBcc( trim($recipient) ); 
  121.  
  122. // Set to use PHP's mail() 
  123. $bb_phpmailer->IsMail(); 
  124.  
  125. // Set Content-Type and charset 
  126. // If we don't have a content-type from the input headers 
  127. if ( !isset( $content_type ) ) { 
  128. $content_type = 'text/plain'; 
  129.  
  130. $content_type = apply_filters( 'bb_mail_content_type', $content_type ); 
  131.  
  132. $bb_phpmailer->ContentType = $content_type; 
  133.  
  134. // Set whether it's plaintext or not, depending on $content_type 
  135. if ( $content_type == 'text/html' ) { 
  136. $bb_phpmailer->IsHTML( true ); 
  137.  
  138. // If we don't have a charset from the input headers 
  139. if ( !isset( $charset ) ) { 
  140. $charset = bb_get_option( 'charset' ); 
  141.  
  142. // Set the content-type and charset 
  143. $bb_phpmailer->CharSet = apply_filters( 'bb_mail_charset', $charset ); 
  144.  
  145. // Set custom headers 
  146. if ( !empty( $headers ) ) { 
  147. foreach( (array) $headers as $name => $content ) { 
  148. $bb_phpmailer->AddCustomHeader( sprintf( '%1$s: %2$s', $name, $content ) ); 
  149. if ( false !== stripos( $content_type, 'multipart' ) && ! empty($boundary) ) { 
  150. $bb_phpmailer->AddCustomHeader( sprintf( "Content-Type: %s;\n\t boundary=\"%s\"", $content_type, $boundary ) ); 
  151.  
  152. if ( !empty( $attachments ) ) { 
  153. foreach ( $attachments as $attachment ) { 
  154. $bb_phpmailer->AddAttachment($attachment); 
  155.  
  156. do_action_ref_array( 'bb_phpmailer_init', array( &$bb_phpmailer ) ); 
  157.  
  158. // Send! 
  159. $result = @$bb_phpmailer->Send(); 
  160.  
  161. return $result;