duanpanbo9476 2019-02-21 12:17
浏览 65
已采纳

我想在联系表单7邮件正文中传递php变量

I want to pass php variable in contact form 7 mail body. I have added code in functions.php file. I have added hidden field but didn't work. So I want to check with some other way:

add_action('wpcf7_before_send_mail', 'save_application_form');

function save_application_form($wpcf7) {

//global $wpdb;
    $wpcf7 = WPCF7_ContactForm :: get_current();
    $submission = WPCF7_Submission::get_instance();

    if ($submission) {
        $submited = array();
        $submited['title'] = $wpcf7->title();
        $submited['posted_data'] = $submission->get_posted_data();
        $uploaded_files = $submission->uploaded_files();
    }
    $finfo = finfo_open(FILEINFO_MIME_TYPE);
    $position = $submited['posted_data']["file-181"];
    $cf7_file_field_name = 'file-846';
    $image_location = $uploaded_files[$cf7_file_field_name];
    $mime_type = finfo_file($finfo, $image_location);
    $token = GetRefreshedAccessToken('client_id', 'refresh_token', 'client_secret');
    $ch = curl_init();
    curl_setopt_array($ch, array(
        CURLOPT_URL => 'https://www.googleapis.com/upload/drive/v3/files?uploadType=media',
        CURLOPT_HTTPHEADER => array(
            'Content-Type:' . $mime_type, // todo: runtime detection?
            'Authorization: Bearer ' . $token
        ),
        CURLOPT_POST => 1,
        CURLOPT_POSTFIELDS => file_get_contents($image_location),
        CURLOPT_RETURNTRANSFER => 1
    ));


    $response = curl_exec($ch);
    $id = json_decode($response, TRUE);
    $get_id = $id['id'];
    $link= "https://drive.google.com/file/d/" . $get_id . "/view?usp=sharing";

$err = curl_error($ch);
    curl_close($ch);
    if ($err) {
        echo "cURL Error #:" . $err;
    } else {
        print_r($response);
    }
}

How can I send $link variable in contact form 7 mail? I want to add this share link in mail.

  • 写回答

1条回答 默认 最新

  • duanbei7005 2019-02-28 11:33
    关注

    This might put you on the right track:

    add_action('wpcf7_before_send_mail', 'save_application_form');
    function save_application_form($wpcf7) {
    
    //global $wpdb;
        $wpcf7 = WPCF7_ContactForm :: get_current();
        $submission = WPCF7_Submission::get_instance();
    
        if ($submission) {
            $submited = array();
            $submited['title'] = $wpcf7->title();
            $submited['posted_data'] = $submission->get_posted_data();
            $uploaded_files = $submission->uploaded_files();
    
            $finfo = finfo_open(FILEINFO_MIME_TYPE);
            $position = $submited['posted_data']["file-181"];
            $cf7_file_field_name = 'file-846';
            $image_location = $uploaded_files[$cf7_file_field_name];
            $mime_type = finfo_file($finfo, $image_location);
            $token = GetRefreshedAccessToken('client_id', 'refresh_token', 'client_secret');
            $ch = curl_init();
            curl_setopt_array($ch, array(
                CURLOPT_URL => 'https://www.googleapis.com/upload/drive/v3/files?uploadType=media',
                CURLOPT_HTTPHEADER => array(
                    'Content-Type:' . $mime_type, // todo: runtime detection?
                    'Authorization: Bearer ' . $token
                ),
                CURLOPT_POST => 1,
                CURLOPT_POSTFIELDS => file_get_contents($image_location),
                CURLOPT_RETURNTRANSFER => 1
            ));
    
            $response = curl_exec($ch);
            $id = json_decode($response, TRUE);
            $get_id = $id['id'];
            $link= "https://drive.google.com/file/d/" . $get_id . "/view?usp=sharing";
    
            // Gets the Mail property
            $mail = $submission->prop('mail');
    
            // Append Google drive link to email body of Mail property
            $mail['body'] .= $link;
    
            // Set properties - with updated email body
            $submission->set_properties(array('mail' => $mail));
    
            $err = curl_error($ch);
            curl_close($ch);
            if ($err) {
                echo "cURL Error #:" . $err;
            } else {
                print_r($response);
            }        
    
        }
    }
    

    I've added some code to modify the email body - it just appends your $link variable.

    I've moved all your code inside the $submission IF statement as well.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 Macbookpro 连接热点正常上网,连接不了Wi-Fi。
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程