I'm aiming to submit my Contact Form 7 data in Wordpress to an API (all fields are fine, but adding the file attachment breaks my submit - error 500), which includes an attachment. I can make it work with a dummy file like so:
$attachedfile = get_template_directory() . '/test.pdf';
$file_contents = file_get_contents($attachedfile);
$base64_file = base64_encode($file_contents);
$file_name = basename($attachedfile);
$mime_type = mime_content_type($attachedfile);
$documents = [
[
'FileName' => $file_name,
'MimeType' => $mime_type,
'Base64' => $base64_file,
],
];
This sends the file to the server, and it receives it fine. But I need to do it dynamically through the form.
So I constructed this function:
function my_custom_form_submission($contact_form) {
$submission = WPCF7_Submission::get_instance();
if ($submission) {
$data = $submission->get_posted_data();
$email = isset($data['your-email']) ? sanitize_email($data['your-email']) : '';
$phone = 'Nincs megadva';
$lastname = isset($data['your-name']) ? sanitize_text_field($data['your-name']) : '';
$firstname = isset($data['text-532']) ? sanitize_text_field($data['text-532']) : '';
$companyname = isset($data['text-479']) ? sanitize_textarea_field($data['text-479']) : '';
$created = current_time('Y-m-d\TH:i:s');
$branch = isset($data['menu-586']) ? sanitize_textarea_field($data['menu-586']) : '';
if ($branch == 'Divat / Design') {
$flow = 'DivatDesign';
} else {
$flow = 'Startup';
}
$uploaded_files = $submission->uploaded_files();
// if (!empty($uploaded_files)) {
// $uploaded_file = $uploaded_files['file-524'];
// // Read file contents
// $file_content = file_get_contents($uploaded_file);
// if ($file_content !== false) {
// // Encode file content in base64
// $base64_file = base64_encode($file_content);
// // Prepare data array for API
// $documents = array(
// 'FileName' => $uploaded_file['name'],
// 'MimeType' => $uploaded_file['type'],
// 'Base64' => 'data:' . $uploaded_file['type'] . ';base64,' . $base64_file
// );
// }
// }
//error_log('Uploaded File Path: ' . $attachedfile);
if (wf_checkEmail($email) == false) {
$userId = saveToWf($email, $lastname, $firstname);
//Log the userId for debugging
error_log('User ID: ' . $userId);
if ($userId) {
registerToWf($email, $lastname, $firstname, $flow, $userId, $companyname, $phone, $attachedfile, $branch, $documents);
}
}
}
}
add_action('wpcf7_before_send_mail', 'my_custom_form_submission');
Issue is, if I submit my form like this, it works for all fields, however if I comment out the fields related to $uploaded_files, it breaks the submit with an 500 error. I have found no way to see (output / dump) what $uploaded_files exactly returns, but I'm guessing it's something different than my dummy file.
There is only 1 file uploaded, so I don't need a foreach. Anyone done something similar, or can point me in the right direction? I was not able to find anything on this.
- Also it's worth noting that I only have FTP access to their server, and wp_debug seems to be disabled somehow. No log is visible despite having define( 'WP_DEBUG', true ); define( 'WP_DEBUG_DISPLAY', true ); define( 'WP_DEBUG_LOG', true ); in wp_config.