<p>Finally got this working. I was following the example from Craft Commerce on the <code>$message-&gt;attach()</code> method but all I needed really was <code>$message-&gt;attach($filename);</code></p>
<p>Full module snippet for this is</p>
<pre class="lang-php prettyprint-override"><code>Event::on(
            MailerService::class,
            MailerService::EVENT_BEFORE_SEND,
            function (SendEmailEvent $event) {
                $form = $event-&gt;getForm();

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

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

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

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

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

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

</code></pre>
<p>will likely batch into a Queue job now as well to make the response snappier.</p>

CC BY-SA 4.0
2022-05-24T08:29:27.300Z
2022-05-24T08:29:27.300Z
Steven Grant
<p>I'm building a form that allows a user the ability to attach their authored assets onto the form submission.</p>
<p>I have a custom module that's hooking into <code>MailerService</code> in Freeform and looking up the asset that's been selected.</p>
<pre class="lang-php prettyprint-override"><code>Event::on(
            MailerService::class,
            MailerService::EVENT_BEFORE_SEND,
            function (SendEmailEvent $event) {

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

                $message-&gt;attach(CRAFT_BASE_PATH . '/applications/' . $document-&gt;filename, [
                    'fileName' =&gt; &quot;document.$extension&quot;,
                    'contentType' =&gt; FileHelper::getMimeType($document),
                ]);
            }
        );
</code></pre>
<p>however, Freeform tells me it failed to send the email notification with error</p>
<p><code>finfo_file(my file copy): failed to open stream: No such file or directory</code></p>
<p>where am I going wrong?</p>
<p>removing the <code>$message-&gt;attach</code> and the email sends as normal, just without the attachment.</p>

<emails><plugin-freeform><modules><freeform>
Attach file to Freeform submission
# Attach file to Freeform submission

(Asked by: username_0 on 2022-05-20)

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.

**Tags:** emails, plugin-freeform, modules, freeform


---

## 1 answers

### Answer by username_0 on 2022-05-24. Score: 1 | Accepted answer

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.


---

