Submit a form with an attachment

@Deight,

Maybe I’m mistaken, but I believe you wanted a file attachment added to an email. Isn’t the code you are showing only adding a url to the email?

By the way, it is not a good idea to edit files of plugins/themes. A better solution is to copy the template into your (inherited/custom) theme and edit that copied file.

Anyway, I’ve taken some time to have a look at the Email plugin and try to add an attachment to an email.

Using a fresh install of Grav 1.7.18, I did the following:

  • Created page 03.contact/form.md with the following form definition:
      ---
      title: Contact Form
    
      form:
          name: contact
    
          fields:
              name:
                label: Name
                type: text
                validate:
                  required: true
    
              email:
                label: Email
                type: email
                validate:
                  required: true
    
              myfile:
                 label: 'Add a file'
                 type: file
                 multiple: false
                 destination: user/data/files
                 accept:
                    - application/pdf
                    - application/x-pdf
                    - image/png
                    - text/plain
    
          buttons:
              submit:
                type: submit
                value: Submit
    
          process:
             email:
                subject: "New file submitted by {{ form.value.name|e }}"
                body: |
                   Document submitted by user {{ form.value.name }}: 
                   {{ (form.value.myfile | first).name }}
                attachments:
                   - myfile
             message: Thank you for getting in touch!
      ---
    
  • Filled in the form in the browser
  • Got the following email sent to me:
    Untitled
1 Like