Send mail with attachment using the MIME Mail module I am not able to send mails with an attachement using the MIME Mail module.
There isn't munch documentation about this module, but some people claim they have successfully used the module, and sent a mail with an attachment.

I've enabled and successfully configured:


Mail System
Mime Mail
Swift Mailer


I think I configured the modules correctly.



No mail with attachement is delivered. I call drupal_mail() in a cron job with the following code.

drupal_mail('mymodule', 'notice', $user_email, $lang, $params, $admin_email);


It should invoke any hook_mail() implementations, which it does, but without the attachment.    

function mymodule_mail($key, &amp;$message, $params) {
  $data['user'] = $params['from'];
  $account = $data['user']-&gt;name;

  $file_content = file_get_contents('some/file/path');

  $attachments = array(
     'filecontent' =&gt; $file_content,
     'filename' =&gt; 'example-' . $account,
     'filemime' =&gt; 'application/pdf',
   );

  switch($key) {
    case 'notice':

      $langcode = $message['language']-&gt;language;
      $message['subject'] = 'example submission from '. $account;
      $message['body'][] =
        '&lt;p&gt;'. $account .' has submitted an example.&lt;/p&gt;';
      $message['params']['attachments'][] = $attachments;
      break;
  }
}
The solution I've found is to use hook_mail_alter to attach the file to the email after it's generated and before it's sent. The key to that is making an array of details about your attachment and adding that to $message['params']['attachments'].

hook_mail will send mail, but if you're using another program to send (Swift Mailer or PET), then I believe you'll need hook_mail_alter to alter the mail instead of sending it yourself.

I also suggest using the Form API (also this) to keep track of your file. That's not required, however, just provide the necessary info.

function myhook_mail_alter(&amp;$message) {

  print "message id: {$message['id']}\n";

  // if this email is one of the ones I want to alter:
  if (!empty($message['id']) &amp;&amp; (preg_match('/^message-id-set-by-formmail[\d]{1,2}/', $message['id']))) {

    // $message['params']['body'] is a single string, not an array
    $result = preg_match('/\[my dumb string with node id# (\d+)]/', $message['params']['body'], $matches);

    print "Message body: \n {$message['params']['body']} \n\n Matches:\n";
    print_r($matches);

    if (!empty($matches[1])) {
      $file_id = $matches[1];
      $my_pdf = file_load($file_id);
      $original_attachments = $message['params']['attachments'];
      // $original_attachments is an array of associative arrays, each bearing details about an attachment.
      // Add an associative array about this desired attachment.
      $my_attachment = array(
        'filecontent' =&gt; file_get_contents($my_pdf-&gt;uri),
        'filemime' =&gt; $my_pdf-&gt;filemime,
        'filename' =&gt; $my_pdf-&gt;filename,
        'filepath' =&gt; NULL,
      );
      $message['params']['attachments'][] = $my_attachment;
    }

  }
}