You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress投诉表单发件邮箱配置冲突问题求助

问题根源

当前代码全局挂载了wp_mail_from和wp_mail_from_name过滤器,这会影响所有通过wp_mail发送的邮件(包括Elementor表单、评价插件的邮件)。只要请求中存在c_email或c_name参数,发件人就会被修改;甚至没有这些参数时,过滤器函数未返回默认值,还可能导致发件人异常。

解决方案

不要全局注册过滤器,而是在发送投诉邮件的临时场景中添加过滤器,发送完成后立即移除,确保仅影响当前投诉邮件的发件人设置。

修改后的完整代码

<form id="complaint_form" class="complaint_form" action="" method="post">
    <label for="name">Name</label>  <span>*</span>
    <input class="c_name" id="c_name" type="text" placeholder="Full Name" required>
    
    <label for="email">Email</label>  <span>*</span>
    <input class="c_email" id="c_email" type="email" placeholder="Email" required>
    
    <label for="phone">Phone Number</label>  <span>*</span>
    <input class="c_number" id="c_number" type="text" pattern="[0-9]*" maxlength="10" placeholder="Phone Number" required>
    
    <label for="order">Order Number</label>  <span>*</span>
    <input class="order_num" id="order_num" type="text" pattern="[0-9]*" placeholder="Order Number" required>
    
    <label for="reason">Reason for Complaint</label>  <span>*</span>
    <textarea class="c_reason" id="c_reason" name="reason" rows="4" placeholder="Enter Message" required></textarea>

    <input type="submit" class="com_but" id="com_but" value="Register">
</form>
<script>
    jQuery(document).ready(function($) {
            $('#complaint_form').on('submit', function(e) {
                e.preventDefault();
                var c_name  = $('#c_name').val();
                var c_email  = $('#c_email').val();
                var c_number  = $('#c_number').val();
                var order_num  = $('#order_num').val();
                var c_reason  = $('#c_reason').val();
             $.ajax({
                    url: '<?php echo admin_url('admin-ajax.php'); ?>',
                    type: 'POST',
                    data: {
                        action:'complaint_email_form_php',
                        c_name: c_name,
                        c_email: c_email,
                        c_number:c_number,
                        order_num:order_num,
                        c_reason:c_reason
                    },
                    success: function( response ) {
                        console.log( 'Complaint email sent successfully.' );
                        alert('Complaint Registered Successfully !!');
                        location.reload();
                    },
                    error: function( response ) {
                        console.log( 'Failed to send complaint email.' );
                        alert('Failed to register complaint, please try again later.');
                    }
                });
             });
        });         
</script>

<?php

add_action( 'wp_ajax_complaint_email_form_php', 'complaint_email_form_php' );
add_action('wp_ajax_nopriv_complaint_email_form_php', 'complaint_email_form_php');

function complaint_email_form_php() {
    // 验证并 sanitize 输入
    $c_name = isset($_POST['c_name']) ? sanitize_text_field($_POST['c_name']) : '';
    $c_email = isset($_POST['c_email']) ? sanitize_email($_POST['c_email']) : '';
    $c_number = isset($_POST['c_number']) ? sanitize_text_field($_POST['c_number']) : '';
    $order_num = isset($_POST['order_num']) ? sanitize_text_field($_POST['order_num']) : '';
    $c_reason = isset($_POST['c_reason']) ? wp_kses_post($_POST['c_reason']) : '';
    
    // 验证必填字段
    if (empty($c_name) || empty($c_email) || empty($c_number) || empty($order_num) || empty($c_reason)) {
        wp_send_json_error('All fields are required.');
    }

    $to_email = 'no.reply.ovenfresh@gmail.com';
    $headers = array('Content-Type: text/html; charset=UTF-8');
    $subject = "Complaint for Order Number: " . $order_num;
    $message = "Full Name: " . $c_name . "<br>Order Number: " . $order_num . "<br>Phone Number: " . $c_number . "<br>Complaint Reason: " . $c_reason;

    // 定义临时过滤器函数
    $temp_from_email = function() use ($c_email) {
        return $c_email;
    };
    $temp_from_name = function() use ($c_name) {
        return $c_name;
    };

    // 添加临时过滤器
    add_filter('wp_mail_from', $temp_from_email);
    add_filter('wp_mail_from_name', $temp_from_name);

    // 发送邮件
    $mail_sent = wp_mail($to_email, $subject, $message, $headers);

    // 立即移除过滤器,避免影响后续邮件
    remove_filter('wp_mail_from', $temp_from_email);
    remove_filter('wp_mail_from_name', $temp_from_name);

    if ($mail_sent) {
        wp_send_json_success();
    } else {
        wp_send_json_error('Failed to send email.');
    }
}

?>

关键改动说明

  1. 移除全局过滤器:删掉原代码中全局注册的wp_mail_from和wp_mail_from_name回调,彻底避免影响其他插件的邮件发送。
  2. 临时过滤器机制:在发送投诉邮件前临时添加过滤器,绑定当前用户的邮箱和姓名,发送完成后立即移除,确保仅本次邮件生效。
  3. 输入安全处理:增加输入字段的存在性检查,用sanitize_text_field、sanitize_email、wp_kses_post处理不同类型的输入,防止非法数据注入。
  4. 修复逻辑错误:将wp_send_json_success()移至return语句前(原代码中return true后永远无法执行该方法),确保AJAX请求能收到正确响应。
  5. 优化用户提示:在AJAX错误回调中添加友好的用户提示,同时完善日志信息便于调试。

内容的提问来源于stack exchange,提问作者Rishikesh Dubey

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 04:09:52