Given drupal 4.5's feature to attach files to any node, mail allows a file to be attached to the mail node, but does not seem to send it out with the email as an attachment.

Comments

nedjo’s picture

Noted. Attaching files isn't something I've done in PHP mailing. I'd be happy to apply a patch if anyone wants to generate one....

Dublin Drupaller’s picture

I'm wiling to have a go generating that patch if someone could give me some directions on how to start..

Dub

nedjo’s picture

Thanks for your willingness to take this on!

I'm thinking you would need to:

* open and read the uploaded file
* detect its content type
* insert it as an attachment within the body of the mail
* set the header to indicate the email is multipart.

An example is given in the user comments on PHP's mail() function documentation on the PHP site.

Dublin Drupaller’s picture

Thanks for the pointers nedjo...

I'm thinking of doing something a little simpler..i.e.

* check to see if there is an attachment to the mail node
* if there is....run a sendemailwithattachment function.
* if there is no attachement to the node....run the sendmailnormally function.

That way all the person has to do is have the ATTACHMENT module installed.

Am I being too simplistic? or is the above a good approach?

Dub

nedjo’s picture

I don't see any such function in the attachment module (which seems to be for handling uploads, and not sending attachments out).

Dublin Drupaller’s picture

the attachment module allows users to attach a file with a mail node....

i.e. it already does the uploading, checking etc.

I was thinking of just extending out the MAIL.MODULE to check to see if the user has attached a file to the Mail node and run the appropriate function to send an email with attachment....and if the user has not, just run the normal routine for sending a mail..

does that make sense?

Dub

nedjo’s picture

Um, no. The upload module (or the attachment one) already handles the saving of an upload. (I see no advantage in using the attachment module rather than the core upload one.) But to email it out there is no simple "send with attachment" method available. So far as I know, you need to do this manually, through writing the appropriate function. That's why I pointed you to the example on the php site.

Dublin Drupaller’s picture

Ah..sorry..I didnt make myself clear...

I meant when the mail.module is compiling the email and setting up the headers...I could add in extra lines to see if the node has an attachment with it (which is already handled by the attachment module or the upload module). If the mail node DOES have an attached file...when sending the mail..send it this way...and if the mail node DOES NOT have an attachment send it normally..

i.e. in the default workflow settings (with attachment.module installed for example) a drupal admin can specify that MAIL node types may have an attachment or not.

At the moment it is possible to attach a file to a mail node..but the mail.module ignores it. (as was mentioned by a previous poster). the mail.module only looks at the subject and body..i'm merely suggesting it looks at the node's attached file as well...when the mail.module is compiling the email to send out.

Does that make better sense?

Dub

Dublin Drupaller’s picture

Addendum to previous post:

If the above isn't possible..are you saying the only way to do this is to replicate an upload file feature and put it within the mail.module ?

And use the php classes you linked to for compiling the email headers etc.?

Dub

xolotl’s picture

My 2 cents: +1 for using core attachment module.

Dublin Drupaller’s picture

hiya..

am trying to do some work on the patch but I need some tips to progreess..am hoping I can just tweak it in association with the attachment.module...i.e.

--->> with attachment.module installed and set up, it allows you to attach a file to a node - mail nodes included.

bearing that in mind, I was hoping it would be as simple as the following:

-->> check if the node has an attachment to it and if its does setup and send the email with the proper multipart headers (as referenced in the php.net link above)..

-->> if the mail node doesn't have an attachment to it..just setup and send the email normally..

Am unsure to whether that is the case or not...any tips appreciated...

Cheers

Dub

nedjo’s picture

The changes need to be made to the _mail_send() function. Use something like


  if (($file = file_check_upload('upload')) && user_access('upload files')) {
    
  }

