Create a file dynamically from a text field and send it via FormIt attachment


I have a textfield in a FormIt form. Now I want to create a file (e.g.PDF File) from one Textfield and put it as attachment to FormIt.

How does the adding as an attachment for FormIt work? (when I create this file dynamically from a text field)

Where do you create this file? In a custom hook?

You probably can just call

$modx->getService('mail', 'mail.modPHPMailer');
$modx->mail->mailer->AddAttachment($file_path, $name);

to add it to the email.

1 Like

What happens if another service/extra sends an email before FormIt does? You probably have to clean up the attachment manually when validation etc. fails.

I believe a prehook would be a betters solution: Hooks - FormIt | MODX Documentation

If I understand the Documentation right, a preHook fires when the form is loaded. But wehn this happend, is could be that the textfield is not filled yet.

Or do I get this wrong?

Thank you for that good starting-point. Yeah my apporach was it to fire it with a customHook.

I agree with @joshualuckers that it is kinda ugly to add an attachment in a hook without sending the actual email.

You could just send the email in your custom hook and not use the email hook at all, or maybe you could try to simulate a successful file upload by adding a new field in your hook that is an array with the keys “tmp_name”, “name” and “error” and check if that gets correctly added as an attachment in the email hook.

Ok, it seem that is not as easy as I thought it. Thanks for the infos.