wp_mail() in a loop, only sending to last address

I’m using a plugin that allows you to send an e-mail to x amount of people. All you have to do is enter their “name” and “e-mail address”. These entries are named friend_name[0] & friend_email[0] respectively. There are + & - buttons for adding and removing entry fields. The number in the $key goes up and down relative to the number of fields. I have confirmed all the $keys change on the entry form.

When the form is submitted and is processed through the loop, the e-mails all send, but only to the last e-mail entered. So if I enter 5 names and 5 e-mails, they send with this data:

Read More

Name: John Doe 1
E-Mail: JohnDoe5@gmail.com

Name: John Doe 2
E-Mail: JohnDoe5@gmail.com

Name: John Doe 3
E-Mail: JohnDoe5@gmail.com

Name: John Doe 4
E-Mail: JohnDoe5@gmail.com

Name: John Doe 5
E-Mail: JohnDoe5@gmail.com

Here’s the loop:

function invfr_sendmail() {
$post = ( !empty( $_POST ) ) ? true : false;

if( $post ) {
    $subject = invfr_get_settings( 'subject' );
    $message = invfr_get_settings( 'message' );
    $friends = $_POST['friend_email'];
    $headers = "From:" . $_POST['user_name'] . " <" . $_POST['user_email']. ">" . "rn";
    $errors = array();
    foreach ( $friends as $key => $friend ) {
        $name = stripslashes( $_POST['friend_name'][$key] );
        $email = trim( $_POST['friend_email'][$key] );

        // Check name
        if( !$name )
            $errors[] = '#friend_name-' . $key;

        if( !$email )
            $errors[] = '#friend_email-' . $key;

        if( $email && !is_email( $email ) )
            $errors[] = '#friend_email-' . $key;
    }

    // send email 
    if( !$errors ) {
        foreach ( $friends as $key => $friend )
            $mail = wp_mail( $email, invfr_tokens_replacement( $subject, $_POST, $key ), invfr_tokens_replacement( $message, $_POST, $key ), invfr_tokens_replacement( $headers, $_POST, $key ) );
        if( $mail )
            echo 'sent';
    }

Related posts

Leave a Reply

1 comment

  1. Your foreach ( $friends as $key => $friend ) loop is rewriting the $name and $email variables every time, so at the end they’ll just have the last value. You should store this data in an array after your error checks:

        // Check name and email 
        $data_missing = false;
        if( ! $name ) {
            $errors[] = '#friend_name-' . $key;
            $data_missing = true;
        } 
        if ( ! $email || ! is_email( $email) ) {
            $errors[] = '#friend_email-' . $key;
            $data_missing = true;
        } 
        if ( ! $data_missing ) {
            $email_data[] = array( 'name' => $name, 'email' => $email );
        }
    

    Then, loop through this array in your send mail part:

    if ( ! empty( $email_data) ) {
        foreach( $email_data as $data ) {
            $name = $data['name'];
            $email = $data['email'];
            $mail = wp_mail( .... )
        }
    }