Early Bird Registration for DrupalCon Portland 2024 is open! Register by 23:59 PST on 31 March 2024, to get $100 off your ticket.
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'
)));
Comment | File | Size | Author |
---|---|---|---|
#7 | mimemail.932962_04.patch | 4.89 KB | sgabe |
#6 | mimemail-contents-attach.patch | 1.9 KB | arvana |
#4 | contents.patch | 1.73 KB | ezheidtmann |
mimemail.patch | 1 KB | ezheidtmann | |
Comments
Comment #1
ezheidtmann CreditAttribution: ezheidtmann commentedPatch applies against new development version without modification.
Comment #2
sgabe CreditAttribution: sgabe commentedI 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?
Comment #3
arvana CreditAttribution: arvana commentedI 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:
Comment #4
ezheidtmann CreditAttribution: ezheidtmann commentedI 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.
Comment #5
arvana CreditAttribution: arvana commentedQuick testing -- you need the
elseif ($content)
clause before theelseif ($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.Comment #6
arvana CreditAttribution: arvana commentedThe attached patch works for me.
Comment #7
sgabe CreditAttribution: sgabe commentedI 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.
Comment #8
arvana CreditAttribution: arvana commentedThanks 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:
Comment #9
sgabe CreditAttribution: sgabe commentedThat's okay, what I don't understand is why the patch in #7 would not work for you?
Comment #10
arvana CreditAttribution: arvana commentedIt 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. :)
Comment #11
sgabe CreditAttribution: sgabe commentedI 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.
Comment #12
arvana CreditAttribution: arvana commentedHaha 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.
Comment #13
naringas CreditAttribution: naringas commented(subscribe)
I'm interested in this... my module needs to send generated PDFs (using TCPDF) from a rule
Comment #14
ezheidtmann CreditAttribution: ezheidtmann commentedsgabe, your latest patch looks great. And it is working well in my use case:
Is it now ready to be rolled into the official release?
Comment #15
sgabe CreditAttribution: sgabe commentedIt 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.
Comment #16
arvana CreditAttribution: arvana commentedSorry 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. :)
Comment #17
sgabe CreditAttribution: sgabe commentedThanks for your feedback, arvana. I guess this can be marked as RTBC.
Comment #18
sgabe CreditAttribution: sgabe commentedCommitted to 6.x-1.x, but needs to be ported to 7.x.
Comment #19
sgabe CreditAttribution: sgabe commentedNow committed to both branches.