I am generating content within my code, and I want to email that content as an attachment. Currently this cannot be done except by creating a temporary file. This patch allows me to pass the content directly to mimemail() like this:


mimemail('to@to.com', from@from.com', 'Subject', 'Body', NULL, array(), NULL,   
  array(array(
    'contents' => 'asdfadsfawer', 
    'filename' => 'gibberish.txt', 
    'filemime' => 'text/plain'
  )));
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

ezheidtmann’s picture

Version: 6.x-1.0-alpha6 » 6.x-1.x-dev

Patch applies against new development version without modification.

sgabe’s picture

Status: Needs review » Needs work

I think this should be done by _mimemail_file(). We can add a $content parameter then it will do the rest. Would you mind modify your patch in that way?

arvana’s picture

I looked into rewriting this patch as requested by sgabe, but with all due respect it seems to me that it makes more sense the way it was originally written by clydefrog. Can you explain the reasoning behind wanting this to be done by _mimemail_file()? If the attachment is a string rather than a file then there isn't really any reason to call _mimemail_file, and as far as I can tell it would take more code to include it there.

For my implementation, the one change I made from the submitted patch was:


    if(!isset($a->list) || $a->list) {
-      if (isset($a->filepath)) {
+      if (!isset($a->contents)) {
        _mimemail_file($a->filepath, $a->filename, $a->filemime, 'attachment');
        $parts = array_merge($parts, _mimemail_file());

ezheidtmann’s picture

FileSize
1.73 KB

I like sgabe's suggestion, actually. It puts the code with similar code. I made this patch, but haven't had a chance to test it yet.

arvana’s picture

Quick testing -- you need the elseif ($content) clause before the elseif ($url) clause. For my use case, I need to pass a path to mimemail -- I am attaching a PDF created by a view passed through the print_pdf module, and the path provides the view that is being converted.

arvana’s picture

Status: Needs work » Needs review
FileSize
1.9 KB

The attached patch works for me.

sgabe’s picture

FileSize
4.89 KB

I think we can do a bit better than that.

@arvana: Why would you need the path? You just need to set the "filecontent". I'm doing something like this and it works fine. Of course this is a dirty hack for me to be able to test this patch.

$attachments = array(
  array(
    'filecontent' => _print_pdf_tcpdf($print, $html, $pdf_filename), // Returns the document as string.
    'filename' => $pdf_filename,
    'filemime' => 'application/pdf'
  )
);
arvana’s picture

Thanks to both of you for giving this issue your kind attention.

@sgabe: In my use case, I am attaching a PDF that is converted from a View. So I'm using print_pdf both to create the $html and the PDF attachment. I am definitely not a real programmer though, especially in Drupal, so I'd be happy to hear if there's a better way to do what I want. Here's my code -- 'ticket' is the View of the content I'm attaching:

<?php
      $path = 'ticket/' . $node->nid;
      $attachments[$counter] = array(
        'contents' => print_pdf_controller($path),
        'filemime' => 'application/pdf',
        'filename' => 'ticket-' . $order->order_id . '.pdf',
      );
?>
sgabe’s picture

That's okay, what I don't understand is why the patch in #7 would not work for you?

arvana’s picture

It doesn't work because I am passing both a $url and $content through _mimemail_file -- it may not be the most common use case, but anyone who wants to attach a PDF created by the print module is going to have to do that. So for this use, it needs to check for $content before it does other things with $url. :)

sgabe’s picture

I am passing both a $url and $content through _mimemail_file

I still don't understand why would you need to pass the $url to _mimemail_file() when the attachment is created by it's content.? :)

Furthermore, in #8 you didn't set the filepath for the attachment, hence the $url will be NULL in _mimemail_file(). So the patch in #7 should work for you.

arvana’s picture

Haha well it DOESN'T work, whereas if I put the $content clause before the $url clause it does. Maybe my diagnosis of WHY it doesn't work is wrong. I will investigate further.

naringas’s picture

(subscribe)
I'm interested in this... my module needs to send generated PDFs (using TCPDF) from a rule

ezheidtmann’s picture

sgabe, your latest patch looks great. And it is working well in my use case:


$attachments = array(array(
    'filecontent' => nodexml_nodexml($node->nid),
    'filename' => $type .'-'. $node->nid .'.xml',
    'filemime' => 'text/xml',
  ));

mimemail($sender, $recipients, $subject, $message, NULL, array(), NULL, $attachments);

Is it now ready to be rolled into the official release?

sgabe’s picture

It looks ready to me, I would like to commit some issues next week. This one included, if arvana can't tell us why would this not work for him.

arvana’s picture

Sorry it took me a while to get back here -- I finally figured out the error. It was my fault; I missed the variable name-change from $attachments[x]['contents'] to $attachments[x]['filecontent'].

So this gets the go-ahead from me to commit. :)

sgabe’s picture

Status: Needs review » Reviewed & tested by the community

Thanks for your feedback, arvana. I guess this can be marked as RTBC.

sgabe’s picture

Version: 6.x-1.x-dev » 7.x-1.x-dev
Status: Reviewed & tested by the community » Patch (to be ported)

Committed to 6.x-1.x, but needs to be ported to 7.x.

sgabe’s picture

Status: Patch (to be ported) » Fixed

Now committed to both branches.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.