Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 290
  • Last Modified:

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?
0
kstefani
Asked:
kstefani
1 Solution
 
festiveCommented:
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
0
 
tel2Commented:
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.
0
 
DL_PlagueCommented:
You could use "sendmail -t" and the body of message is uuncoded.
via popen ".../sendmail -t"
put there:
"From: ...\n"
"To: ....\n"
<uuencoded message body>
".\n"

Also read rfc822
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now