bp_send_email

Send email, similar to WordPress' wp_mail().

Description

(bool|WP_Error) bp_send_email( (string) $email_type, (string|array|int|WP_User) $to, (array) $args = 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.

Returns (bool|WP_Error)

True if the email was sent successfully. Otherwise, a WP_Error object describing why the email failed to send. The contents will vary based on the email delivery class you are using.

Parameters (3)

0. $email_type (string)
Type of email being sent.
1. $to (string|array|int|WP_User)
Either a email address, user ID, WP_User object, or an array containg the address and name.
2. $args — Optional. (array) => array()
Array of extra parameters.

Options

    ;        


    Usage

    1. if ( !function_exists( 'bp_send_email' ) ) { 
    2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-core/bp-core-functions.php'; 
    3.  
    4. // Type of email being sent. 
    5. $email_type = ''; 
    6.  
    7. // Either a email address, user ID, WP_User object, 
    8. // or an array containg the address and name. 
    9. $to = null; 
    10.  
    11. // Optional. Array of extra parameters. 
    12. $args = array(); 
    13.  
    14. // NOTICE! Understand what this does before running. 
    15. $result = bp_send_email($email_type, $to, $args); 
    16.  

    Defined (1)

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

    /bp-core/bp-core-functions.php  
    1. function bp_send_email( $email_type, $to, $args = array() ) { 
    2. static $is_default_wpmail = null; 
    3. static $wp_html_emails = null; 
    4.  
    5. // Has wp_mail() been filtered to send HTML emails? 
    6. if ( is_null( $wp_html_emails ) ) { 
    7. /** This filter is documented in wp-includes/pluggable.php */ 
    8. $wp_html_emails = apply_filters( 'wp_mail_content_type', 'text/plain' ) === 'text/html'; 
    9.  
    10. // Since wp_mail() is a pluggable function, has it been re-defined by another plugin? 
    11. if ( is_null( $is_default_wpmail ) ) { 
    12. try { 
    13. $mirror = new ReflectionFunction( 'wp_mail' ); 
    14. $is_default_wpmail = substr( $mirror->getFileName(), -strlen( 'pluggable.php' ) ) === 'pluggable.php'; 
    15. } catch ( Exception $e ) { 
    16. $is_default_wpmail = true; 
    17.  
    18. $args = bp_parse_args( $args, array( 
    19. 'tokens' => array(),  
    20. ), 'send_email' ); 
    21.  
    22.  
    23. /** 
    24. * Build the email. 
    25. */ 
    26.  
    27. $email = bp_get_email( $email_type ); 
    28. if ( is_wp_error( $email ) ) { 
    29. return $email; 
    30.  
    31. // From, subject, content are set automatically. 
    32. $email->set_to( $to ); 
    33. $email->set_tokens( $args['tokens'] ); 
    34.  
    35. /** 
    36. * Gives access to an email before it is sent. 
    37. * @since 2.8.0 
    38. * @param BP_Email $email The email (object) about to be sent. 
    39. * @param string $email_type Type of email being sent. 
    40. * @param string|array|int|WP_User $to Either a email address, user ID, WP_User object,  
    41. * or an array containg the address and name. 
    42. * @param array $args { 
    43. * Optional. Array of extra parameters. 
    44. * @type array $tokens Optional. Assocative arrays of string replacements for the email. 
    45. * } 
    46. */ 
    47. do_action_ref_array( 'bp_send_email', array( &$email, $email_type, $to, $args ) ); 
    48.  
    49. $status = $email->validate(); 
    50. if ( is_wp_error( $status ) ) { 
    51. return $status; 
    52.  
    53. /** 
    54. * Filter this to skip BP's email handling and instead send everything to wp_mail(). 
    55. * This is done if wp_mail_content_type() has been configured for HTML,  
    56. * or if wp_mail() has been redeclared (it's a pluggable function). 
    57. * @since 2.5.0 
    58. * @param bool $use_wp_mail Whether to fallback to the regular wp_mail() function or not. 
    59. */ 
    60. $must_use_wpmail = apply_filters( 'bp_email_use_wp_mail', $wp_html_emails || ! $is_default_wpmail ); 
    61.  
    62. if ( $must_use_wpmail ) { 
    63. $to = $email->get( 'to' ); 
    64.  
    65. return wp_mail( 
    66. array_shift( $to )->get_address(),  
    67. $email->get( 'subject', 'replace-tokens' ),  
    68. $email->get( 'content_plaintext', 'replace-tokens' ) 
    69. ); 
    70.  
    71.  
    72. /** 
    73. * Send the email. 
    74. */ 
    75.  
    76. /** 
    77. * Filter the email delivery class. 
    78. * Defaults to BP_PHPMailer, which as you can guess, implements PHPMailer. 
    79. * @since 2.5.0 
    80. * @param string $deliver_class The email delivery class name. 
    81. * @param string $email_type Type of email being sent. 
    82. * @param array|string $to Array or comma-separated list of email addresses to the email to. 
    83. * @param array $args { 
    84. * Optional. Array of extra parameters. 
    85. * @type array $tokens Optional. Assocative arrays of string replacements for the email. 
    86. * } 
    87. */ 
    88. $delivery_class = apply_filters( 'bp_send_email_delivery_class', 'BP_PHPMailer', $email_type, $to, $args ); 
    89. if ( ! class_exists( $delivery_class ) ) { 
    90. return new WP_Error( 'missing_class', __CLASS__, $this ); 
    91.  
    92. $delivery = new $delivery_class(); 
    93. $status = $delivery->bp_email( $email ); 
    94.  
    95. if ( is_wp_error( $status ) ) { 
    96.  
    97. /** 
    98. * Fires after BuddyPress has tried - and failed - to send an email. 
    99. * @since 2.5.0 
    100. * @param WP_Error $status A WP_Error object describing why the email failed to send. The contents 
    101. * will vary based on the email delivery class you are using. 
    102. * @param BP_Email $email The email we tried to send. 
    103. */ 
    104. do_action( 'bp_send_email_failure', $status, $email ); 
    105.  
    106. } else { 
    107.  
    108. /** 
    109. * Fires after BuddyPress has succesfully sent an email. 
    110. * @since 2.5.0 
    111. * @param bool $status True if the email was sent successfully. 
    112. * @param BP_Email $email The email sent. 
    113. */ 
    114. do_action( 'bp_send_email_success', $status, $email ); 
    115.  
    116. return $status;