I'm hesitant to submit this as a bug as it could be that I'm just not understanding the code properly but it seems as if programatically sent emails with attachments have the attachment reset. I am using the drupal_mail function to send an email and running the mimemail code through a debugger I can see that my specified attachment params are being reset to a basic array with one key called filepath which is set to an empty array. I believe the offending code in mimemail is:

/**
 * Implements hook_mail().
 */
function mimemail_mail($key, &$message, $params) {
  $context = $params['context'];

  // Prepare the array of the attachments.
  $attachments = array();
  $attachments_string = trim($params['attachments']);
  if (!empty($attachments_string)) {
    $attachment_lines = array_filter(explode("\n", trim($attachments_string)));
    foreach ($attachment_lines as $filepath) {
      $attachments[] = array(
        'filepath' => trim($filepath),
      );
    }
  }

My code to send an email is:

$attachments[] = array(   // attach PDF
    'filecontent' => print_pdf_generate_path($path),  // call the print_pdf function to generate PDF content string
    'filemime' => 'application/pdf',
    'filename' => 'Invoice.pdf',
    'filepath' => NULL,
  );

  // Send email.
  $params = array(
    'context' => array(
      'subject' => 'Invoice',
      'body' => 'Your invoice is attached.',
    ),
    'plaintext' => FALSE,
    'attachments' => $attachments,
  );
  drupal_mail('mimemail', 'invoice', $details['email'], language_default(), $params);

I can get this to work by changing the mimemail_mail function so that it looks something like:

/**
 * Implements hook_mail().
 */
function mimemail_mail($key, &$message, $params) {
  $context = $params['context'];

  $attachments = $params['attachments'];

Can you let me know what I might be doing wrong or if its a bug I'll happily help submit a patch to fix it if you can explain the issue.

Comments

sgabe’s picture

Category: bug » support
Status: Active » Fixed

Please take a look at drupal_mail(). The first parameter of drupal_mail() is a module name to invoke hook_mail() on. The {$module}_mail() hook will be called to complete the $message structure which will already contain common defaults.

As the README.txt says, you can send message via Mime Mail by specifing MimeMailSystem as the responsible mail system for a particular message or all mail sent by one module. This doesn't mean to call drupal_mail('mimemail', ... ). You should use the name of your own custom module which is sending the message. So your custom hook_mail() implementation will be invoked and everything will be just fine.

tanc’s picture

Excellent, that makes sense, thanks for your quick reply.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.