I’ve searched and searched for a solution to my problem, and haven’t had any luck…
I have a WordPress site collecting form submissions with Contact Form 7. I’m taking those submissions and creating a new board item with them via webhook, using Zapier.
So far, I have all of the data zapping correctly, except for one part. If the user uploads a file via Contact Form 7, I would like to attach it to the ‘files’ column on the board. So far everything I’ve tried has not worked.
If I’m not mistaken, I have to use ‘add_file_to_column’, but I’m 99% sure I’m not formatting it correctly. I’ve also read through this thread, but I don’t think it’s trying to achieve the same thing I am.
The file uploads themselves are stored on my web server, so they have static URLs.
Welcome to the community. I do see where you are coming from, I had the same need (to upload files from my WordPress site to monday). I was too struggling with Zapier and Integromat and at the end I decided to build a PHP snippets that does this. If you are interested in such an approach, let me know and I can show you the snippet.
It has become somewhat larger snippet and in my case I use WordPress plugin WPForms that will handle the upload from my local PC to my webserver. After the file(s) are on my WordPress server the snippet below is called with a valid monday boardId and an array called upload. If I remember correctly the array has only name: filename and value: actual contents members.
The snippet creates an item in the specified board and uploads the file to the file column. The challenge is that you need to post a multipart body and therefore you need to build from scracht, including a generated boudary to split the multipart.
PS: my monday key is stored in my profile and is retrieved by the get_field call. MONDAY_TEAM and MONDAY_URL are defined in my wpconfig file.
Have fun with it.
/************************************************************************************************************************************************************
* Name: monday_upload_files
* Arguments: $board_id, $upload - board_id: DOCS board id where item needs to be created / $upload: array with file upload information
* Returns: None
* Function: Creates an item in the given board (first group) and attaches the file to the file column
*************************************************************************************************************************************************************/
function monday_upload_files( $board_id, $upload) {
//Create a new item in the board
$header = array(
'User-Agent' => MONDAY_TEAM . ' GraphQL Client',
'Authorization' => get_field('mondaykey', 'user_' . get_current_user_id()),
'Content-Type' => 'multipart/form-data'
);
//str_replace is used in an effort to get as close as possible to the original file name (sanitized by WPForms)
$body = array(
'query' => 'mutation {create_item (board_id: ' . $board_id . ', group_id: "topics", item_name: "' . str_replace("-"," ",pathinfo($upload['name'], PATHINFO_FILENAME)) . '") {id}}'
);
$response = wp_remote_post( MONDAY_URL, ['headers' => $header, 'body' => $body] );
$arr1 = json_decode($response['body'], true);
$item_id = $arr1['data']['create_item']['id'];
//Generate a boundary, needed for the Header (Content-Type) and the Body (to split in multiple parts)
$boundary = wp_generate_password(24);
$eol = "\r\n";
$header = array(
'User-Agent' => MONDAY_TEAM . ' GraphQL Client',
'Authorization' => get_field('mondaykey', 'user_' . get_current_user_id()),
'Content-Type' => 'multipart/form-data; boundary=' . $boundary
);
//We need to build the body from scratch because it is multipart with boundaries
//Take care of the extra empty lines (twice $eol) before the actual query and file, otherwise an error 500 is returned
$body = '--' . $boundary . $eol;
$body .= 'Content-Disposition: form-data; name="query"' . $eol . $eol;
$body .= 'mutation ($file: File!) {add_file_to_column (item_id: ' . $item_id . ', column_id: file, file: $file) {id}}' . $eol;
$body .= '--' . $boundary . $eol;
$body .= 'Content-Disposition: form-data; name="variables[file]"; filename="' . $upload['name'] . '"' . $eol;
$body .= 'Content-Type: ' . $upload['type'] . $eol . $eol;
$body .= file_get_contents($upload['value']);
$body .= $eol . '--' . $boundary . '--';
$response = wp_remote_post( MONDAY_URL . 'file/', ['headers' => $header, 'body' => $body] );
unlink(ABSPATH . substr($upload['value'], 25));
}
Thanks for this @basdebruin, I think it’s a bit above my skillsets though. I understand the concept, but I don’t think I’d be able to get this to technically work properly. We’re trying to achieve this on 3 different forms, feeding data to 3 different boards, as well.
I know you mentioned that you were struggling with this, but to the best of your knowledge, is there a way to handle the file upload via Zapier?
Hey @cccreative – uploading a file isn’t possible using the current modules available in Zapier. However, as Bas mentioned, Integromat does have a module for this.
FINALLY got this working via Integromat. For anyone who wants to achieve this in the future, here’s the scenario I set up. Keep in mind that this scenario works alongside the Contact Form Submissions and CF7 to Webhook plugins, in order attach a webhook to the form, and get the static URL of the uploaded file.
Many thanks to @basdebruin and @dipro for helping me figure this out!