1

I am trying to add an attachment to an email from a form_submit. I can see the attachment loading in the hook_mail() before the email sends, but no attachment is coming through. I also tried to use the swiftmailer_swiftmailer_attach() hook and that doesn't work. The emails are coming over with Content-Type: multipart/alternative;

Here is the code:

// from submitForm()
$params['message'] = $body;
$mail_manager = \Drupal::getContainer()->get('plugin.manager.mail');

$mail_manager->mail('so_assets', 'banner_email', '[email protected]', \Drupal::languageManager()->getDefaultLanguage()->getId(), [
  //'content_type' => "text/html; charset=UTF-8; format=flowed; delsp=yes",
  'from' => '[email protected]',
  'message' => $body,
  'myattachment' => $store_logo[0],
]);


// in .module file
function so_assets_mail($key, &$message, $params) {
  $options = array(
    'langcode' => $message['langcode'],
  );
  //var_dump($params['myattachment']); die(); // I can see the managed file ID here and load it.
  switch ($key) {
    case 'banner_email':
      $message['from'] = \Drupal::config('system.site')->get('mail');
      $message['subject'] = t('New Request');
      $message['body'][] = $params['message'];
      //$message['params']['files'][] = \Drupal\file\Entity\File::load($params['myattachment']);
      break;
  }
}
// I tried this, with and without the file attachment commented out in the hook_mail()
function so_assets_swiftmailer_attach($key) {
  return \Drupal\file\Entity\File::load(5130);
}
3
  • For media attachments, do you need to set the header and content data appropriately (multipart/mixed etc) or does SwiftMailer interpret this and do it for you? See also: stackoverflow.com/questions/25113666/…
    – Kevin
    Aug 7, 2017 at 18:08
  • According to the README, that hook should work. As long as you cleared cache it should fire. You might want to use a debugger to step through the code and see that it actually does fire. However, there is an open issue here too related to what you are asking: drupal.org/node/2841672 PS, you will want to evaluate what the mail $key is in the attach hook so that you are not attaching that to every outgoing email via SwiftMailer.
    – Kevin
    Aug 7, 2017 at 18:20
  • Kevin, are you saying that I should be using the attach hook and not just trying to add the attachment in the case statement? I am looking at that issue and trying to figure out if it applies, thanks. Aug 7, 2017 at 18:37

2 Answers 2

3

Swiftmailer currently expects a plain \stdClass object in files as this part hasn't been fully ported from D7. See https://www.drupal.org/node/2713651.

So you need to do something like

$file = (object) [
'filename' => $file->getFilename(),
'uri' => $file->getFileUri(),
'filemime' => $file->getMimeType(),
];
$message['params']['files'][] = $file;
3
  • Yep, thank you. I finally figured that out. I was going to submit a patch to swiftmailer module to fix the SwiftMailer.php file. I will probably just do something similar to what you have posted here in the meantime. You beat me to my own answer! Aug 7, 2017 at 20:56
  • Would be great if you find time to work on the issue there and update the patch based on my feedback.
    – Berdir
    Aug 7, 2017 at 21:46
  • I would upvote that answer 10 times if I could.
    – xpersonas
    Feb 22, 2018 at 20:55
1

It is also worth mentioning that if you have generated contents rather than a Drupal file, you have to use an array rather than a stdClass, and insert it into "attachments" rather than "files", for example

$file = [
    'filename' => $filename,
    'filemime' => $mimetype,
    'filecontent' => $contents,
] ;
$message['params']['attachments'][] = $file ;

I was getting crazy to get it to work, luckily I could look at Swiftmailer code, which is very clear.

1
  • This is what I was looking for. Thank you it worked for me.
    – Ahmad
    Mar 30, 2021 at 5:46

Your Answer

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

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