如何在wp_mail中限制发送两次电子邮件

时间:2018-07-24 作者:Minesh

我已经创建了添加/删除动态输入字段。我正在使用wp_mail(); 发送邮件。我已限制第一个用户接收邮件。我想向其他用户发送邮件。一切正常。唯一的问题是,每个用户都会收到两封电子邮件。wp_mail(); 同时创建两次。

意味着当我提交表单时,每个用户都会收到两次相同的电子邮件。我希望每个用户只能收到一封电子邮件。以下是我的代码:

<?php get_header(); ?>

<?php

$member_details->user_email = array_map( \'sanitize_text_field\', $_POST[\'user_email\'] );
$member_details->user_role = array_map( \'sanitize_text_field\', $_POST[\'user_role\'] );
$member_details->status = array_map( \'sanitize_text_field\', $_POST[\'status\'] );

if ( isset( $_POST[\'project_prev\'] ) ) {

$user_emails = $member_details->user_email;
$user_statuses = $member_details->status;

for ($i=1; $i < count($user_emails); $i++) { 
    $user_email1[] = $user_emails[$i];

}

for ($a=1; $a < count($user_statuses); $a++) { 
    $user_status = $user_statuses[$a];

if ( $user_status == \'Unverified\' ) {

$to = $user_email1;
$subject = "Congrats! You are added to the Project  -  " . "\'" . $project_title . "\'";
$message = \'If you are not the member of project plz contact us to remove at [email protected]\';

wp_mail( $to, $subject, $message ); 

}

}

}

?>

<form method="POST">

<div class="panel panel-default">

  <div class="panel-heading"><center><b>Team Members</b></center></div>

  <div class="panel-body">

    <div class="row">

      <div class="col-md-5"><label>Member\'s Registered Email</label></div>
      <div class="col-md-5"><label>Role in Project</label></div>
      <div class="col-md-2"></div>

    </div>

    <div class="row">

      <div class="col-md-5">

        <div class="form-group">

          <input type="text" class="form-control" name="user_email[]" value="<?php $current_user = wp_get_current_user(); if ( !($current_user instanceof WP_User) ) return; echo $current_user->user_email; ?>" placeholder="" readonly="readonly">

        </div>

      </div>

      <div class="col-md-5">

        <div class="form-group">

          <input type="text" class="form-control" name="user_role[]" placeholder="">
          <input type="hidden" class="form-control" name="status[]" value="Verified" placeholder="">

        </div>

      </div>

      <div class="col-md-2">

        <button type="button" class="btn btn-success" id="add-member-fields"><span class="glyphicon glyphicon-plus" aria-hidden="true"></span> Add</button>

      </div>

    </div>

    <div id="member-fields">

    </div>

<p class="help-block"><i>To add member please register new User, if already not registered.</i></p>

  </div>

</div>

<input class="btn btn-info btn-block" type="submit" name="project_prev" value="Preview" style="border-radius: 0px;">

</form>

<?php

$user = wp_get_current_user();
$args = array(
  \'role\'         => \'backer\',
  \'exclude\'      => array( $user->ID ),
 );

$users = get_users( $args );
$get_user_emails = wp_list_pluck( $users, \'user_email\' );

?>

<script type=\'text/javascript\'>

jQuery(document).ready(function($) {
  var wrapper = $("#member-fields");
  var add_button = $("#add-member-fields");

  var x = 1;
  var availableAttributes = <?php echo json_encode($get_user_emails); ?>;
  var previousValue="";

  add_button.click(function(e) {
    e.preventDefault();
    x++;
    var element = $(\'<div id="user-fields"><div class="row"><div class="col-md-5"><div class="form-group"><input type="text" class="form-control" id="user_email" name="user_email[]" placeholder=""></div></div><div class="col-md-5"><div class="form-group"><input type="text" class="form-control" name="user_role[]" placeholder=""><input type="hidden" class="form-control" name="status[]" value="Unverified" placeholder=""></div></div><div class="col-md-2"><button type="button" class="btn btn-danger" id="remove_member_field"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span> Remove</button></div></div><div class="clear"></div></div>\');

    element.fadeIn("slow").find("input[name^=\'user_email\']").autocomplete({
    autoFocus: true,
      source: availableAttributes,
    });
    wrapper.append(element);
  });

  wrapper.on("keyup","#user_email",function() {
    var isValid = false;
    for (i in availableAttributes) {
        if (availableAttributes[i].toLowerCase().match(this.value.toLowerCase())) {
            isValid = true;
        }
    }
    if (!isValid) {
        this.value = previousValue
    } else {
        previousValue = this.value;
    }

});

  wrapper.on("click", "#remove_member_field", function(e) {
    e.preventDefault();
    $(this).parent().fadeOut(300, function() {
      $(this).closest(\'#user-fields\').remove();
      x--;
    });
  });
});

</script>

<?php get_footer(); ?>

2 个回复
最合适的回答,由SO网友:Sally CJ 整理而成

这些POST 变量:$_POST[\'user_email\'], $_POST[\'user_role\'], 和$_POST[\'status\'] (每个都是array) 通过它们的索引彼此连接,因此您可以只循环其中一个,并使用相同的索引(或迭代器)访问其他数组中的项。

这应该有助于您理解它:

for ( $i = 0; $i < count( $member_details->user_email ); $i++ ) {
    $user_email = $member_details->user_email[ $i ];
    $user_role = $member_details->user_role[ $i ];
    $status = $member_details->status[ $i ];
    ...
}
因此,该部分/代码:

if ( isset( $_POST[\'project_prev\'] ) ) {

$user_emails = $member_details->user_email;
$user_statuses = $member_details->status;

...

}
我这样编码:

(请注意,为了清晰起见,我重新缩进了它。)

if ( isset( $_POST[\'project_prev\'] ) ) {

    // Starts with the first user (index 1).
    for ( $i = 1; $i < count( $member_details->user_email ); $i++ ) {
        $user_status = $member_details->status[ $i ];

        if ( $user_status === \'Unverified\' ) {

            $to = $member_details->user_email[ $i ];
            $project_title = \'??\'; // This wasn\'t defined anywhere in the file/code.
            $subject = "Congrats! You are added to the Project  -  " . "\'" . $project_title . "\'";
            $message = \'If you are not the member of project plz contact us to remove at [email protected]\';

            wp_mail( $to, $subject, $message );

        }

    }

}

SO网友:WebElaine

这里的问题是,首先创建一个包含所有电子邮件地址的数组,然后在用户状态的第二个“for”循环中,如果循环中当前用户的状态未经验证,则向整个数组发送一封电子邮件$user_email1. 如果可以安全地假设$i 在两个$user_email1 数组和$user_statuses 数组,您可以更改

$to = $user_email1;

$to = $user_email1[$i];

这样,您只需将电子邮件发送给for循环中的当前用户。

结束

相关推荐