to check if a file has been uploaded through the upload.module. If so, detect its content type (I'm not sure how) and add appropriate headers as given on the PHP.net example I pointed you to. Then add the file content by opening and reading it, also as shown on the PHP.net site. You won't be able to use the user_mail() function, though, since it adds a plain text content type header, and we need multipart here.

nedjo’s picture

Keep in mind the parameters returned with the $file object returned by file_check_upload(). You can see these in the function (in file.inc).


    $file->filename = trim(basename($_FILES["edit"]["name"][$source]), '.');
    $file->filemime = $_FILES["edit"]["type"][$source];
    $file->filepath = $_FILES["edit"]["tmp_name"][$source];
    $file->error = $_FILES["edit"]["error"][$source];
    $file->filesize = $_FILES["edit"]["size"][$source];
    $file->source = $source;

So, to adapt the php.net code, you'll use these parameters. Something like:


$message .= "Content-Type: " . $file->filemime . "; name=\"" . $file->filename . "\"\n";
$message .= "Content-Transfer-Encoding: base64\n";
$message .= "Content-Disposition: attachment; filename=\"" . $file->filename . "\"\n\n";

$fp = fopen($file->filepath, 'r');

Also, in sending, you may be able to use user_send() since you'll be redeclaring a content-type header (i.e., including it in the headers sent with the function call), and that might simply override the function-declared header.

Dublin Drupaller’s picture

thanks nedjo...am working on this today and tomorrow (may 21st/22nd) if you have some time to look at a demo.

Dub

Dublin Drupaller’s picture

am almost there with the update...just need a little guidance on how to insert the file attachment....

I'm using an open class file to do other stuff I needed, such as giving the user the option of sending in either HTML or PLAIN TEXT format, choose an alternative character set, Authenticate the address etc. So if I could just nail the final element - the file ATTACHMENT it would be complete as a patch/update.

(I have updated the test module with a more expansive settings page to allow users to set extra capaibliities, such as HTML/PLAN TEXT etc.)

this is how the (enhanced) mail is constructed:

(the html_yes variable is set on the admin/mail settings page and if it's = "1" sends the email in HTML format)

      if (variable_get('html_yes',1)) {$email = new activeMailLib("html");} 
         else {$email = new activeMailLib("plain"); //checks for HTML or PLAIN TEXT format.
      $email->From("$user->name"); //the FROM field in the email
      $email->To($account->mail); //the TO field in the email
      $email->Subject("$node->title"); //the subject for the email.
      $email->Message("$account->mail"); // the message
      if (variable_get('send_attachments',1)) {$email->Attachment($att1,"somename1.txt");} 

The external class opens up the file and does all the encryption etc. with the added option of making an attachment inline. So all I need to do is how to extract a node attachment or attachment array and return

Anyone know how I get the node attachment filename or array into the last line?

Which could have more than one file attachment. i.e.

if (variable_get('send_attachments',1)) {
$email->Attachment($att1,"somename1.txt");
$email->Attachment($att2,"somename2.txt");
} 

the $att1 is the valid file path..

Any help or pointers appreciated. I tried using the php you posted Nedjo, but, it wouldn't work.

Dub

Dublin Drupaller’s picture

StatusFileSize
new7.42 KB

just created a beta mail.module (enhanced). works on drupal 4.6.0. and has the added features mentioned...in particular the file attachment.

I have put it up for testing before submitting a patch for Nedjo to have a look at.

Click here for instructions and to download a copy of the beta...

http://drupal.org/node/23421

Dub

nedjo’s picture

StatusFileSize
new10.49 KB

Thanks for the good work. Here's a somewhat revised version. I've:

* cleaned up the code a bit
* moved the mail sending into its own function
* added format, encoding, etc. to the node editing page, with the module settings page setting defaults
* moved the attachment code to use the upload module (not tested)

IIf you can test, I'm happy to apply this now and then to add more that you might come up with later.

Nedjo

Dublin Drupaller’s picture

just tested it...

Plain text - unusable

* plain text emails with no attachment
* generates errors and sends an email incorrectly formatted. unusable
* after submission the page is full of a copy of every email.

Html option - unusable

* html mails with no attachment
* generates errors and sends an email incorrectly formatted
* after submission the page is full of a copy of every email.

html option/Plain text with attachment -

* all the same as above...unusable and doesn't send attachment.

didn't bother testing it with the other options.

A few minor ergonomics issues:

A) when I edit a mail and submit, does it resend?
b) notice that attachments cannot be removed. Is that wise?

back to the drawing board?
or is there something simple you have forgotten/mistyped that is breaking it completely?

Dub

Dublin Drupaller’s picture

Just noticed that it reaks havoc drupal site as well....(was testing it on 4.5.x)

After you (try) sending a HTML mail..every link on the site is buggered and every page or link you click on displays a page full of sent emails.

Not good.

just noticed this thread has changed to CSV from a 4.5. issue so I'm not sure what version the updated mail.module is supposed to work with.

Just thought I'd flag it Nedjo, probably better to pull the update or specify that it won't work on 4.5.x or 4.6

Dub

nedjo’s picture

StatusFileSize
new10.43 KB

Generally, I like to add new functionality to the head version. I reset this from 4.5 to head because, after the issue was created, Drupal 4.6 was released and I updated the module. The current head version is 4.6 compatible, so patches to head can also be merged into the 4.6 version.

Here's a fixed up and tested version. I've changed the attachment handling to use the $node->files array and added support for resending emails when node is edited. Please note any remaining issues. Thanks!

Dublin Drupaller’s picture

Sorry Nedjo..slightly confused..when you say it's a fixed up version...will it work with 4.5. and/or 4.6?

It's not clear from you're post whether it's fixed up to work with head/4.5./4.6 or Cvs.

Dub

nedjo’s picture

Should work for 4.6. Haven't tested for 4.5.

Dublin Drupaller’s picture

thanks...

when you say it should work..have you actually tested it?

Dub

nedjo’s picture

Changes applied to 4.6 and to head. Thanks for your fine work.

Dublin Drupaller’s picture

nice one nedjo...works brilliantly. great to see a great module get even better.

Just tested it on drupal 4.5. & drupal 4.6. and the same version works fine on both.

cheers
Dub

Anonymous’s picture