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

时间:2019-02-21 12:17:11

标签: php wordpress contact-form-7

我想在联系表单7邮件正文中传递php变量。我已经在functions.php文件中添加了代码。我添加了隐藏字段,但是没有用。所以我想用其他方法检查一下:

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

如何通过联系表7邮件发送$link变量?我想在邮件中添加此共享链接。

1 个答案:

答案 0 :(得分:0)

这可能会让您走上正轨:

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

    }
}

我添加了一些代码来修改电子邮件正文-它只是追加了$link变量。

我也将所有代码移到了$submission IF语句中。