1

I have a site with a webform, which when submitted sends an email. The form has a file upload which should then attach the file as an attachment to the email. I'm not getting any attachment.

I've created a hook for hook_mail_alter and tried hardcoding any attachment to be sent but every email i get has no attachment.

function my_module_mail_alter(&$message) {
  $file_content = file_get_contents('sites/default/files/tmp/test.pdf');
  $message["params"]["attachments"] = array(
    'filecontent' => $file_content,
    'filename' => 'test.pdf',
    'filemime' => 'application/pdf'
  );
}

I've debugged the MailManager.php just before the email is sent and the $message variables param['attachement'] has the valid contents from the code above.

I've instaled modules: SMTP (so i can send emails from my local), MimeMail and enabled MailSystem The emails are being sent without any issue, but again never with an attachement.

I don't know what i'm doing wrong, any ideas?

Core: 8.6

4
  • did you check $file_content has the content, 'sites/default/files/tmp/test.pdf' it may not the correct location ?
    – berramou
    Commented Oct 31, 2018 at 10:58
  • Try to install mailcatcher.me to work with mail in local, it support attachement.
    – berramou
    Commented Oct 31, 2018 at 11:00
  • 2
    Try with this it works for me , $file = file_load($fid); $message["params"]["attachments"][] =[ 'filepath' => $file->uri, ];
    – berramou
    Commented Oct 31, 2018 at 11:03
  • Instead of attaching files to emails you better should just send a download link, IMHO.
    – leymannx
    Commented Oct 31, 2018 at 11:04

1 Answer 1

1

Here's the answer, which is just an extension on the comment from @berramou

$file = file_load($fid);
  $message["params"]["attachments"][] =[
    'filepath' => $file->getFileUri(),
    'filemime' => '' // argument must be present, but can remain empty
  ];
}
2
  • 1
    Please create a ticket in the Webform issue queue so that we can add support for file attachments via SMTP and MimeMail.
    – jrockowitz
    Commented Nov 1, 2018 at 12:51
  • @jrockowitz sure
    – lordZ3d
    Commented Nov 2, 2018 at 13: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.