0

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.

double-beep
  • 5,031
  • 17
  • 33
  • 41
P.patel
  • 49
  • 1
  • 9

1 Answers1

0

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.

lumonald
  • 344
  • 4
  • 11