如何以编程方式发送联系人表单7中的其他通知电子邮件

时间:2020-05-30 作者:Karue Benson Karue

我有一个从Wordpress数据库中获取的电子邮件列表。

我想使用联系人表单7以编程方式向这些电子邮件发送电子邮件。

这是我的密码

function benson_call_before_form_submit( $wpcf7 ){

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

    if( $wpcf7->id() == 2216 || $wpcf7->id() == "2216" ) {
        //$wpcf->skip_mail = true; 
        // we can now send the email here
        $emails = get_mod_and_admin_emails(); // these are emails

        // how do I send email inside here from contact form 7
        // ?????????????????

    }

}

add_action( \'wpcf7_before_send_mail\', \'benson_call_before_form_submit\' );

function get_mod_and_admin_emails(  ){
    global $bp; 
    $users = get_users( array( \'fields\' => array( \'ID\', \'user_email\' ) ) );
    $emails = ""; 



    foreach($users as $key => $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 ) ){
           $email = $user->user_email; 

           $emails .=$email.","; 

       }

    }

    return $emails; 
}

2 个回复
SO网友:Geza Gog

您可以在发送电子邮件之前使用wpcf7\\u before\\u send\\u mail挂钩操作收件人。

下面是一个添加额外收件人的简单脚本:

add_action( \'wpcf7_before_send_mail\', \'add_my_second_recipient\', 10, 1 );

function add_my_second_recipient($instance) {
    $properites = $instance->get_properties();
    // use below part if you want to add recipient based on the submitted data
    /*
    $submission = WPCF7_Submission::get_instance();
    $data = $submission->get_posted_data();
    */
    $additional_recipent = \'[email protected]\';
    $properites[\'mail\'][\'recipient\'] .= \', \' . $additional_recipent;
        
    $instance->set_properties($properites);
    return ($instance);
}

SO网友:Aurovrata

有两种方法可以实现这一点

1使用“wpcf7\\u mail\\u components”挂钩,修改电子邮件收件人或抄送列表,

add_filter( \'wpcf7_mail_components\', \'change_email_address\',10,3);
function change_email_address($components, $form, $mailobj){
  //get the $users....
  foreach($users as $key => $user){
    $email = $user->user_email;
    //either as in the recipient list,
    $components[\'recipient\'].=\',\'.$email;
    //or add in cc
    $cc=$email.\',\';
  }
  $components[\'additional_headers\']=\'cc:\'.$cc;
  return $components;
}
2使用“wpcf7\\u additional\\u mail”创建其他电子邮件,

add_filter( \'wpcf7_additional_mail\', \'send_additional_mails\',10,2);
function send_additional_mails($mails, $form){
  //$mails will container the maail_2 template if you have enabled it.
  //reset it if you don\'t want to send mail_2, but just using it as an additional mail template. 
  $mails = array(); 
  //get the $users....
  foreach($users as $key => $user){
    $email = $user->user_email;
    //copy your mail template
    $template = $form->prop(\'mail\');
    //or if you have enabled mail 2 template, you can use that instead.
    $template = $form->prop(\'mail_2\');
    //add the mail to the recipient list...
    $template[\'recipient\'] = $email ;
    $mails[$email] = $template;
  }
  return $mails;
}
设置好模板后,可以挂接“wpcf7\\u mail\\u组件”,进一步过滤其他邮件。

相关推荐