0

I am trying to send multiple files attached to an email, but I can only make it work when only one file is attached. I am using mandrill mailer, btw.

Here is the code I have on a form submit:

    $attachments = [];
    if ($files) {
      foreach ($files as $key => $value) {
        $file = File::load($value['target_id']);
        $attachments[] = [
          'filepath' => $file->getFileUri(),
          'filename' => $file->getFileName(),
          'filemime' => $file->getMimeType(),
        ];
      }
    }

    $mailManager = \Drupal::service('plugin.manager.mail');
    $module = 'mymodule';
    $langcode = \Drupal::currentUser()->getPreferredLangcode();
    $params = [
      'subject' => 'Application from ' . $applying_user->get('field_first_name')->value . ' ' . $applying_user->get('field_last_name')->value,
      'candidate_name' => $applying_user->get('field_first_name')->value . ' ' . $applying_user->get('field_last_name')->value,
      'attachments' => $attachments,
    ];
    $mailManager->mail($module, 'email_application', $user_email, $langcode, $params);

Then using hook_mail():

 case 'email_application':
      $message['from'] = \Drupal::config('system.site')->get('mail');
      $message['subject'] = $params['subject'];
      $message['body'][] = buildBodyMail($params);
      $message['attachments'] = $params['attachments'];
      break;

The email I receive does not have any attachment, even though there are 4 files in this particular example meant to be attached.

If I change the code to:

    if ($files) {
      foreach ($files as $key => $value) {
        $file = File::load($value['target_id']);
        $attachments = [
          'filepath' => $file->getFileUri(),
          'filename' => $file->getFileName(),
          'filemime' => $file->getMimeType(),
        ];
      }
    }

...then it attaches only the last file of the set. It seems that

$message['attachments']

does not allow a multidimensional array. I've seen examples with

$message['params']['attachments'] = $params['attachments']; 

and have tried but to no avail.

How can multiple files be attached to an email? Thanks in advance.

2
  • try with something like $message['params']['attachments'][] = $params['attachment']; $message['params']['attachments'][] = $params['attachment2']; – berramou May 1 '20 at 16:55
  • Tried that but does not attach even a single file; only thing that has worked for me is: $message['attachments'], not $message['params']['attachments']. But again, not for multiple file attachments... – itsututa May 1 '20 at 17:40
0

Try as $message['attachments'][] instead of $message['attachments'] example $message['attachments'][] = $params['attachment2'];

1
  • Doesn't work. $attachment = [ 'filepath' => 'sites/default/files/file1.pdf', 'filename' => 'file1.pdf', 'filemime' => 'application/pdf' ]; $attachment2 = [ 'filepath' => 'sites/default/files/file2.pdf', 'filename' => 'file2.pdf', 'filemime' => 'application/pdf' ]; $message['attachments'][] = $attachment; ...does not send any attachment. – itsututa May 2 '20 at 8:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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