Solved

Attaching Files to Mail in Unix

Posted on 2000-05-02
3
274 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 2

Expert Comment

by:festive
ID: 2773361
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 12

Expert Comment

by:tel2
ID: 2775834
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
ID: 2852953
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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
A metadevice consists of one or more devices (slices). It can be expanded by adding slices. Then, it can be grown to fill a larger space while the file system is in use. However, not all UNIX file systems (UFS) can be expanded this way. The conca…
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…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…

624 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