bp_send_email( string $email_type, string|array|int|WP_User $to, array $args = array() )

Send email, similar to WordPress’ wp_mail().

Description

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.

Parameters

$email_type

(Required) Type of email being sent.

$to

(Required) Either an email address, user ID, WP_User object, or an array containg the address and name.

$args

(Optional) Array of extra parameters.

  • 'tokens'
    (array) Optional. Assocative arrays of string replacements for the email.

Default value: array()

Return

(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.

Source

File: bp-core/bp-core-functions.php

3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
function bp_send_email( $email_type, $to, $args = array() ) {
    static $is_default_wpmail = null;
    static $wp_html_emails    = null;
 
    // Has wp_mail() been filtered to send HTML emails?
    if ( is_null( $wp_html_emails ) ) {
        /** This filter is documented in wp-includes/pluggable.php */
        $wp_html_emails = apply_filters( 'wp_mail_content_type', 'text/plain' ) === 'text/html';
    }
 
    // Since wp_mail() is a pluggable function, has it been re-defined by another plugin?
    if ( is_null( $is_default_wpmail ) ) {
        try {
            $mirror            = new ReflectionFunction( 'wp_mail' );
            $is_default_wpmail = substr( $mirror->getFileName(), -strlen( 'pluggable.php' ) ) === 'pluggable.php';
        } catch ( Exception $e ) {
            $is_default_wpmail = true;
        }
    }
 
    $args = bp_parse_args( $args, array(
        'tokens' => array(),
    ), 'send_email' );
 
 
    /*
     * Build the email.
     */
 
    $email = bp_get_email( $email_type );
    if ( is_wp_error( $email ) ) {
        return $email;
    }
 
    // From, subject, content are set automatically.
    $email->set_to( $to );
    $email->set_tokens( $args['tokens'] );
 
    /**
     * Gives access to an email before it is sent.
     *
     * @since BuddyPress 2.8.0
     *
     * @param BP_Email                 $email      The email (object) about to be sent.
     * @param string                   $email_type Type of email being sent.
     * @param string|array|int|WP_User $to         Either an email address, user ID, WP_User object,
     *                                             or an array containg the address and name.
     * @param array                    $args {
     *     Optional. Array of extra parameters.
     *
     *     @type array $tokens Optional. Assocative arrays of string replacements for the email.
     * }
     */
    do_action_ref_array( 'bp_send_email', array( &$email, $email_type, $to, $args ) );
 
    $status = $email->validate();
    if ( is_wp_error( $status ) ) {
        return $status;
    }
 
    /**
     * Filter this to skip BP's email handling and instead send everything to wp_mail().
     *
     * This is done if wp_mail_content_type() has been configured for HTML,
     * or if wp_mail() has been redeclared (it's a pluggable function).
     *
     * @since BuddyPress 2.5.0
     *
     * @param bool $use_wp_mail Whether to fallback to the regular wp_mail() function or not.
     */
    $must_use_wpmail = apply_filters( 'bp_email_use_wp_mail', $wp_html_emails || ! $is_default_wpmail );
 
    if ( $must_use_wpmail ) {
        $to = $email->get( 'to' );
 
        return wp_mail(
            array_shift( $to )->get_address(),
            $email->get( 'subject', 'replace-tokens' ),
            $email->get( 'content_plaintext', 'replace-tokens' )
        );
    }
 
 
    /*
     * Send the email.
     */
 
    /**
     * Filter the email delivery class.
     *
     * Defaults to BP_PHPMailer, which as you can guess, implements PHPMailer.
     *
     * @since BuddyPress 2.5.0
     *
     * @param string       $deliver_class The email delivery class name.
     * @param string       $email_type    Type of email being sent.
     * @param array|string $to            Array or comma-separated list of email addresses to the email to.
     * @param array        $args {
     *     Optional. Array of extra parameters.
     *
     *     @type array $tokens Optional. Assocative arrays of string replacements for the email.
     * }
     */
    $delivery_class = apply_filters( 'bp_send_email_delivery_class', 'BP_PHPMailer', $email_type, $to, $args );
    if ( ! class_exists( $delivery_class ) ) {
        return new WP_Error( 'missing_class', __CLASS__, $this );
    }
 
    $delivery = new $delivery_class();
    $status   = $delivery->bp_email( $email );
 
    if ( is_wp_error( $status ) ) {
 
        /**
         * Fires after BuddyPress has tried - and failed - to send an email.
         *
         * @since BuddyPress 2.5.0
         *
         * @param WP_Error $status A WP_Error object describing why the email failed to send. The contents
         *                         will vary based on the email delivery class you are using.
         * @param BP_Email $email  The email we tried to send.
         */
        do_action( 'bp_send_email_failure', $status, $email );
 
    } else {
 
        /**
         * Fires after BuddyPress has succesfully sent an email.
         *
         * @since BuddyPress 2.5.0
         *
         * @param bool     $status True if the email was sent successfully.
         * @param BP_Email $email  The email sent.
         */
        do_action( 'bp_send_email_success', $status, $email );
    }
 
    return $status;
}

Changelog

Changelog
Version Description
BuddyPress 2.5.0 Introduced.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.