I have a webform where a user can upload a file; I want this file to be attached to the email sent to the site manager.

What is the best practice doing so? I tried finding a helper module, but I didn't find one.


4 Answers 4


Or you can use Mail System and Mime Mail modules. A "Include files as attachments" checkbox will appear in Webform E-Mails configuration.

  • 6
    If you do not see the "Include files as attachments" checkbox even after enabling the Mime Mail module, make sure that the variable webform_email_html_capable is set to 1. The Webform module only sets this variable at installation time, so it will not be updated if you add the Mime Mail module later. You can set this variable to 1 just disabling and re-enabling the Webform module, or using drush vset webform_email_html_capable 1. Note that the use of HTMLMail or MailChimp modules instead of Mime Mail is also a valid option. Oct 15, 2015 at 13:57
  • 1
    Before the option of "Include files as attachments" appears on the webform > emails configuration page, you also need to make sure that on /admin/config/content/webform "HTML mail system" is checked.
    – wotney
    Nov 8, 2016 at 18:14

You can use Mimemail module to send a file as attachment in webform, and also you can choose file types.


  • It permits users to receive HTML email and can be used by other modules. The mail functionality accepts an HTML message body, mime-endcodes it and sends it.
  • If the HTML has embedded graphics, these graphics are MIME-encoded and included as a message attachment.
  • Adopts your site's style by automatically including your theme's stylesheet files in a themeable HTML message format
  • If the recipient's preference is available and they prefer plaintext, the HTML will be converted to plain text and sent as-is. Otherwise, the email will be sent in themeable HTML with a plaintext alternative.
  • Allows you to theme messages with a specific mailkey.
  • Converts CSS styles into inline style attributes.
  • Provides simple system actions and Rules actions to send HTML email with embedded images and attachments.

Note that there are some serious limitations you need to take into consideration when creating email layouts, see the Email Standard Project for more information.


First Paste this code in your current theme Template.php file

function yourthemename_webform_mail_headers($node, $submission, $email) {
  $attach_bool = false;
  foreach ($node->webform['components'] as $item) {
    if ($item['type'] = 'file') {
      $attach_bool = true;  // found a file component!
  $headers = array(
    'X-Mailer' => 'Drupal Webform (PHP/' . phpversion() . ')',
  if ($attach_bool) {
    $hash = md5('randomstring');  // remember you can change randomstring to anything you want, just make sure it's consistent w/ part 2
    $headers['Content-Type'] = "multipart/mixed; boundary=\"".$hash."\"";
  return $headers;

Don't forget change youthemename with your theme name

2) Now Create webform-mail.tpl.php in your current theme directory and paste this code.

// $Id: webform-mail.tpl.php,v 2010/03/25 02:07:29 quicksketch Exp $

* @file
* Customize the e-mails sent by Webform after successful submission.
* This file may be renamed "webform-mail-[nid].tpl.php" to target a
* specific webform e-mail on your site. Or you can leave it
* "webform-mail.tpl.php" to affect all webform e-mails on your site.
* Available variables:
* - $node: The node object for this webform.
* - $submission: The webform submission.
* - $email: The entire e-mail configuration settings.
* - $user: The current user submitting the form.
* - $ip_address: The IP address of the user submitting the form.
* The $email['email'] variable can be used to send different e-mails to different users
* when using the "default" e-mail template.
$hash = md5('randomstring');  // refer to part 1
$attachments = '';
foreach ($node->webform['components'] as $item) {  // loop through each webform component
  if ($item['type'] == 'file') {  // is it a file?
    if ($submission->data[$item['cid']]) {  // did the user attach a file?
      $result = db_query_range('SELECT f.filename, f.filepath, f.filemime FROM {files} f WHERE f.fid = ' . $submission->data[$item['cid']]['value'][0], 0, 1);
      // query the database for the file information
      $file = db_fetch_array($result);
      $attachments[] = array(
        'mime' => $file['filemime'],
        'path' => $file['filepath'],
        'name' => $file['filename'],
        'data' => chunk_split(base64_encode(file_get_contents($file['filepath']))));

if (is_array($attachments)) {
  print "--".$hash."\n"
    ."Content-Type: text/plain; charset=ISO-8859-1\n";
<?php print t('Submitted on %date'); ?>

<?php if ($user->uid): ?>
<?php print t('Submitted by user: %username'); ?>
<?php else: ?>
<?php print t('Submitted by anonymous user: [%ip_address]'); ?>
<?php endif; ?>

<?php print t('Submitted values are') ?>:


<?php print t('The results of this submission may be viewed at:') ?>


if (is_array($attachments)) { // let's attach the files now
  foreach ($attachments as $attachment) {
    print "--".$hash."\n"
      ."Content-Type: ".$attachments['mime']."; name=".$attachment['name']."\n"
      ."Content-Disposition: attachment; filename=".$attachment['name']."\n"
      ."Content-Transfer-Encoding: base64\n\n"
  print "--".$hash."--";

Now clear cache and done and taken from this source and I never used this before.

  • Someone downvoted this - can they explain why ?
    – commonpike
    Mar 16, 2020 at 10:51

I think webform_multifile module useful for what u want.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.