Link to home
Start Free TrialLog in
Avatar of kstefani
kstefani

asked on

Attaching Files to Mail in Unix

I am creating a shell script which concatenates a few files.  Then if the file exists and has a size greater than 0 I want to email a couple of files.  My problem is that I can't figure out how to attach the files.  I don't want to just include the contents of the files within the e-mail message but attach the files.  Any help?
Avatar of festive
festive

I looked at this some stage back:
what you need to look at is the rfc for mime attachments.

Alternatively you could get someone to send you a similarly attached text file to your unix box and look at the actual spool file in /var/mail/root (or other user name) to determine the format required.

It should be noted that there are mail clients available for most unixes that allow file attachments now.

You could also uuencode any binary files and include the resulting text in the message (messy but effective for some environments).

Hope this helps
Avatar of tel2
How's this, kstefani:

  uuencode file1 attachment1.txt >temp1.dat
  uuencode file2 attachment2.txt >temp2.dat
  mail -s "My Subject" abc@email.com <<EOF
  My message body.
  ~r temp1.dat
  ~r temp2.dat
  EOF
  rm temp1.dat
  rm temp2.dat

NOTES:
- The attachmentX.txt names are what you want the attachment names to be for the recipient.  Eg, you could put myfile.csv for a spreadsheet file.
- If the string "EOF" might appear in the message body on a line by itself, use some other obscure string in its place.
- If you copy & paste the above script to UNIX, MAKE SURE you delete all spaces from the beginning AND END of lines first, otherwise it may not work!
- If you're emailing text file attachments to a non-UNIX environment, then the end-of-line markers may have to be changed for the receiving machine to be able to read it properly.  Let me know if you need help with this.
- I don't know how to control the location of the message body relative to the attachments.  Maybe it can't be done using this mechanism.
- Emailing a single attachment is much simpler.  Eg:
  uuencode file1 attachment1.txt | mail -s "My Subject" abc@email.com
But I don't know if body text can be included using this mechanism.  The first example can be modified to handle any number of attachments.

Let me know how you get on.
ASKER CERTIFIED SOLUTION
Avatar of DL_Plague
DL_Plague

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial