Skip to:
Content
Pages
Categories
Search
Top
Bottom

Using wp_mail and emails


  • joaomluz
    Participant

    @joaomluz

    Hello,

    Im using the wp_mail configuration adding

    add_filter('bp_email_use_wp_mail', '__return_true');

    in bp-custom.php. This configuration send all emails in plain text but i need to send this emails following the configured template (in HTML)

    There is an solution to send e-mails via wp_mail in HTML?

    Thanks.

Viewing 6 replies - 1 through 6 (of 6 total)

  • Henry Wright
    Moderator

    @henrywright

    You can change the content type of WordPress emails using the wp_mail_content_type filter:

    add_filter( 'wp_mail_content_type', function( $content_type ) {
        $content_type = 'text/html';
        return $content_type;
    } );

    joaomluz
    Participant

    @joaomluz

    Thanks for your help.

    I add this filter but i have also to change bp_send_email function in file /buddypress/bp-core/bp-core-functions.php. then it works.

    Before:

    $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' )
    		);
    	}

    After:

    $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_template( 'add-content' )
    		);
    	}

    Is there any way to hook/override this “$email->get_template( ‘add-content’ )” keeping the plugin core unchanged?


    Henry Wright
    Moderator

    @henrywright

    Can you instead use the wp_mail filter to change the message?

    add_filter( 'wp_mail', function( $array ) {
        // Set conditions here.
    
        $array['message'] = 'Change this';
        return $array;
    } );

    melhergui
    Participant

    @melhergui

    I got it working for me only using @henrywright solution with the filter for html emails. But my bp_email_use_wp_mail function i have set to return false. Thank you!


    Khurshid Faizullaev
    Participant

    @loveplove

    thank you. works perfectly.

    I just had to put all three together

    add_filter(‘bp_email_use_wp_mail’, ‘__return_true’);

    add_filter( ‘wp_mail_content_type’, function( $content_type ) {
    $content_type = ‘text/html’;
    return $content_type;
    } );

    $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_template( ‘add-content’ )
    );
    }

    add_filter( ‘wp_mail’, function( $array ) {
    // Set conditions here.

    $array[‘message’] = ‘Change this’;
    return $array;
    } );

    DELETE THE FOLLOWING:

    $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’ )
    );
    }

    LovePlove


    elucinda
    Participant

    @elucinda

    do I add this to bp-custom.php?

Viewing 6 replies - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.
Skip to toolbar