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

如何在Contact Form 7中手动发邮件及编程批量发送(代码求助)

Hey,针对你的两个问题,我来一步步梳理解决方法:

问题1:如何在Contact Form 7中手动发送邮件?

CF7本身提供了贴合原生流程的触发方式,常见的有两种思路:

  • 调用CF7内置的邮件处理函数,自动解析表单模板、触发相关钩子,完全遵循CF7的邮件发送逻辑;
  • 结合WordPress原生的wp_mail函数,自行构建邮件内容发送,适合不需要依赖CF7模板的场景。

下面我会结合你的第二个问题的代码,给出具体可落地的实现方案。

问题2:在指定函数内通过Contact Form 7发送批量邮件

你的代码已经挂在了wpcf7_before_send_mail动作上,只需要在表单ID判断的区块里补上邮件发送逻辑即可。我推荐用贴合CF7原生流程的方式,能自动处理短代码解析、附件等细节,下面是修改后的完整代码:

首先优化你的get_mod_and_admin_emails函数,直接返回数组更可靠(避免字符串拼接的多余逗号问题):

function get_mod_and_admin_emails( ){
    global $bp;
    // 先判断当前是否在群组页面,避免非群组场景下报错
    if( !isset($bp->groups->current_group->id) ){
        return array();
    }
    
    $users = get_users( array( 'fields' => array( 'ID', 'user_email' ) ) );
    $emails = array();

    foreach($users as $user){
        $user_id = $user->ID;
        if( groups_is_user_mod( $user_id, $bp->groups->current_group->id ) || groups_is_user_admin( $user_id, $bp->groups->current_group->id ) ){
            $emails[] = $user->user_email;
        }
    }

    return $emails;
}

然后修改benson_call_before_form_submit函数,加入CF7邮件发送逻辑:

function benson_call_before_form_submit( $wpcf7 ){
    $submission = WPCF7_Submission::get_instance();
    $posted_data = array();
    if ( $submission ) {
        $posted_data = $submission->get_posted_data();
    }

    // 仅处理ID为2216的表单
    if( $wpcf7->id() == 2216 ) {
        $emails = get_mod_and_admin_emails();
        
        // 有收件人邮箱才继续执行
        if( !empty($emails) ){
            // 获取当前表单的邮件配置(主题、内容、发件人等)
            $original_mail = $wpcf7->prop( 'mail' );
            $mailer = WPCF7_Mail::get_instance();
            
            // 遍历每个收件人发送邮件
            foreach( $emails as $recipient_email ){
                // 复制原邮件配置,避免修改原表单的默认设置
                $custom_mail = $original_mail;
                // 设置当前收件人邮箱
                $custom_mail['recipient'] = $recipient_email;
                
                // 解析邮件内容,自动替换表单短代码和提交数据
                $mail_result = $mailer->compose(
                    $custom_mail,
                    $posted_data,
                    $wpcf7
                );
                
                // 邮件内容验证通过后发送
                if ( $mail_result['valid'] ) {
                    $mailer->send( $mail_result['mail'] );
                }
            }
        }

        // 如果你不需要CF7发送原表单的默认邮件,取消下面的注释
        // $wpcf7->skip_mail = true;
    }
}
add_action( 'wpcf7_before_send_mail', 'benson_call_before_form_submit' );

为什么这么写?

  • 用WPCF7_Mail::get_instance()处理邮件,会自动触发CF7的相关钩子,和正常表单提交的逻辑完全一致;
  • 复制原邮件配置再修改收件人,不会影响原表单的默认设置;
  • 加入了群组ID的存在判断,避免非群组页面调用时出现PHP错误;
  • 数组存储邮箱列表,比字符串拼接更稳定,不会出现末尾多余逗号导致的发送失败。

如果你想要更轻量化的实现,也可以直接调用WordPress的wp_mail函数(适合无附件的简单场景):

function benson_call_before_form_submit( $wpcf7 ){
    $submission = WPCF7_Submission::get_instance();
    $posted_data = array();
    $subject = '';
    $body = '';
    $from = '';
    
    if ( $submission ) {
        $posted_data = $submission->get_posted_data();
        $mail_components = $wpcf7->prop( 'mail' );
        
        // 解析CF7的邮件主题和内容,替换短代码
        $subject = wpcf7_mail_replace_tags( $mail_components['subject'], $posted_data, $wpcf7 );
        $body = wpcf7_mail_replace_tags( $mail_components['body'], $posted_data, $wpcf7 );
        $from = wpcf7_mail_replace_tags( $mail_components['sender'], $posted_data, $wpcf7 );
    }

    if( $wpcf7->id() == 2216 ) {
        $emails = get_mod_and_admin_emails();
        
        if( !empty($emails) ){
            foreach( $emails as $email ){
                // 直接用wp_mail发送邮件
                wp_mail(
                    $email,
                    $subject,
                    $body,
                    array( "From: $from" )
                );
            }
        }

        // 可选:跳过原表单的默认邮件发送
        // $wpcf7->skip_mail = true;
    }
}
add_action( 'wpcf7_before_send_mail', 'benson_call_before_form_submit' );

内容的提问来源于stack exchange,提问作者Karue Benson Karue

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.07 19:37:32