联系表格 7 - 动态收件人电子邮件

问题描述 投票:0回答:3

我正在尝试从自定义字段动态获取收件人电子邮件,并使用字符串替换来修改联系表单 7 收件人电子邮件。联系表格正在发送,但似乎没有更改收件人电子邮件,因为我没有收到电子邮件。

<?php
function wpcf7_dynamic_email_field( $args ) {

    $dynamic_email = '';
    $submission = WPCF7_Submission::get_instance();
    $unit_tag = $submission->get_meta( 'wpcf7-f3936-p3933-o1' );

    // get the post ID from the unit tag
    if ( $unit_tag && preg_match( '/^wpcf7-f(\d+)-p(\d+)-o(\d+)$/', $unit_tag, $matches ) ) {
        $post_id = absint( $matches[2] );
        $dynamic_email = get_post_meta( $post_id, 'email', true );
    }
    if ( $dynamic_email ) {
        $args['recipient'] = str_replace('[email protected]', $dynamic_email, $args['recipient']);
    }

    return $args;
}

add_filter( 'wpcf7_mail_components', 'wpcf7_dynamic_email_field' );
?>

我正在运行 CF7 4.5.1 和 PHP 5.3 我在这里遗漏了什么吗?

php email contact-form-7
3个回答
8
投票

要向动态收件人发送电子邮件 wince WPCF7 5.2,这就是我的做法:

function wpcf7_before_send_mail_function( $contact_form, $abort, $submission ) {

  $dynamic_email = '[email protected]'; // get your email address...

  $properties = $contact_form->get_properties();
  $properties['mail']['recipient'] = $dynamic_email;
  $contact_form->set_properties($properties);

  return $contact_form;

}
add_filter( 'wpcf7_before_send_mail', 'wpcf7_before_send_mail_function', 10, 3 );

3
投票

目前还不清楚您要使用单位标签做什么,但是,这是解决您的问题的另一种方法,

function wpcf7_dynamic_email_field( $args ) {
    //create a hidden field with your post-id
    $dynamic_email = '';
    if(isset($_POST['post-id'])){
      $post_id = $_POST['post-id'];
      $dynamic_email = get_post_meta( $post_id, 'email', true );
    }
    if ( $dynamic_email ) {
        $args['recipient'] = str_replace('[email protected]', $dynamic_email, $args['recipient']);
    }

    return $args;
}

add_filter( 'wpcf7_mail_components', 'wpcf7_dynamic_email_field' );

为了填充帖子 ID,您可以在客户端使用 javacript,也可以使用 CF7 动态文本扩展在表单加载时预加载它(请参阅教程此处)。


0
投票

现在有一个内置功能。请参阅插件作者发布的文档:https://contactform7.com/selectable-recipient-with-pipes/

© www.soinside.com 2019 - 2024. All rights reserved.