You need to login to send post. No account yet? Create one:
Create account

Drupal Weform e-mail with file attachment

No replies
Lone Wolf

Web design is not ending with design website itself, but with theming of all output of website - include e-mails. Drupal's webform is sending e-mail with content of form automatically, but what will happens, when you want attach to this e-mail some file (image, pdf etc.)? First you need is theming of e-mail itself. This is done by copying file webform-mail.tpl.php from webform folder to your theme folder.

  1. Rename the file using your webform node ID in this way: webform-mail-NID.tpl.php
  2. Change content of the file in this way:

<?php
  $hash = md5("fretzerte");
  ob_start();
  print "--".$hash."\n"."Content-Type: text/plain; charset=ISO-8859-1\n";
?>

<?php print t('Submitted on @date', array('@date' => format_date(time(), 'small'))) ?>

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

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

<?php
  print theme('webform_mail_fields', 0, $form_values['submitted_tree'], $node);
?>

<?php

    print "--".$hash."\n"
      ."Content-Type: MIME_TYPE; name=NAME_OF_FILE"."\n"
      ."Content-Disposition: attachment; filename=NAME_OF_FILE"."\n"
      ."Content-Transfer-Encoding: base64\n\n"
      .chunk_split(base64_encode(file_get_contents('sites/FOLDER_OF_SITE/files/NAME_OF_FILE')))."\n";

   print "--".$hash."--";
   print(ob_get_clean());
?>

Mime type for your file you can find here.

At second you need to change header of e-mail. This you can do in template.php in your theme folder by adding:

function YOUR_THEME_NAME_webform_mail_headers($form_values, $node, $sid, $cid) {

  if (NID == $form_values['details']['nid']) {
    $hash = md5("fretzerte");
    $headers = array(
      "Content-Type" => "multipart/mixed; boundary=\"".$hash."\"",
      "X-Mailer" => 'Drupal Webform (PHP/'.phpversion().')',
    );
  } else {
    $headers = array(
      'X-Mailer' => 'Drupal Webform (PHP/'. phpversion() .')',
    );
  }
  return $headers;
}

Don't forget clear the cache!