Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
I'm wondering how to attach a file to an email sent using drupal_mail using htmlmail and mailmime? My use case is attaching an invoice PDF generated as a string by the Print module using the print_pdf_generate_path() function. I have this working in mimemail module but I'd like to try this with htmlmail/mailmime. My mimemail code looks like:
module_load_include('inc', 'print_pdf', 'print_pdf.pages'); // require print_pdf.pages.inc
// Prepare attachment.
$path = 'node/' . $details['invoice']->nid;
$attachments[] = array( // attach PDF
'filecontent' => print_pdf_generate_path($path), // call the print_pdf function to generate PDF content string
'filemime' => 'application/pdf',
'filename' => 'Invoice.pdf',
'filepath' => NULL,
);
// Send email.
$params = array(
'context' => array(
'subject' => 'Invoice',
'body' => 'Your invoice is attached.',
),
'plaintext' => FALSE,
'attachments' => $attachments,
);
drupal_mail('mimemail', 'invoice', $details['email'], language_default(), $params);
Could you please post some example code showing how to achieve the same thing using htmlmail?
Comments
Comment #1
lmeurs CreditAttribution: lmeurs commented+1
Comment #2
B-Prod CreditAttribution: B-Prod commentedHere is a function that allows to send attachments. Please note that in my case, the module it belongs to has htmlmail and mailmime as dependencies. If you do not intend to do as well, you will need to add some extra tests.
Note also that the patch 1 from #1518322: addAttachment method: error in the $location variable name is welcome to avoid error messages.
Here is a way to call this function :
@pillarsdotnet: I would be pleased to help adding such functionality inside HtmlMail module, but before I would like to know if it is a desired feature or not.
Comment #3
pillarsdotnet CreditAttribution: pillarsdotnet commented@B-Prod -- Patches are always welcome.
Comment #4
B-Prod CreditAttribution: B-Prod commentedActually I did not provide a patch because it is a workaround to answer tanc's question.
If you really want to add such feature, I would be glad to help, but it seems to me that it is something related to HTML mail module rather than maill MIME.
Since you also are a maintainer of HTML mail module, please let me know if you want to add this feature. If so, I will create a regular patch in a separate issue.
Moreover, if you need some help for maintaining the HTML mail module, I would be happy to contribute as a co-maintainer.
Comment #5
pillarsdotnet CreditAttribution: pillarsdotnet commented@#4
Please submit patch and I'll see about adding you as co-maintainer. Help is always welcome; I'm swamped with work right now.
Comment #6
bluetegu CreditAttribution: bluetegu commentedHi B-Prod,
#2 was a great help for me. I did remove all lines related to mailsystem settings (i.e. set and clear), cause I think it messed up my mailsystem settings for other modules configured through the mailsystem admin form. I simply configured the settings manually instead. I didn't investigate this, so my issue may be related to something else altogether, but I thought its worth reporting.
Thanks,
Ron
Comment #7
B-Prod CreditAttribution: B-Prod commented@pillarsdotnet: sorry for the delay, I don't know why this issue was lost from my dashboard... Co-maintaining is not necessary at all, the patch below affects HTML mail module.
This patch allows users that have enabled Mail MIME module to send attachments. Some comments in the project page would be welcome if this feature is accepted. A starting point may be in the
HTMLMailSystem::handleAttachments()
method comments:The HTML Mail test form has also be updated, to allow users to test file attachments. I created 2 inputs, one for internal path, other for external file. The first file is used as this (file URI), the second is uploaded in the Drupal temporary directory and its content is used, not its URI. This allows to tests the "is_file" feature and also to remove the temporary file immediately.
I attached a blank PDF file to this comment, because when putting this file in the patch, the PDF file is corrupted when applying it (patch command does not accept binary files). The "blank.pdf" file goes under HTML Mail module root directory.
Comment #8
semiaddict CreditAttribution: semiaddict commentedThank you B-Prod.
This worked great for me.
Comment #9
B-Prod CreditAttribution: B-Prod commentedSet as RTBC from comment #8 above.
@pillarsdotnet: is this new feature going to be ported in a future release?
Comment #10
webankit CreditAttribution: webankit commented#8 It works:-)
If there is an image it gets embedded rather than being an attachment
Comment #11
yogeshchaugule8 CreditAttribution: yogeshchaugule8 commentedThe #7 patch worked for me when I'm returning the filepath in $attachments['file'] not when returning the file data in it..
Comment #12
AnybodyImportant for D7 I think: #1389504: Documentation for sending emails with attachments
Comment #13
stewart.adam CreditAttribution: stewart.adam commentedNote that although this patch works, Mail MIME is currently broken on PHP 5.4 and above due to its dependency on outdated PEAR libraries.
Edit: it would appear that this issue restores compatibility for now #1564790: E_STRICT compatibility
Comment #14
salvis@stewart.adam: I'm a bit confused by #13 vs. #2293273: PDF attachments truncated in GMail web interface.
So, please review and test it, so we can commit it.
Comment #15
lmeurs CreditAttribution: lmeurs commented@B-Prod: Thanks a million, works like a charm!
Comment #16
lmeurs CreditAttribution: lmeurs commentedMinor problem... When opening the test form after the patch has been applied, I received the following warning:
htmlmail_test_form()
tries to load the default test values usingvariable_get()
. When default test values have been stored previously, the$defaults
array does not contain theinternal_file
element yet. This element is used later on to give a value to the form field, that's why the warning appeared.Suggestion
On form submit
internal_file
's value is being checked and set in case it has not been set before. This checking should probably be done after loading the defaults and before building the form.Comment #17
zeroduh CreditAttribution: zeroduh commentedWell, this is my 2 cents. For my setup, after applying the patch, I adjust some of the patch code, because my attachments array are returning slightly diferent (using Drupal 7.31, Rules 7.x-2.7, HtmlMail 7.x-2.65+10-dev and MailMime 7.x-2.18+2-dev ). I think the file was not loaded, for me was something like this:
So, I've adde the file load functions from a uri path. If you are having the same problem, just replace the function with this:
Hope that helps!
Comment #18
Alan D. CreditAttribution: Alan D. commentedBased off zeroduh comment, this probably needs a review.
Using #1341772: File attachments patch, I successfully sent emails with attachments using MailMime.
Code was as simple as:
Hope this helps others :)
Comment #20
salvisThank you for your feedback, Alan D. — it's great if you got it to work, but I wish we could clean up the current mess and make it work for everyone.
If #1341772: File attachments works for you, how about RTBC'ing it?
Also still pending:
#1564790: E_STRICT compatibility
#2390001: Strict Warnings
Here in this thread we have only one patch that does not apply. Did you use it? How did you get it to apply? Do you have an updated patch? What exactly do you mean that needs review?
Where does your snippet go?
Comment #21
Alan D. CreditAttribution: Alan D. commentedThis thread was Needs review as per @zeroduh comment, I didn't personally attempt to see if this one worked.
"Did you use it?", manually applied from memory. That patch was the first I tried and it worked, but this thread looked like it was more comprehensive so I didn't want to rtbtc the other one....
Comment #22
salvisI'm in a bad spot here — I volunteered to co-maintain HTML Mail and Mail MIME to try to keep them afloat in the absence of the maintainer, but I never have used nor will ever use either of them, so I must rely on those who actively use them to tell me exactly what works and how...
Comment #23
lmeurs CreditAttribution: lmeurs commentedFew years ago I used the patch from this issue to send attachments. Thanks Alan D. for linking to #1341772: File attachments, today I successfully tested HTML Mail 7.x-2.71 with Mail MIME, without the patch from this issue.
I set the status to duplicate. Thanks all!