How to send an email via a GRAV form with a picture attachment?

User should have the option to upload an attachment via browser. I’ve tried to use the Filepicker field to add a picture, but completely don’t understand how to attach the picture file to the email. Besides that the email form works. I#m using SMTP.

I would need an example how to exactly do it.

I found some hints in this forum, but they seem to be completely outdated. I didn’t find an example in the GRAV documentation.

Would be nice if someone could help. Thanks.

@David_Lee, This reply to post Submit a form with an attachment contains an example that adds a file attachment to an email send by a form.

@pamtbaau

Thanks for the link. Yes this / your solution works but has some flaws. For example the picture / files i attach are always stored as page media. I don’t wont to store the attachments as page media, i want them only to be sent as email attachment. Any ideas on that?

Also: how do i do comments in the frontmatter section?

As you might notice, i’m pretty new to GRAV. :wink:

@David_Lee, Hm… I presume you mean you only want the file to be emailed and not stored on the server?

I’m not sure, but I guess that will not be possible out-of-the-box. The ‘file’ field is meant to upload files and that is what it does: Uploading a file to the server.

However I can think of two options to clear the uploaded file(s).

Custom plugin

  • One can define custom actions to be executed after the form has been processed. For example:
    process:
      email:
        ...
      deleteupload: ''
    
  • A custom plugin could then catch event onFormProcessed (see link above), extract the filename from the form data and delete the file.
    public function onFormProcessed(Event $event) {
      $action = $event['action'];
    
      switch ($action) {
         case 'deleteupload':
            $form = $event['form'];
    
            foreach (array_keys($form->data['myfile']) as $key) {
               $fileInstance = File::instance($key);
               $fileInstance->delete();
            };
    
            break;
      }                
    }
    

Manually clean-up files
Or, if programming is not your cup of tea, you might ‘send’ the uploaded files to /tmp/uploads’ and delete the files from time to time.

That’s kind of true, but not really.

After submit, file is stored in a global $_FILES only till the script finishes. If you don’t move the file where you want, it will not be saved on the server. So basically with a custom plugin you could attach uploaded file and send email instead of saving it to some location.

Correct me if I’m wrong :thinking:

@Karmalakas, I’m not sure if I understand you correctly…

After submit, file is stored in a global $_FILES only till the script finishes

Event onFormProcessed is called for every action, but global variable $_FILES is empty during all of these calls.

If you don’t move the file where you want, it will not be saved on the server.

What do you mean by ‘If you don’t move’?

On closer inspection…

It seems there are two instances when the uploaded file gets saved:

  • The Email plugin calls the Form plugin to save the file to destination.
  • The Form plugin itself also writes the file to disk, when action upload (undocumented) is either not defined or !== false.

In other words…

  • If the email action is defined, the file will be written to disk.
  • If the email action is not defined, the file will also be written to disk, except when action upload is defined and === false

Apparently, if an email needs to be sent without saving the file, a custom plugin is required:

  • When not using email action in form:
    Construct/send an email with attachment manually.
    Action upload: false needs to be defined too.
  • When using email action in form:
    Delete the file afterwards.

Any suggestion/feedback/correction is welcome…

OK, so there’s some action happening under the hood in Grav itself :thinking: Gotcha

@pamtbaau

Need some more time to study your attempt above. Will come back to that and have to mention that i would have solved the problems in 15 minutes if i could use php and would know how to integrate it in GRAV and would understand how GRAV exactly works. At the moment i’m far away form understanding the basics. Only trial and error, which is annoying.

At the moment i’m reading the GRAV documentation and stumbled over GRAV Flex. To me that looks like it can solve all the problems i have at the moment with forms (sending pictures, accessing arrays of form selects…), because you can also use it for forms.

What do you think. Is it worth invest time in GRAV Flex?