UNIX mail with Attachment
by Bruce Knox  bknox @t uaex.edu     Oracle Database Programmer/Analyst                                       Copyright 2009, 2011     last updated 01/22/11               

Creating email with a text message and an attachment from standard UNIX. 

The following is for instructional purposes and provides a solution to UNIX's mail not directly providing for attachments.  While you can have attachments (by using uuencode piped to mail) or send a text message using mail, you must take some extra steps to include both in the same email. 

The example below has been tested on AIX (Korn Shell) and may be different on other flavors of UNIX.  The example sends a report file (unix_attachment.lst) as an attachment.  The text message file (message.txt) will become the body of the email.

# Create the email message text and the report file.
# Note that the final Line Feed (LF) and
Carriage Return (CR) are critical.
# That is, the last line of text in both files must be ended.

# to check our test files:
# cat message.txt should give:

$ cat message.txt
Message Text Line1 This is the email message text
Message Text Line2
Message Text Line3

Hope this helps, Bruce
$

# the $ prompt above should be on its own line
# cat unix_attachment.lst should give:

$ cat unix_attachment.lst
Report Line1 This is the Attached Report
Report Line2
Report Line3
$

# the $ prompt above should be on its own line

# uuencode the attachment file

/usr/bin/uuencode unix_attachment.lst email_attachment.lst > attachment.txt

# note that the uuencode'd file will contain an internal file label of email_attachment.lst

# concatenate or combine the email message and the uuencode'd attachment

cat message.txt attachment.txt > combined.txt

# the combined text file looks a bit strange:

$ cat combined.txt
Message Text Line1 This is the email message text
Message Text Line2
Message Text Line3

Hope this helps, Bruce
begin 666 email_attachment.lst
M4F5P;W)T($QI;F4Q(%1H:7,@:7,@=&AE($%T=&%C:&5D(%)E<&]R=`I297!O
6<G0@3&EN93(*4F5P;W)T($QI;F4S"FAE
`
end
$


# note that the "begin 666 email_attachment.lst" starts at the begining of a line of text

# finally, mail the combined file

mail -s "Message with Attachment" bknox @t uaex.edu < combined.txt

(Just give it a real email address. Yes, I did get lots of test messages before I broke my example address;)

While instructive, this example leaves a wide trail of work files.  To help you remove them:

rm message.txt
rm unix_attachment.lst
# Note that rm email_attachment.lst is not a file on the sending server.
rm attachment.txt
rm combined.txt

see: http://betwinx.com/formatted_documents_from_oracle_or_unix.htm for how to send plain text that includes formatting and font control.

Disclaimer

Use this information and these scripts at your own risk. As a condition of using these scripts and information from this site, you agree to hold harmless both the University of Arkansas Cooperative Extension Service and Bruce Knox for any problems that they may cause or other situations that may arise from their use, and that neither the Extension Service nor I will be held liable for those consequences.  The scripts and information are provided "as is" without warranty, implied or otherwise.  Limitation of liability will be the amount paid to the University of Arkansas specifically for this information. (It was free:)

http://betwinx.com is my personal site.  betwinx?  This site is largely oriented to Oracle Reporting Solutions in the context of generating bespoken reports from SunGard Higher Education's Banner product.  My university site is a more restrained version which cannot contain endorsements of products or companies.
(I maintain a number of special topic pages with links from this primary site.)                                
View Bruce Knox's profile on LinkedIn  

Near mirror at http://www.uaex.edu/bknox/ which is my university work site.

mirror