Private add_uploaded_file
-
Hi,
Last update (5.4) you made the method add_uploaded_file private instead of public in submission.php. Why? Can you please restore it, because many people are using this method to attach files to the e-mail. What is the alternative method?
-
Hi @robm89,
You can still have full access to files, and manually attach them to created PDF etc.
It’s only called a bit differently due to now being placed within an Arrow data structure. Calling now is done adding [0], so it would like (….)[0]
Ziegel, thank you for your input, but your comment makes little sense.
how are you adding [0]? why? where?
if, for example, the attachment tag is [submission_csv] in your contact form mail attachments config;
instead of
$submission->add_uploaded_file('submission_csv', $file);
try this
$properties = $form->get_properties(); $tag = '[submission_csv]'; if (false !== strpos($properties['mail']['attachments'], $tag)) { $properties['mail']['attachments'] = str_replace($tag, '', $properties['mail']['attachments']); $properties['mail']['attachments'] .= "\n" . $file; $form->set_properties( $properties ); }
if you want to always add a file, without a tag in the attachments box, just add it;
$properties = $form->get_properties(); $properties['mail']['attachments'] .= "\n" . $file; $form->set_properties( $properties );
These examples assume
- you are in the wpcf7_before_send_mail hook
- $form is of type WPCF7_ContactForm
- $submission was of type WPCF7_Submission
- $file is a valid path; there is no change in the way paths are handled. If it worked before, it will work again
-
This reply was modified 4 years ago by
Internetbureau Clearsite. Reason: added a solid example
-
This reply was modified 4 years ago by
Internetbureau Clearsite.
for a nice in-line replacement, I am currently using this function in my functions.php
function add_uploaded_file(WPCF7_Submission $submission, $tag, $file, $add_regardless_of_tag = false) { $properties = $submission->get_contact_form()->get_properties(); $tag = '[' . trim(trim($tag, '[]')) . ']'; // ensure '[bla]' format if (false !== strpos($properties['mail']['attachments'], $tag) || $add_regardless_of_tag) { $properties['mail']['attachments'] = str_replace($tag, '', $properties['mail']['attachments']); $properties['mail']['attachments'] .= "\n" . $file; $submission->get_contact_form()->set_properties( $properties ); } }
Now you can replace
$submission->add_uploaded_file('submission_csv', $file);
with
add_uploaded_file($submission, 'submission_csv', $file);
Thanks for sharing your solution in detail! I like your replacement function.
Back then I just ended up using a different hook wpcf7_mail_components, and add the attachment in there.
Simplified example:
add_filter('wpcf7_mail_components', function ($components, $contactForm, $instance) { $components['attachments'][] = 'path/to/file.pdf'; return $components; });
@robm89 I like how simple this solution is, thank you for sharing ??
They also added a new public method called “add_extra_attachments” on version 5.4.
You can just change
$submission->add_uploaded_file(…)for
$submission->add_extra_attachments(‘path-to-file’)@cristianmat changing
$submission->add_uploaded_file(…)
to
$submission->add_extra_attachments(‘path-to-file’)
sounds simple but I’m struggling. My mail is being sent without the attachment.
Can anyone provide an example of how I’d implement
add_extra_attachments
?I’m using this code for dynamic attachments using custom fields:
-
This reply was modified 3 years, 9 months ago by
karl578. Reason: clarification
@karl578 I remember that I had to add the attachment in the form settings of cf7. Did you configure this as well?
Hope it helps..
@robm89 Thank for the reply.
Do you mean in the
File attachments
input on theMail
tab? If so yes.I followed the moometric example when using CF7 v5.3.2 (but v5.4 breaks this example)
-
This reply was modified 3 years, 9 months ago by
karl578.
Hey @karl578,
In what hook are you trying to add the attachment?
I noticed in recent projects that it does not work in the “wpcf7_before_send_mail” hook.you could use something like this; (disclaimer: untested)
<?php add_action('wpcf7_before_send_mail', function () { // get the current CF7 submission $submission = WPCF7_Submission::get_instance(); if (!$submission || !is_a($submission, WPCF7_Submission::class)) { return; } // get the contact form for this subimssion $contact_form = $submission->get_contact_form(); // get the form properties $properties = $contact_form->get_properties(); $file_attachment_mail_tag_to_look_for = '[my_attachment]'; $attachment_needed_in_this_mail = []; // search the mail-tab properties to see in which mail the attachments needs to be sent. foreach ([ 'mail', 'mail_2'] as $property) { // todo: there are filters to add more mails, perhaps there is a way to get them all?? if (false !== strpos($properties[$property]['attachments'], $file_attachment_mail_tag_to_look_for)) { $attachment_needed_in_this_mail[] = $property; } } // which attachment? use your own logic to get the file path here ;) $attachment_file_path = this_is_the_function_that_returns_a_file_path(); // if there is an attachment, filter the mail_components if ($attachment_file_path && is_file($attachment_file_path) && $attachment_needed_in_this_mail && is_array($attachment_needed_in_this_mail)) { add_filter('wpcf7_mail_components', function ($components, $contact_form, $mail) use ($attachment_needed_in_this_mail, $attachment_file_path) { if (in_array($mail->name(), $attachment_needed_in_this_mail)) { $components['attachments'][] = $attachment_file_path; } return $components; }, 11, 3); } });
@clearsite Thank you so much for this code.
Yes I’m trying to use the
wpcf7_before_send_mail
hook.I tried your code. With CF7 5.3.2 my file (local on server) is successfully attached to the sent email (mail_2/auto-responder). However no file is attached once I upgrade to CF7 5.4.1.
Any ideas?
@clearsite Just got your code to work!!!
I messed up the generated path to the file that needed to be attached.
Thank you so much for your help.
- The topic ‘Private add_uploaded_file’ is closed to new replies.