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 tried to work out this issue but I'm still unable to send an email with its attachment, can someone post some documentation regarding how to do this in code ?
$fid = 123;
$file = file_load($fid);
$message = array(
'headers' => array('Content-Type' => 'text/html'),
'key' => 'test',
'to' => 'test@example.com',
'from' => 'test@example.com',
'subject' => 'Test email',
'body' => 'test'
);
$message['params']['attachments'][] = array(
'filepath' => $file->uri,
'filename' => $file->filename,
'filemime' => $file->filemime,
'list' => TRUE
);
$system = drupal_mail_system('mimemail', 'test');
$system->format($message);
$result = $system->mail($message);
Comments
Comment #1
sgabe CreditAttribution: sgabe commentedYou need to use drupal_mail() to send messages as the documentation says in the README.txt. The
list
parameter is unnecessary. You can find further information in the Doxygen documentation of the module.Comment #2
Torenware CreditAttribution: Torenware commentedNo offense, but that was not an answer to the question.
The readme has no code example. It is inadequate for the purpose.
Add some working sample code to the readme, and you have an argument.
I've read the development thread for this feature, and the needed info isn't there either. This need documentation, and badly. If you google "Drupal 7" and "attachments", you will see a level of frustration with this issue approaching anger. It really needs a serious solution.
Comment #3
sgabe CreditAttribution: sgabe commentedI will add a page to the handbook ASAP.
Comment #4
sgabe CreditAttribution: sgabe commentedMarked as duplicate #1420916: Sample code still needed for sending attachements with D7.
Comment #5
Ross-Hunter CreditAttribution: Ross-Hunter commentedIs there example code for this yet?
Comment #6
Ross-Hunter CreditAttribution: Ross-Hunter commentedAfter a ton of fun figuring this out, here is some example code for sending an email with an attachment using mime mail.
I have a bigger writeup including sending pdfs on my company's blog.
Comment #7
narcisgirona CreditAttribution: narcisgirona commentedSorry if i'm asking a stupid question..
But what i suppose to do with that code? where i should implement it?
Thanks for you help
Comment #8
Ross-Hunter CreditAttribution: Ross-Hunter commentedThis code would go into a custom module that would be responsible for sending the email. The link I provided talks about it more in detail. My use case was a custom form that users would fill out. Upon submitting the form Drupal would create a node, and then send the user a pdf version of the form they just filled out, as well as send it to administrators based on some other rules I set up in the module.
Comment #9
Ross-Hunter CreditAttribution: Ross-Hunter commenteddouble post.
Comment #10
akhilesh.sawarkar CreditAttribution: akhilesh.sawarkar commentedI tried to send csv file as attachment but its not working for me (using Drupal 7). Email received without attachment.
Here is code from custom module
Please help me.
Comment #11
ghalenir CreditAttribution: ghalenir commented#10 Make sure that you have installed and enabled MIME mail module which is need to send email attachment.
Comment #12
dgtlmoon CreditAttribution: dgtlmoon commentedI have to agree with this issue, I'm a fairly experienced developer and I am also stumped at times.
Problem is, to use this module , the developers assume you have working knowledge of sending emails the "Drupal 7 way", but on the other hand, people are coming across this module because they read somewhere that they need to this to send attachments - so there is a bit of a disconnect there, I think some proper working example in the README would be great
Comment #13
oadaeh CreditAttribution: oadaeh commentedFor people looking for real world examples, the Drupal 7 compatible versions of both Mass Contact and Basic webmail use Mime Mail for sending messages that include HTML and/or attachments. I believe the Webform module also does, but I haven't looked through the code to be sure.
Also, for those wishing to read Ross' blog posting (in comment #6), here is the new address: http://rapiddg.com/blog-post/drupal-7-html-e-mail-pdf-attachments
Comment #13.0
oadaeh CreditAttribution: oadaeh commentedAdded some code
Comment #14
saurabh.tripathi.cs CreditAttribution: saurabh.tripathi.cs commentedYou can use php mail in drupal
sample code:
Comment #15
AnybodyPlease have a look at my comment for version problems between beta1 and alpha2... https://drupal.org/node/2113961#comment-8370229 in #2113961: E-Mail Attachments no more work in 7.x-1.x-beta1 and 7.x-1.x-dev but were working before in alpha2
The difference between "filepath" and "filecontent" seems to be important, but seems to be documented poorly!
Comment #16
splitsplitsplit CreditAttribution: splitsplitsplit commentedOk so I couldn't find a module handbook page and I'm not really sure how to make one. But hopefully this'll save people some pain. This is a working run through of setting up the email module I'm using as of beta2.
Caveats
The code was written when I was relatively new so we can probably improve it! Yep I'm spotting shoddy coding as I write this (looking at that terrible validation). Use it as an example of how the email structure works, not for terrible form coding.
I also need to have a read through about how to best format drupal code for this width, as at the moment it's very difficult to read, and working on a widescreen means I'm pretty lax about it.)
Onto the tutorial
So in this tutorial we'll create a module to use a form to send emails with attachments.
The reason I'll go through everything from scratch is to give a baseline. This should hopefully allow you diagnose problems easier.
To use this tutorial you'll need Mimemail, Mail System and the SMTP authentication module. You can probably skip the last one if you have another way of sending emails and you'll have to modify the mail system module part accordingly.
Step 1
Install both modules. Then we'll need to do some set-up. You can use whatever mail host you want to send email, however I'd recommend using gmail if you're just getting started as then you can copypasta the settings and know they'll work.
Step 2: Setting SMTP Authentication
Head to Admin > Configuration > SMTP Authentication Support
Then turn the module on here. Set the following values:
SMTP Server: smtp.gmail.com
SMTP Port: 465
Use encrypted protocol: Use SSL
Username: *Gmail email goes here*
Password: *Gmail password goes here*
E-mail from address: The email address you want your email sent from.
E-mail from name: The name you want on your emails.
And finally the reason why this module is incredible.
The test section:
We can enter an email to send a test email. Better still we can tick the enable debug checkbox and it'll print out all the SMTP messages. If you're using Gmail, you'll probably find it won't work and you'll need to set-up an application specific password. You should actually see this error in the debug messages along with a hyperlink to the Google help page on setting it up.
This ability to view the header traffic is super useful in making sure your email delivery system is working.
Step 3: Setting up Mimemail
We want to save attachments and to do that we need mimemail.
So head to Admin > Configuration > Mimemail
Choose the sender name, sender email address (this is actually the same two fields we just filled in, in the last step. Both modules have the ability to set these. The mail system module will let you choose which one to use).
I personally enable Full HTML email and include the site style sheets to format my emails.
Step 4: Set -up a Mail System
Click the new class button.
On the format dropdown you want MimeMailSystem.
On the mail dropdown you want SmtpMailSystem.
Save these settings then select this new class for the new "Site-wide default MailSystemInterface class".
I have the Mime Mail Module Class set to MimeMailSystem. (although I'm not sure what it does).
Step 5: Time to create the module.
It's a basic form module, so I'm going to assume you know about, menu, block, permission hooks etc. and focus just on the form parts.
If you just want to send an email without the form, then you can re-purpose the code that goes in the form_submit.
We're going to have two possible attachments with our email. The first is a file saved on the current users account.
I've previously set-up a file field on the user accounts. It's this file we'll be calling. The file field is called
field_user_file_included
The second file is a file the user can upload on the form.
And that's it. That should give you a working form which sends an email with attachments!
Comment #17
pabloroberto27 CreditAttribution: pabloroberto27 commented#16 Your step through was too usefull, I had the smtp module installed and was able to send HTML formatted emails using it, but can´t get the attachments work.
The only problem I faced was a couple of errors error returned by the mime mail module, which said that it expects a valid path instead of the string returned by the file_get_contents function.This error can be solved by adding they key 'filepath' to the params array, like this:
Thanks for the documentation, I would like to see it in the Handbook!
Comment #18
sgabe CreditAttribution: sgabe commentedI have added an example module.
Comment #19
tostinni CreditAttribution: tostinni commentedThanks a lot.
Comment #21
eric.chenchao CreditAttribution: eric.chenchao commentedI have also added some document on StackOverflow http://drupal.stackexchange.com/a/143571/6423
Comment #22
Anonymous (not verified) CreditAttribution: Anonymous commented#6 - FYI - the blog link has moved to http://blog.rapiddg.com/blog/drupal-7-html-e-mail-pdf-attachments
Comment #23
wheretoplaygames CreditAttribution: wheretoplaygames commentedmgantner, thanks for the updated link.
I'm almost thinking it would be easer to just put a link to a file in the emal, and maybe password protect the file/folder. I'm using this for the Entity Event registration.