Submit a form with an attachment

Hello to everybody,

I go through the forum and issues on github for over an hour to find a solution on how to send an attachment using the form.

  1. The visitor fills in the form and attaches a picture
  2. Sends it to the system
  3. I will receive an email with information + an attachment he has added

title: Reklamace
form:
    name: reklamace
    fields:
        content:
            type: section
            title: 'Ověřovací informace'
            underline: true
            title_level: h4
            classes: col-12
        name:
            label: 'Celé jméno'
            type: text
            autofocus: 'on'
            validate:
                required: true
            autocomplete: 'on'
        company:
            label: Firma
            type: text
            validate: null
            autocomplete: 'on'
        email:
            label: E-mail
            type: email
            validate:
                required: true
                rule: email
            autocomplete: 'on'
        
        phone:
            label: Telefon
            type: tel
            validate:
                required: true
            autocomplete: 'on'
        
        custom_file:
            name: priloha
            type: file
            label: 'Vyberte fotku'
            multiple: true
            random_name: false
            limit: 10
            accept:
                - 'image/*'
        agree_to_terms:
            type: checkbox
            label: 'Accept'
            validate:
                required: true
        g-recaptcha-response:
            label: Captcha
            type: captcha
            recaptcha_not_validated: 'Neověřeno'
    buttons:
        submit:
            type: submit
            value: 'Send'
    process:
        captcha: true
        save:
            fileprefix: contact-
            dateformat: Ymd-His-u
            extension: txt
            body: '{% include ''forms/data.txt.twig'' %}'
        email:
            to: **myEmail**
            from: '{{ form.value.email|e }}'
            subject: '[REKLAMACE] {{ form.value.email|e }}'
            body: '{% include ''forms/data.html.twig'' %}'
            attachments:
                - priloha
        message: 'E-mail sent'
        display: odeslano

Body Mail looks:


Celé jméno : test
Firma : Name
E-mail : [mail](mailto:mail)
Telefon : Phone
Vyberte fotku :

  * 20210818115146-eshop_cz.PNG
  * image/png
  * 100408
  * upload_data_users/20210818115146-eshop_cz.PNG

Accept: Yes

I have everything set up and functional, but the attachment will be displayed in the email only as information where the uploaded file is located.

How do I attach it to an email as an attachment?

Thank you very much everyone, for their time.

@Deight, You might want to take a look at the Email plugin and especially its events onEmailSend and onEmailMessage.

You can catch these events in your own custom plugin and manipulate the params.

I’ll definitely go through them, but I don’t understand why the email attachment doesn’t work as an email attachment, don’t you know the reason?

@Deight, Forget my previous reply. I wasn’t aware of the existence of the attachment option in the email action.

I don’t know the reason why it doesn’t work for you. I do spot a difference between your form definition and the one shown in the docs for the Email plugin: destination: user/data/files field.

destination works great, upload works with both the multiupload variant and individual files, but the problem is that it does not attach it to the email as an attachment.

Thank you for your time.

@Deight, There are quite a few issues with respect to attachment at the repo of the Email plugin. Maybe you can find some pointers over there. If not, you might consider opening a new issue.

3 hrs and solution is here.

user\plugins\form\templates\forms\default\data.html.twig

From:

{% for val in value %}
    {% if val is iterable %}
          <ul>
                 {% for v in val %}
                         <li>{{ string(v)|e }}</li>
                 {% endfor %}
          </ul>
    {% else %}
          <li>{{ string(val)|e }}</li>
    {% endif %}
{% endfor %}

Edit to this:

{% for val in value %}
           {% if val is iterable %}
                 <ul>
		 {% for item in form.value(field.name) %}
		        <li><a href="**SITE_URL**{{ string(item.path) }}">{{ item.name }}</a></li>
	         {% endfor %}
           </ul>
          {% else %}
         <li>{{ string(form.value(field.name))|nl2br }}</li>
   {% endif %}
{% endfor %}

I find solution here: Contact form and file upload - display images in data.{html|text}.twig

But have a one other problem, the files show cicling whan you add more that one file.
I try to find solution for this.

@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

I tried it, but it doesn’t work for me, the attachment won’t be sent, I also found that it was solved here:

But the new version should have it fixed.

Others advise you to set the final upload folder without characters like: “-”, “_” …

I’ve checked the server that sends it again, thinking it’s deleting my attachments, but that wasn’t it.

Another advice is to simplify writing, but in the end only the developer of the form plugin writes that he fixed it, I updated everything, but it still doesn’t work for me and there is no error anywhere :frowning: