Hi
Some one can help me how to attach a file in contact form mail.

Comments

dotmundo’s picture

Out of the box, Drupal 8 does not support HTML formatted emails or emails with file attachments. Thus the first thing you need to install is the Swift Mailer module and most likely the Mail System module.

Second, lookup online the Swift Mailer API documentation and you will need code similar to:

            $finfo = finfo_open(FILEINFO_MIME_TYPE);
            $attachments = new \stdClass();
            $attachments->uri = $pdf_path;
            $attachments->filemime = finfo_file($finfo, $pdf_path);
            $params['files'][] = $attachments;

Finally, add to $params the usual email parameters and pass that to hook_mail. Within that hook you are going to need code similar to the following:

            if ( isset($params['files']) ) {
                $message['params']['files'][] = $params['files'];
            }

Good luck.

steniya’s picture

I've been struggling with an issue today that I just can't seem to figure out.

I have a webform that includes a file upload that I want to email upon submission.

The email sends correctly (using MIMEmail modules) but there is no attachment.

The webform is for job application.The user will upload the resume and portfolio. The hr and manager want to get mail along with attachment.How they can always be logged into site that is quite impossible. Now the issue is the who ever logged into the site only can see the attachment when click on the link. How to sent attachment from private folder as attachment not just in link(want proper attachment)

I'm running drupal 8. USed modules are webform, mime mail, mail system and also tried swift mailer.

But in webform module mail settings , there is "Include files as attachments" i checked that option. Still it is not working.

Is this fuctionality support sending mail with attachment from private folder as attachment not just in link

dotmundo’s picture

The code posted earlier is what I use for sending attachments however for the problem you are describing I suspect you need to have modules installed that supports this like the ones I had mentioned.

tgrass’s picture

Hei, thank you for your advise. What is the $pdf_path in your example? Any idea why my version below does not work?

            $attachment = new \stdClass();
            $attachment->uri = $_FILES['file']['tmp_name'];
            $attachment->filename = 'my-file-name.jpg';
            $attachment->filemime = $_FILES['file']['type'];
            $params['files'][] = $attachment;

Thanks,
Thomas

dotmundo’s picture

This link might be helpful: https://arrea-systems.com/Install_use_SwiftMailer_Drupal_8_(part_2_implementation)

Also how do you know if $_FILES['file']['type'] is returning your MIME type in the correct format? Use finfo_file like I did in my example.

tgrass’s picture

Thank you for your fast help. Your code and your link did already worked, i just had the problem that SwiftMailer wasn't configured properly in my development environment.

Just as additional info: The attachment in my case will be provided in a form with an file field. I implemented it as an multiple file field. Our working solution (with real mime type and real filename) is:

    for ($i = 0; $i < count($_FILES['file']['tmp_name']); $i++) {
       $finfo = finfo_open(FILEINFO_MIME_TYPE);
       $attachment = new \stdClass();
       $attachment->uri = $_FILES['file']['tmp_name'][$i];
       $attachment->filename = $_FILES['file']['name'][$i];
       $attachment->filemime = finfo_file($finfo, $_FILES['file']['tmp_name'][$i]);
       $params['files'][] = $attachment;
    }
aahmd2’s picture

I realize this thread is a few months old now, but I am new to drupal and coding a website for the university I work for. I have a contact form with a required image upload field. When someone fills out the form and it comes to my email, the image attachment is always a link, which requires you to be logged onto the site. While this is okay, it still is extra work just to see the image attachments. Is there a way to make it an attachment in the email?

Is there a way to make it show up as an attachment by editing the email template (Webform / E-mails / Email Template)? As I said, I am new to drupal, I don't know how to install SwiftMailer or anything really.

xedeon’s picture

Hi,

I am trying to implement your solution, can you post your mail_alter module so I can see where the code above goes?

Thanks

sam452’s picture

... is my customer's default mailer for Drupal 8. I wonder how I can use this mailer instead of SwiftMailer?