• Resolved Steelclover

    (@steelclover)


    <?php // CUSTOM EMAIL HANDLER SOCCER TEAM
    function oberbayern_mailer( $new_status, $old_status, $post ){global $post;
    if ( 'publish' !== $new_status or 'publish' === $old_status )
    return; if( has_term( 'oberbayern', 'event_category' ) ) 
    { $spielern = get_users( array ( 'role' => 'oberbayern' ) );
    $emails = array (); foreach ( $spielern as $spieler ) $emails[] = $spieler->user_email; 
    $post_title = get_the_title( $post_id );
    $post_url = get_permalink( $post_id );
    $subject = 'Das Spiel wurde verschoben';
    $message .= "Das Spiel\n -> ".
    $post_title. " <-\nwurde verschoben. \n\nBitte prüfen Sie den folgenden Link:  " .
    $post_url. "\n\n\n\n\n\nMit freundlichen Grüßen\n\n\n\nWeb Org Team\nFußballverein 1 Bayern";
    $headers = 'bcc: coachemail@adresse.com' ;
    wp_mail( $emails,$subject, $message,$headers );}}
    add_action( 'transition_post_status', 'oberbayern_mailer', 10, 3 );?>

    Now it is sending an email to all Players of the team Oberbayern and a static Bcc to the coach when the status of a post is changed.

    I want to swap the function to:

    If the status of a Post is changed, the coach gets the Email and all the Players get the Bcc.

    Means i need to swapp “TO” to “BCC”. Any help – this one seems hard.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    In an overall sense, you’d just swap the $emails and $headers args for wp_mail(), i.e.
    wp_mail( $headers,$subject, $message,$emails );

    It’s not that simple of course, the coach’s email can no longer contain “bcc: “. And the player emails pushed into the $emails array would each need “bcc: ” prepended to them.

    Note that we cannot endlessly stuff BCC emails into a single wp_mail() call. There is some practical limit to the total length of all the strings combined. I don’t recall the limit, but it’s not all that large. But if you were successful in emailing all players and the coach in one call, swapping TO and BCC will not change that.

    Thread Starter Steelclover

    (@steelclover)

    Thanks a lot!

    Where exactly do i have to write down the „Bcc: „?

    I messed around a few options but it wont fit.

    I really appriciate your help.

    Maybe try something like this?
    Note: it’s untested.

    function oberbayern_mailer( $new_status, $old_status, $post ) {
    	global $post;
        if ('publish' !== $new_status or 'publish' === $old_status) {
            return;
        }
        if (has_term('oberbayern', 'event_category')) {
    		$spielern = get_users(array('role' => 'oberbayern'));
    		$to = 'coachemail@adresse.com';
            $emails = array();
    		foreach ($spielern as $spieler) {
                $emails[] = $spieler->user_email;
            }
    
            $post_title = get_the_title($post_id);
            $post_url = get_permalink($post_id);
            $subject = 'Das Spiel wurde verschoben';
            $message .= "Das Spiel\n -> " .
                $post_title . " <-\nwurde verschoben. \n\nBitte prüfen Sie den folgenden Link:  " .
                $post_url . "\n\n\n\n\n\nMit freundlichen Grüßen\n\n\n\nWeb Org Team\nFußballverein 1 Bayern";
            $headers = 'bcc: '. implode(",", $emails);
            wp_mail($to, $subject, $message, $headers);
    	}
    }
    add_action('transition_post_status', 'oberbayern_mailer', 10, 3);
    
    Thread Starter Steelclover

    (@steelclover)

    I’ve tested it and it works awesome, even with my custom wp_mail queue.

    Thank you @zex2911 and @bcworkz!

    • This reply was modified 3 years, 9 months ago by Steelclover.
Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Send BCC instead of TO’ is closed to new replies.