How to Attach Files to Emails using Webform in Drupal

By default, Drupal does not support allowing a user to upload a file in a webform which can then be sent as an email attachment. Drupal can accomplish this using the contributed modules Webform and Swift Mailer.

The Webform module is a comprehensive module that allows you to create and configure webforms. In a nutshell, it will enable you to create a webform with many variations of form fields. After submission, the results can be sent via customizable emails.

This tutorial will focus on creating a file upload field whereby a user can upload a local file into a webform.

The Swift Mailer module extends the basic email sending functionality of Drupal by delegating all email handling to the Swift Mailer library so you can accomplish things such as sending file attachments in emails.

The Swift Mailer library is a free PHP mailer built on the Symphony framework which offers a flexible and elegant object-oriented approach to sending emails with many features.

With these two contributed modules, we can achieve the functionality of allowing a user to upload a file to a webform and sending it as an email attachment.

Getting Started

We will install the Webform and Swift Mailer modules and dependencies and then create a webform with a file upload field. Then we will configure Swift Mailer to allow sending emails with file attachments.

This module requires no additional libraries and can simply be installed with composer.

Using Composer:

composer require drupal/webform

Go ahead and install Webform and Webform UI.

Install and Enable the Swift Mailer Module

The Swift Mailer module depends on the mailsystem module responsible for making the Swift Mailer module available to Drupal. Using composer, you can simply download the Swift Mailer module, automatically downloading its dependency.

Using composer:

composer require drupal/swiftmailer

Once downloaded go and install the “Swift Mailer” module.

Configure Swift Mailer

Now let’s configure Swift Mailer to ensure it can successfully send an email.

Go to Configuration >> Swift Mailer >> Transport.

Under Transport Types, fill in the correct transport types settings for your mail server. Your settings will vary depending on your server configuration.

You can test your settings by clicking on the “Test” tab on the same page.

If you get this error message below:

Then the server your site is running on hasn’t been configured to send emails. If you’re testing this on a local version of Drupal. There’s a good chance your local machine hasn’t been configured to send emails.

Configure Mail System

Once Swift Mailer has been configured, go into Mail System (Configuration >> Mail System).

Select “Swift Mailer” from the Formatter and Sender drop-down in “Default Mail System”.

In my experience, if this isn’t configured correctly then the file won’t attach to the email.

Create Webform with a File Field

Let’s create a simple webform and then add a file field.

1. Go to Structure >> Webforms and click on “Add Webform”.

2. Give your Webform a title and then click on “Save”.

3. On the “Build” tab of your newly created Webform, click on “Add element”.

4. On the next screen, type in “file” in the “filter by element name” textfield.

Now you should see a listing of the different kinds of file upload field elements we can add.

TIP: If you do not see any File Upload Elements, it is most likely because you did not set your Drupal private files path. Webform stores uploaded files in Drupal’s “private” files directory. To set your Drupal private files directory, find this setting in settings.php

$settings['file_private_path'] = ‘...’;

See here for more information on setting the Drupal private files path. After setting your private files path, clear your cache.

5. Let’s add a generic file upload field element. Click on “Add element” for File.

6. In the modal popup, enter all the details for this file upload field. For the purposes of this tutorial, we will just give it a “Title” and leave the rest of the settings to the default. Click on “Save” to save your new field element.

If we were to view the Webform at this point, it will look as follows:

7. Now let’s set up the Email Handler for our Webform that will actually send the email with the uploaded file as an attachment.

In your Webform, click on Settings >> Email / Handlers >> Add email

8. In the modal popup, fill in all the details, i.e., “To” and “From” email address and the subject.

For this tutorial, we will leave all the default settings with the exception of enabling “Include files as attachments”. It is important to check this box so that the uploaded file will be sent as an attachment to the email.

You’ll also need to expand “Included email values/Markup” and include the file element in the email.

Once everything is has been configured click on Save.

Test Webform

Go to the URL of your Webform and try uploading a file and submitting your form.

Momentarily you should receive an email with an attachment for the uploaded file.

And that’s it! You have successfully configured Webform and Swift Mailer.

Summary

Drupal, by default, does not ship with the functionality of allowing a user to upload a file via a webform field which can then be sent as an email attachment. We have shown how you can accomplish this using the Webform and Swift Mailer contributed modules.

FAQs

Q: Emails are being sent, but attachments are not working?

Make sure you check “Include files as attachments” in the email handler and include the file in “Include email values/markup”.

About The Author

7 thoughts on “How to Attach Files to Emails using Webform in Drupal”

    1. That error means that the server hasn’t been configured to send out emails.

      Just google “unable to send email error” to find a fix.

      This issue is beyond the scope of this tutorial.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top