doushe2513 2013-11-06 16:37
浏览 36
已采纳

wp_mail()在一个循环中,只发送到最后一个地址

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:

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']. ">" . "
";
    $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';
    }
  • 写回答

1条回答 默认 最新

  • dougou5852 2013-11-06 16:49
    关注

    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( .... )
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类