Score:0

E-mail uploaded file from file field in node to user using Rules + Swiftmailer

kr flag

I have a node type "invoice" which contains a file field "field_invoice" (pdf). I'm setting up following rules action and am searching for a way to rules pick up the file from "field_invoice" and send out an e-mail with the file attached to an user.

I'm on D9.3.x and have Rules + Swiftmailer running properly.

This is my current Rules

  • Event: After saving a new content entity of type Invoice
  • Condition: Entity is of bundle (node, node, Invoice)
  • Action: Send E-mail

So far I'm good. I can access "field_invoice" in the Send E-mail action.

Next step I tried is creating a custom module with this in mymodule.module:

$file->uri = '<what do I put here?>'; // File path
$file->filename = '<and what do I put here?>'; //File name
$file->filemime = 'application/pdf'; //File mime type
$params['attachments'][] = $file;
$mailManager->mail($module, $key, $to, $langcode, $params, NULL, $send);

function mymodule_mail($key, &$message, $params) {
  switch ($key) {
    case 'my_email_key':
      $message['body'][] = $params['body'];
      $message['subject'] = $params['subject'];
      $message['params']['attachments'] = $params['attachments'];
      break;
  }
}

But I have no idea how to let mymodule pick up my Rule and pass the uploaded file when it runs. There are a lot of similar, well-answered questions, but all dating back from the D7-era with outdated information. I find little documentation or use cases on how to achieve this with D8-D9.

Documentation I found but was insufficient to me:

How do you add a PDF attachment to an email?

https://www.drupal.org/project/swiftmailer/issues/2713651

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.