Since file_save_upload in D8 returns an array of file entities, I think it would be very useful if Mimemail were capable of handling file entities directly (in MimeMailFormatHelper::mimeMailHtmlBody).

Comments

hansfn created an issue. See original summary.

tr’s picture

Category: Task » Feature request
tr’s picture

Status: Active » Closed (works as designed)

Actually, Mime Mail already supports this. See the mimemail_example module which uses file_save_upload() to attach files.

hansfn’s picture

I think you missed the fine point of my feature request - handle file entities directly.

It seems counter-productive to (manually) convert file entities to file objects / arrays when Drupal core uses file entities internally. I thought file objects was the Drupal 7 way and file entities the Drupal 8 way.

For the record: It's not only Mime Mail that is stuck in Drupal 7 land when it comes to attachments. I think Swiftmailer have the same problem. Excerpt from a (random) blog post (using Swiftmailer):

        $invoice_pdf = File::load($params['message']['invoice_fid']);
        $invoice_file = (object) [
          'filename' => 'custom_invoice.pdf',
          'uri' => $invoice_pdf->getFileUri(),
          'filemime' => $invoice_pdf->getMimeType(),
        ];
tr’s picture

I think Mime Mail is already handling file entities directly. I have been writing documentation (not yet published) to show the various ways to attach files to email, and one way is to directly use a public://, private://, or a temporary:// URL to attach a file entity. This works.

Try this on your own website - you can add this code to mimemail_example:

/**
 * Implements hook_mail_alter().
 */
function mimemail_example_mail_alter(&$message) {
  // Attach this file to every message sent with Mime Mail.
  $message['params']['attachments'][] = [
    'filepath' => 'public://media-icons/generic/video.png',
  ];
}

That video.png file is available as a file entity on any Drupal site that has the Media module enabled. Or if you have the file entity as a variable $file, then use 'filepath' => $file->getFileUri(),. This is just the same as passing around a node ID to refer to the node entity. We are definitely NOT passing around the file entity contents, just the unique entity URL.

Likewise, what I said above - I use file_save_data() to upload data into a file entity on the website OR to dynamically create file entities on the website for attaching.

I think the confusion come because the $attachments parameter accepted by mimeMailHtmlBody() is an array - but it's an array of *metadata* about the attachment - it's not the attachment itself. In the case of a file entity, the metadata just contains the URL of the file entity, it does not contain the actual data. Again, this is like passing around a node ID.

We don't want to JUST accept a file entity type $attachments parameter because we have other use cases than just file entities - dynamically-created text or image content, for example, where we DON'T want to create a file entity, we just want to attach the data. We need to support more than one use case.

But we do handle file entities directly with that thin metadata wrapper, along with the other use cases, so I think the status of this issue is correct - that already works.

Yes, there still a lot of ugliness in the code and a lot of leftover D5, D6 and D7 stuff, but that's what I've been working hard at eliminating over the past two months. 55 commits over the past two months, including making this module D9 compatible and making the first release in more than 3 years. I've added testing, and in the process found and fixed serious bugs, I've ported the missing submodule mimemail_example, I've written the documentation, etc. I have read through old issues, like this one, and considered them. I am not just closing things because they're old, I'm dealing with each issue on its merits.

There's still a lot of things that either don't work or haven't been shown to work properly. I'm addressing each of those as I find them. You can help by participating in the issue queue: reviewing or posting patches, answering support questions, improving the documentation, etc. I can't continue to put in this much effort on a volunteer basis without community participation, so if this module is important to you please help.

hansfn’s picture

OK, I guess I was considering the file entity as the base / most used case and hence wanted that to be super smooth to use. I'm fine with the current situation - it was just a feature request, not a bug report.

PS! If I had time, I would have helped out. I barely have time to make Views Send work. It's a struggle when the different (Mime) mail modules don't use the same setup for attachments, they don't follow the mail interface (format VS mail) and so on. And I never had time to rewrite the module properly for Drupal 8.