1

I'm building a form that allows a user the ability to attach their authored assets onto the form submission.

I have a custom module that's hooking into MailerService in Freeform and looking up the asset that's been selected.

Event::on(
            MailerService::class,
            MailerService::EVENT_BEFORE_SEND,
            function (SendEmailEvent $event) {

                $formHandle = 'apply';
                if ($event->getForm()->getHandle() != $formHandle) {
                    return false;
                }
                // hardcoding the asset id for testing
                $document = Asset::find()->id(23438)->one();
                
                $extension = pathinfo($document->filename, PATHINFO_EXTENSION);
                $spaces = new Spaces(App::env('SPACES_API_KEY'), App::env('SPACES_SECRET'));
                $space = $spaces->space(App::env('SPACES_BUCKET'));
                
                $file = $space->file($document->filename);
                $file->download(CRAFT_BASE_PATH . '/applications/' . $document->filename);
                $message = $event->getMessage();

                $message->attach(CRAFT_BASE_PATH . '/applications/' . $document->filename, [
                    'fileName' => "document.$extension",
                    'contentType' => FileHelper::getMimeType($document),
                ]);
            }
        );

however, Freeform tells me it failed to send the email notification with error

finfo_file(my file copy): failed to open stream: No such file or directory

where am I going wrong?

removing the $message->attach and the email sends as normal, just without the attachment.

1 Answer 1

1

Finally got this working. I was following the example from Craft Commerce on the $message->attach() method but all I needed really was $message->attach($filename);

Full module snippet for this is

Event::on(
            MailerService::class,
            MailerService::EVENT_BEFORE_SEND,
            function (SendEmailEvent $event) {
                $form = $event->getForm();

                if ($form->getName() != self::APPLICATION_FORM) {
                    return false;
                }

                $existingDoc = $event->getForm()->get('existingDoc')->getValue();

                if (!empty($existingDoc)) {
                    $doc = Asset::find()->id($existingDoc)->one();

                    $spaces = new Spaces(App::env('SPACES_API_KEY'), App::env('SPACES_SECRET'));
                    $space = $spaces->space(App::env('SPACES_BUCKET'));

                    $file = $space->file($doc->filename);
                    $filename = CRAFT_BASE_PATH . '/applications/' . $doc->filename;
                    $file->download($filename);

                    $message = $event->getMessage();
                    $message->attach($filename);
                }
            }
        );

will likely batch into a Queue job now as well to make the response snappier.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.