by Bruce Knox bknox @t uaex.edu Oracle Database Programmer/Analyst Copyright 2009 last updated 05/26/10

Creating email with a text message and an attachment from standard UNIX. mirror at http://betwinx.com/email_with_attachment.htm
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
`
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
# 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
`
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:
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
rm unix_attachment.lst
# Note that rm email_attachment.lst is not a file on the sending server.
rm attachment.txt
rm combined.txt
No comments:
Post a Comment