Mandril checks $message['attachements'] for an array of paths to files to attach to emails. The mimemail module handles attachments a little differently, it uses $message['params']['attachments'] and stores file info in an array.

As many contrib modules expect you'll be using mimemail for attachments (notably webform in my case), it formats the $message variable accordingly, and mandrill misses the attachements.

I have fixed this in the past using hook_mandrill_mail_alter, but I'm getting sick of doing it for each site I build, so here's a patch ;)

Comments

pjcdawkins’s picture

The patch in the OP works for me. I am combining it with a patch (for Webform) in #2030479 that makes Webform recognise Mandrill as HTML-capable.

pjcdawkins’s picture

My application needs to send attachments with customised names, so it passes the attachment 'filename' to Mime Mail. Mandrill uses 'name'. I've extended the OP's patch to handle this.

pjcdawkins’s picture

StatusFileSize
new1.4 KB

Sorry, I left in a stray watchdog() call.

iztok’s picture

<?php
        if(isset($attachment['uri'])) {
          $attachment_path = drupal_realpath($attachment['uri']);
          if (is_file($attachment_path)) {
            $struct = $mailer->getAttachmentStruct($attachment_path);
            // Allow for customised filenames.
            if (!empty($attachment['filename'])) {
              $struct['name'] = $attachment['filename'];
            }
            $attachments[] = $struct;
          }
        }
        else if ($attachment['filecontent']) {
          $file_buffer = $attachment['filecontent'];
          $file_buffer = chunk_split(base64_encode($file_buffer), 76, "\n");

          $struct['type'] = $attachment['filemime'];
          $struct['name'] = $attachment['filename'];
          $struct['content'] = $file_buffer;
          $attachments[] = $struct;
        }
?>

Wanted to use this patch on commerce_billy_emai but they prepare the attachments in the array in "filecontent" (no URI to the file). With the above code I managed to fix that. If it makes sense I can prepare a patch.

gcb’s picture

tripper54 & pjcdawkins, thanks for the great patch! Rolling it into the development code now.

@Iztok, it would be great to have that fix in the form of a patch. I would say as a general rule that's probably the thing to do if you have code you believe will be of use to others rather than pasting in code out of context. It saves a lot of time for those of us maintaining the module, which greatly increases the odds your code gets included!

Another benefit is that other users can more easily test the code before we get to it. For instructions on creating Drupal patches, if you need them: https://drupal.org/node/707484

I'd also want to make sure that this is the correct way to handle this, or if perhaps we should be altering the way commerce_billy_email deals with attachments to be more in line with what everyone else is doing. I'm closing this issue for now: if you want to, please open up a new one for the commerce_billy_mail compatibility.

gcb’s picture

Status: Needs review » Closed (fixed)

  • Commit f1eceff on 7.x-1.x authored by tripper54, committed by gcb:
    #2152699: Handle Mimemail style attachments. Thanks to pjcdawkins for...
pjcdawkins’s picture

Just a note to say that Mandrill will now send your Webform attachments! (if you have the latest Webform 7.x-4.x-dev)

#2030479: Support Mandrill (and other HTML-capable mail systems via _alter hook)

  • Commit f1eceff on 7.x-1.x, 7.x-2.x authored by tripper54, committed by gcb:
    #2152699: Handle Mimemail style attachments. Thanks to pjcdawkins for...
anybody’s picture

Status: Closed (fixed) » Needs work

I'm sorry to reopen this but as maintainer of the commerce_billy_mail module I have to point out that the patch above is not quite enough as #2183235: Replace mimemail dependency shows (see feedback of Mandrill users!)

The patch is great for all cases where we have a file path. But as #4 says, there are cases where we just have a dynamically generated file that is never being saved to the file system. SMTP and Mimemail module support that via the "filecontent" attribute.

#4 provides a working solution which can also be found in a comment via a hook in #2183235: Replace mimemail dependency so it would make a lot of sense to also handle this condition.

klausi’s picture

Mandrill now also supports this in 2.x, see #2390441: Support inline file attachments with mimemail fileconten.

  • gcb committed f1eceff on 8.x-1.x authored by tripper54
    #2152699: Handle Mimemail style attachments. Thanks to pjcdawkins for...