Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

General

Trying to attach file to email with error

Solved by pamtbaau View solution

Started by Duc 4 weeks ago · 3 replies · 48 views
4 weeks ago

My contact form is modular and sending email with and without a file is working. Files are saved to the folder and I am receiving emails. Now I want to add the file to the email as an attachment, but getting this error when I add the attachments: resume under process.

error:

PHP
Whoops \ Exception \ ErrorException (E_WARNING)
Undefined array key "path"

/var/www/httpd/jcb/user/plugins/email/email.php
                break;
        }
    }

    protected function sendFormEmail($form, $params, $vars, $event)
    {
        // Build message
        $message = $this->email->buildMessage($params, $vars);
        $locator = $this->grav['locator'];

        if (isset($params['attachments'])) {
            $filesToAttach = (array)$params['attachments'];
            if ($filesToAttach) foreach ($filesToAttach as $fileToAttach) {
                $filesValues = $form->value($fileToAttach);

                if ($filesValues) foreach($filesValues as $fileValues) {
                    if (isset($fileValues['file'])) {
                        $filename = $fileValues['file'];
                    } else {
                        $filename = $fileValues['path'];
                    }

                    $filename = $locator->findResource($filename, true, true);

                    try {
                        $message->attachFromPath($filename);
                    } catch (\Exception $e) {
                        // Log any issues
                        $this->grav['log']->error($e->getMessage());
                    }
                }
            }
        }

        //fire event to apply optional signers
        $this->grav->fireEvent('onEmailMessage', new Event(['message' => $message, 'params' => $params, 'form' => $form]));

        // Send e-mail

         $status = $this->email->send($message);

Arguments
"Undefined array key "path""

the form:

YAML
form:
  name: email/jobs
  action: /job
  fields:
    name:
      id: name
      label: Name
      classes: 'form-control form-control-lg'
      placeholder: 'Enter your name'
      autocomplete: 'on'
      type: text
      validate:
        required: true
    email:
      id: email
      classes: 'form-control form-control-lg'
      label: Email
      placeholder: 'Enter your email address'
      type: email
      validate:
        rule: email
        pattern: "^\w([\w\.+-]*[\w-])?@([\w-]+\.)+[a-z]{2,5}$"
        required: true

    resume:
      label: Resume
      destination: 'user/data/email/jobs/uploads'
      type: file
      multiple: true
      autofocus: false
      limit: 5
      accept:
        - application/pdf
        - .docx
        - .doc

    message:
      label: Message
      classes: 'form-control form-control-lg'
      size: long
      placeholder: 'Enter your message'
      type: textarea
      validate:
        required: true
    captcha:
      type: turnstile
      theme: light

  buttons:
    submit:
      type: submit
      value: Submit
      classes: 'btn btn-primary btn-block'
    reset:
      type: reset
      value: Reset
      classes: 'btn btn-primary btn-block'

  process:
    turnstile: true
    upload: true
    email:
      from: '{{ config.plugins.email.from }}'
      to:
        - '{{ config.plugins.email.from }}'
      subject: '[Job Inquiry] {{ form.value.name|e }}'
      body: "{% include 'forms/data.html.twig' %}"
      attachments:  ## <---- THE ERROR HAPPENS WHEN THIS IS ADDED
        - resume    ## < ---- THE ERROR HAPPENS WHEN THIS IS ADDED
    save:
      fileprefix: 'job-inquiry-[{{ form.value.name|e }}]-'
      dateformat: Ymd-His-u
      extension: txt
      body: "{% include 'forms/data.txt.twig' %}"
    message: 'Your email was sent successfully.'
    display: thank-you-inquiry

4 weeks ago Solution

@duceduc, I can reproduce the error using your form definition.

However...

  • What's the purpose of action upload: true?
    There is no action "upload" defined by the email plugin. See https://learn.getgrav.org/17/forms/forms/reference-form-actions.
    Did you create a custom action? If so, what's the code of it?
  • When removing action upload: true the form works fine. The email is send with attachment.
👍 2
4 weeks ago

Thank you. I don't have such custom action. That line may have been added from trying other codes to get file upload working.

4 weeks ago

If the answer above solved your problem, please mark it as a solution 🙏

Suggested topics

Topic Participants Replies Views Activity
General · by Jerry Hunt, 4 days ago
2 74 6 hours ago
General · by pamtbaau, 11 hours ago
1 47 11 hours ago
General · by Andy Miller, 23 hours ago
0 43 23 hours ago
General · by Marcel, 12 months ago
6 346 5 days ago
General · by Duc , 5 days ago
3 39 5 days ago