【问题标题】:Contact form 7 - dynamic recipient email联系表格 7 - 动态收件人电子邮件
【发布时间】:2017-02-18 13:50:15
【问题描述】:

我正在尝试从自定义字段中动态获取收件人电子邮件,并使用字符串替换来修改联系表 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 我在这里遗漏了什么吗?

【问题讨论】:

    标签: php email contact-form-7


    【解决方案1】:

    要向动态收件人 wince 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 );
    

    【讨论】:

      【解决方案2】:

      不是很清楚你想用单元标签做什么,但是,这是解决你问题的另一种方法,

      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' );
      

      为了填充 post-id,您可以在客户端使用 javacript,也可以使用 CF7 动态文本扩展在表单加载时预加载它(参见教程 here)。

      【讨论】:

        猜你喜欢
        • 2019-07-26
        • 1970-01-01
        • 1970-01-01
        • 2015-10-11
        • 2015-09-16
        • 1970-01-01
        • 1970-01-01
        • 2016-10-20
        • 1970-01-01
        相关资源
        最近更新 更多