Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Attaching Files to Mail in Unix

Posted on 2000-05-02
3
Medium Priority
?
281 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 200 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When you do backups in the Solaris Operating System, the file system must be inactive. Otherwise, the output may be inconsistent. A file system is inactive when it's unmounted or it's write-locked by the operating system. Although the fssnap utility…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
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 navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Suggested Courses

721 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