wp_mail()循环发送,仅发送到最后一个地址 [英] wp_mail() in a loop, only sending to last address

查看:58
本文介绍了wp_mail()循环发送,仅发送到最后一个地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个插件,该插件可让您向 x 数量的人发送电子邮件。您所要做的就是输入它们的名称和电子邮件地址。这些条目的名称为 friend_name [0] &分别 friend_email [0] 。有 + & -按钮,用于添加和删除输入字段。 $ key 中的数字相对于字段数上下移动。我已确认输入表单上的所有 $ keys 更改。

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.

提交表单并通过以下方式处理时在循环中,所有电子邮件都发送,但仅发送到最后输入的电子邮件。因此,如果我输入5个名字和5个电子邮件,它们将发送以下数据:

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:

名称: John Doe 1
电子邮件: JohnDoe5@gmail.com

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

姓名: John Doe 2
E -邮件: JohnDoe5@gmail.com

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

姓名: John Doe 3
电子邮件: JohnDoe5@gmail.com

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

姓名: John Doe 4
电子邮件: JohnDoe5@gmail.com

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

名称: John Doe 5
电子邮件: JohnDoe5 @ gmail.com

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

这里是循环:

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']. ">" . "\r\n";
    $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';
    }


推荐答案

您的 foreach($ friends as $ key => $ friend)循环正在重写 $ name $ email 变量,因此最后它们只是最后一个值。错误检查后,应将此数据存储在数组中:

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

这篇关于wp_mail()循环发送,仅发送到最后一个地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