Share icon

Hi guys,

In this tutorial we can see how we can send files as attachments in drupal 7 on webform submit using a hook_mail_alter 

1) For this we need Mime Mail module and webform module

2) Create a simple module and use hook_mail_alter like below, In my case "attach_web" is my module name

function attach_web_mail_alter(&$message) {
$f1 =array(
'filecontent' => file_get_contents("https://yourdomain.com/path/of/file/dummy.pdf"),
'filename' => "dummy.pdf",
'filemime' => 'application/pdf'
);
$message['params']['attachments'][] = $f1;
}

3) you can use existing variable under $message variable like $message->id or $message->module etc.

4) For mutliple files you can do like below :

function attach_web_mail_alter(&$message) {
$f1 =array(
'filecontent' => file_get_contents("https://yourdomain.com/path/of/file/dummy.pdf"),
'filename' => "dummy.pdf",
'filemime' => 'application/pdf'
);
$f2 =array(
'filecontent' => file_get_contents("https://yourdomain.com/path/of/file/dummy2.pdf"),
'filename' => "dummy2.pdf",
'filemime' => 'application/pdf'
);
$message['params']['attachments'][] = $f1;
$message['params']['attachments'][] = $f2;
}

Hopes this helps, thanks


Like to add On please Comment your thoughts and feedback below and add something if you found good in anywhere to help others

Hit a like Button If you like the Post.

Many Thanks

Comments

Profile picture for user Duan Camargo
Alexsefiros (not verified)
Fri, 02/04/2022 - 15:58

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.