Solved

Attaching Files to Mail in Unix

Posted on 2000-05-02
3
242 Views
Last Modified: 2010-04-21
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
Comment
Question by:kstefani
3 Comments
 
LVL 2

Expert Comment

by:festive
Comment Utility
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
 
LVL 11

Expert Comment

by:tel2
Comment Utility
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
 

Accepted Solution

by:
DL_Plague earned 100 total points
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now