getgrav / grav-plugin-email

Grav Email Plugin
http://getgrav.org
MIT License
37 stars 29 forks source link

onSendEmail Event Sometimes Fails to Include Attachments #138

Closed thekenshow closed 1 year ago

thekenshow commented 3 years ago

Based on this issue I was able to create an onEmailSend() function and modify the routing of form email based on form select values. So far, so good (great, actually!).

But when I applied my approach to forms with attachments, one of two problems occur. Here is a sample, where example-form has no attachments and works fine, but example-form-2 shows the two issues:

    public function onEmailSend(Event $e)
    {
        switch ($e['vars']['form']->name()) {
            case 'example-form':
                if ($e['vars']['form']->value('category') == 'Customer Service') {
                    $e['params'] = array('to' => array('cs@example.com'));
                }
                elseif ($e['vars']['form']->value('category') == 'Careers') {
                    $e['params'] = array('to' => array('hr@example.com', 'another@example.com'));
                }
                else {
                    $e['params'] = array('to' => array('info@example.com'));
                }

                break;

            case 'example-form-2':
                // CASE A: The following will send but without attachments
                $e['params'] = array('to' => array('thekenshow@sent.as', 'thekenshow@live.ca'));

                // CASE B: The following never completes to confirmation message and nothing is sent
               $e['params'] += array('cc' => 'thekenshow@live.ca');

                break;

        }
    }

I've also seen the CASE B fail-to-send problem with the $e['params'] = array('to'... version.

For CASE A above, the email arrives with a description of the attachment but nothing attached:

Attach up to 3 files (DOC/DOCX, XLS/XLSX, PDF. Maximum 5 Mb each):   
   * Test.doc
   * application/msword
   * 222720
   * user/data/example-form/attachments/Test.doc

The form.en.md process section looks like this:

    process:
        -
            save:
                fileprefix: rfp-
                dateformat: Ymd-His-u
                extension: txt
                body: '{% include ''forms/data.txt.twig'' %}'
        -
            email:
                -
                    subject: '[Example] from {{ form.value.contactname|e }}'
                    body: '{% include ''forms/data.html.twig'' %}'
                    to: hr@example.com
                    to_name: 'Example Form List'
                    reply_to: '{{ form.value.contactemail|e }}'
                    attachments:
                        - exampleattach

The attachment field configuration looks like this:

            name: example
            label: 'Attach up to 3 files (DOC/DOCX, XLS/XLSX, PDF. Maximum 5 Mb each)'
            display_label: true
            outerclasses: 'field-group file'
            classes: field-control
            type: file
            multiple: true
            limit: 3
            filesize: 5
            destination: user/data/example-form/attachments
            avoid_overwriting: true
            accept:
                - application/msword
                - application/vnd.openxmlformats-officedocument.wordprocessingml.document
                - application/vnd.ms-excel
                - application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
                - application/pdf
                - text/plain

Whether or not avoid_overwriting is true seems to make no difference.

rhukster commented 1 year ago

Cleaning up old issues. please read: https://getgrav.org/blog/new-email-plugin

Symfony/Mailer is very different, attachments are much easier: https://symfony.com/doc/current/mailer.html#file-attachments