2

I can programmatically send emails with an attachment with problem. But after searching I can not seem to find the solution of how to attach multiple files. Does any one have the code to this.

Here's the working code I have for sending email.

$mail = variable_get('site_mail', '');  // sets the site wide email address

      $my_module = 'Email Application';
      $my_mail_token = microtime();
      $from = $sender;  
      $message = array(
        'id' => $my_module,
        'to' => $app_email,  
        'subject' => $subject,
        'body' => $body,
        'headers' => array(
          'From' => $from, 
          'Sender' => $from, 
          'Return-Path' => $from,
  ),
  );

$message['attachments'][] = drupal_realpath($attachment_path);

$system = drupal_mail_system($my_module, $my_mail_token);
0

2 Answers 2

1

You're using $messages['attachments'] as an array, so presumably just add another item to the array:

$message['attachments'][] = drupal_realpath($attachment_path);
$message['attachments'][] = drupal_realpath($another_path);
$message['attachments'][] = drupal_realpath($yet_another_path);
1
  • I should have mentioned that I'd tried that. The error was deeper in my script as I was passing an array to $another_path. Your answer stands correct for my incorrect question though :) So many thanks.
    – Deejay
    Sep 29, 2014 at 12:52
1

My working Drupal 7 solution with module MimeMail without invoking hook_mail():

// Load attachments.
$file1 = file_load($fid1);
$file2 = file_load($fid2);

$to = '[email protected]';
$from = '[email protected]';
$subject = 'Invoice ' . $file->filename;

$module = 'mimemail';
$token = time();

$message = array(
  'id' => $module . '_' . $token,
  'to' => $to,
  'subject' => $subject,
  'body' => array('something text...'),
  'headers' => array(
    'From' => $from,
    'Sender' => $from,
    'Return-Path' => $from,
    'MIME-Version' => '1.0',
    'Content-Type' => 'text/html; charset=UTF-8',
  ),
  'params' => array(
    'attachments' => array(
      0 => array(
        'path' => file_stream_wrapper_get_instance_by_uri($file1->uri)->realpath(),
        'filecontent' => file_get_contents($file1->uri),
        'filename' => $file1->filename,
        'mime' => $file1->filemime,
        'encoding' => 'base64',
        'disposition' => 'attachment',
        'list' => TRUE,
      ),
      1 => array(
        'path' => file_stream_wrapper_get_instance_by_uri($file2->uri)->realpath(),
        'filecontent' => file_get_contents($file2->uri),
        'filename' => $file2->filename,
        'mime' => $file2->filemime,
        'encoding' => 'base64',
        'disposition' => 'attachment',
        'list' => TRUE,
      ),
    ),
  ),
);

$system = drupal_mail_system($module, $token);
$message = $system->format($message);

if ($system->mail($message)) {
  return TRUE;
}
else {
  return FALSE;
}

Your Answer

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

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