getgrav / grav-plugin-email

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

Email Attachments not working #133

Closed daddyfix closed 4 years ago

daddyfix commented 4 years ago

It doesn't look like the attachment is even added to the outgoing email (from the email log)

Attached Files are uploaded to the server after submit: [ VERIFIED ] Grav Email Logs: [ ON ] Grav Plugin: Email v3.0.8 Grav Plugin: Form v4.0.8 Email Sent: [ YES ]

Could You Please point me in ANY direction, Im stumped.

Grav Email Log

++ Starting Swift_SmtpTransport << 220 smtp.gmail.com ESMTP o94sm407806qtd.34 - gsmtp >> EHLO careercoachplus.ca << 250-smtp.gmail.com at your service, [XX.XX.XX.XX] 250-SIZE 35882577 250-8BITMIME 250-STARTTLS 250-ENHANCEDSTATUSCODES 250-PIPELINING 250-CHUNKING 250 SMTPUTF8 >> STARTTLS << 220 2.0.0 Ready to start TLS >> EHLO careercoachplus.ca << 250-smtp.gmail.com at your service, [XX.XX.XX.XX] 250-SIZE 35882577 250-8BITMIME 250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH 250-ENHANCEDSTATUSCODES 250-PIPELINING 250-CHUNKING 250 SMTPUTF8 >> AUTH LOGIN << 334 VXNlcm5hbWU6 >> cGltZWRvvpYS5jYUBnbWFpbC5jb20= << 334 UGFzc3dvcmQ6 >> cmVubzI2NDc= << 235 2.7.0 Accepted ++ Swift_SmtpTransport started >> MAIL FROM:<mygmail@gmail.com> >> RCPT TO:<somerecipeint@gmail.com> >> RCPT TO:<somerecipient@gmail.com> >> DATA << 250 2.1.0 OK o94sm407806qtd.34 - gsmtp << 250 2.1.5 OK o94sm407806qtd.34 - gsmtp << 250 2.1.5 OK o94sm407806qtd.34 - gsmtp << 354 Go ahead o94sm407806qtd.34 - gsmtp >> . << 250 2.0.0 OK 1588735833 o94sm407806qtd.34

form.md

form:
  fields:
    name:
      type: text
      label: Name
      validate:
        required: true
        message: Please enter your name!
    email:
      type: text
      label: Email
      validate:
        type: email
        required: true
        message: Please enter your email address!
    subject:
      type: text
      label: Subject
      validate:
        required: true
        message: Please enter a subject for your message!
    message:
      type: textarea
      label: Message
      validate:
        required: true
        min: 10
        message: Email message needs to be more than 10 characters long!
    my_file:
      name: my_file
      label: 'Documents'
      type: file
      autofocus: false
      destination: user/uploads
      accept:
        - application/pdf
        - application/x-pdf
        - image/png
        - text/plain

  buttons:
    submit:
      type: submit
      value: Send Email

  process:
    email:
        from: '{{ config.plugins.email.from }}'
        to:
            - '{{ config.plugins.email.from }}'
            - '{{ form.value.email }}'
        subject: '[Contact] {{ form.value.name|e }}'
        body: '{% include ''forms/email_template.html.twig'' %}'
    attachments:
        - 'my_file'
    embedimages:
        - /images/cover_3_email_header.jpg
    save:
        fileprefix: contact-
        dateformat: Ymd-His-u
        extension: txt
        body: '{% include ''forms/data.txt.twig'' %}'
    message: 'Thank you from contacting me. I will get back to you soon.'
    display: /form/thankyou

System.yaml

  js_pipeline: false                             # The JS pipeline is the unification of multiple JS resources into one file
  js_pipeline_include_externals: true            # Include external URLs in the pipeline by default
  js_pipeline_before_excludes: true              # Render the pipeline before any excluded files
  js_minify: false                                # Minify the JS during pipelining

Form.html.twig

...
{% block bottom %}
    {{ assets.js('bottom')|raw }}
{% endblock %}
daddyfix commented 4 years ago

[ SOLVED ]

Part 1 - user/plugins/email/email.php Edit

Modify the sendFormEmail function.

Change the reference from $params['attachments'] to $form->process['attachments']) Lines 120 and 121

protected function sendFormEmail($form, $params, $vars)
    {

        // Build message
        $message = $this->email->buildMessage($params, $vars);

        if (isset($form->process['attachments'])) {
            $filesToAttach = (array)$form->process['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 = ROOT_DIR . $fileValues['path'];
                    }

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

        // Get CID if embed image -- Added by Michael Connors May 5 2020 PiMedia
        if (isset($form->process['embedimages'])) {

            $filesToAttach = (array)$form->process['embedimages'];
            if ($filesToAttach) foreach ($filesToAttach as $fileToAttach) {

                    $filename = __DIR__ . $fileToAttach;

                    try {
                        $cid = $message->embed(\Swift_Image::fromPath($filename));

                        $temp = $message->getBody();
                        $pattern=array();
                        $pattern[0]='/\"cid:.*?\"/';
                        $pattern[1]='/\"\[CID\]\"/';
                        $replacements=array();
                        $replacements[0] = $replacements[1] = '"'.$cid.'"';
                        $adjustedBody = preg_replace($pattern, $replacements, $temp);

                        if (strcmp($temp, $adjustedBody) !== 0) {
                            $message->setBody($adjustedBody);
                        }

                    } catch (\Exception $e) {
                        // Log any issues
                        $this->grav['log']->error($e->getMessage());
                    }
                //}
            }
        }
        else {
            $this->grav['log']->info('embedimages value is not set');
        }

        // Send e-mail
        $this->email->send($message);
    }

Part 2 - User Edit

'destination' CANNOT be outside user/data/ path

    my_file:
      name: my_file
      label: 'Documents'
      type: file
      autofocus: false
      destination: user/data/uploads
      accept:
        - application/pdf
        - application/x-pdf
        - image/png
        - text/plain
Deight8 commented 3 years ago

I don't like to respond to a closed ticket, but I did everything from this procedure and I still don't have an attachment to my email, I just still get an email with this content:

Form label name
- filename

- file_format

- file_size

- file_destination

Grav v1.7.18

Does anyone know the reason?