Sterc / FormIt

A dynamic form processing Snippet for MODX Revolution
https://docs.modx.com/current/en/extras/formit
33 stars 58 forks source link

FormIt email hook without attachment? #122

Closed pixelchutes closed 7 years ago

pixelchutes commented 7 years ago

When using FormIt to process file uploads, it currently seems impossible to also rely on the email hook for notifications without attaching the uploaded file(s).

For example, imagine you are processing very large files (e.g. videos), and need an email alert. You don't necessarily want the video file itself emailed, and in fact most times the email will fail to send due to the file size being too large.

It would be great if you could optionally turn off email attachments when working with FormIt for file uploads + built-in email hook.

EDIT: Currently, the only workaround is to unset any tmp_name in a separate hook processed after you've handled the uploaded content, but before the email hooks runs, which works, but not very intuitive / clean...

// Prevent subsequent FormIt email hook from attaching file
foreach ($fields as $k => &$v)
{
    if (is_array($v) && !empty($v['tmp_name']))
    {
        unset($v['tmp_name']);
    }
}
$hook->setValues($fields);

return true;
wuuti commented 7 years ago

We have run into it as well. The formit email hook assumes (wrongly imho) that if any field is an array and contains a key "tmp_name", that it came from the post $_FILES (but could have also come from a custom hook which sets array fields...)

Further: If you have a preprocessing hook which moves the uploaded files (in our case to an upload media source), maybe sanitizes, the tmp_name will get invalid, which causes FormIt to deliver the emails, but without any body content and any log message. That could only be solved by adding a file_exists check on the tmp_names, or with @pixelchutes suggestion to unset (or simply set to empty) the processed file fields. But that is only a workaround.

It would be even better to have

a) a hook option like &emailAttachFiles=0 to turn off the automatic file attaching b) and the check if the files are still where they are expected.

joeke commented 7 years ago

@pixelchutes @wuuti This is on the list for the 3.0.3 release. We've been caught up in the encryption migration for 3.0 and some other fixes. Hopefullly the 3.0.3 release shouldn't take too long, I'll keep you posted :).

joeke commented 7 years ago

I've added the property 'attachFilesToEmail' to the FormIt snippet which controls if the file fields from the form are added as attachment. The property defaults to true, so if you want to disable the attachments you should add &attachFilesToEmail=0 to your FormIt call. This will be included in the upcoming 3.0.3 version.