Forums

Remove saved (uploaded) file after succesful send e-mail with attachement

jj135 20 Feb, 2019
Hi! When we have a form that uses a file upload field we need to save that file to an upload folder on the server in order to be able to send it as an attachment with an e-mail. Is there a way to delete the file after the mail has been send so this upload folder will not fill up?

Kind regards,
Jip
healyhatman 20 Feb, 2019
Sure is! With PHP. Google how to delete a file, and you can get the file path with
$this->get("upload#.filefield.path")
Replace the # with the number of your upload action
jj135 20 Feb, 2019
Thanks healyhatman. we'll try to add a custom PHP action in the 'success' part of the mail send action.

It might be easier for CF6 users to have an action for this though...
jj135 20 Feb, 2019
Mmmm, there is no 'success' or 'fail' area inside the e-mail action ;-)
We could add the custom PHP action after the mail action , but I think that might maybe interfere with the sending of the email... I'm not sure if this custom code will 'wait' for the email to be send.
healyhatman 20 Feb, 2019
Maybe. I can whip something up but it'll a) cost you a few dollars and b) be your fault if you put the wrong thing in the delete file path and delete your website.
healyhatman 20 Feb, 2019
The code won't be run until after the email sends, that's not a problem.
jj135 20 Feb, 2019
Yes, it might be risky ;-) Maybe adding an option to the save files action or adding a new action to CF might be a better idea then using custom scripting. So I's leave this as a feature request for CF6 for now. Thanks for your input healyhatman.

As an alternative we can maybe use Cache Cleaner (Regular Labs) to clean the upload folder. Or use a cron script to do this. But we have to be careful not to clean the folder at the same time someone is using it on the site...
healyhatman 20 Feb, 2019
Nah that shouldn't be a problem. I have a cron job that runs a PHP script that deletes all images older than 7 days from my temp folder.

Adding it to the file upload action is a non-starter, because how is it supposed to know WHEN to delete the file?
This topic is locked and no more replies can be posted.