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

7

我正在尝试从自定义字段动态地获取收件人电子邮件,并使用字符串替换修改联系表格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('emailtoreplace@email.com', $dynamic_email, $args['recipient']);
    }

    return $args;
}

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

我正在使用CF7 4.5.1和PHP 5.3,这里有什么我漏掉的吗?

2个回答

7
为了向动态收件人发送电子邮件,自从 WPCF7 5.2 版本以来,这是我所做的:
function wpcf7_before_send_mail_function( $contact_form, $abort, $submission ) {

  $dynamic_email = 'email@email.com'; // 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
“你试图使用unit标签做什么并不是很清楚,不过,这里有一种解决你问题的替代方法。”
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('emailtoreplace@email.com', $dynamic_email, $args['recipient']);
    }

    return $args;
}

add_filter( 'wpcf7_mail_components', 'wpcf7_dynamic_email_field' );

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

1
bs"d,请在此行中添加):if(isset($_POST['post-id'])){ - to-b

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接