mail an attachment + body

mail an attachment + body

Post by J » Sat, 28 Jun 2003 05:22:18



How can I mail an attachment file + a few lines in body.  I have to
use uuencode.

I tried following but does not work.



TIA!

 
 
 

mail an attachment + body

Post by j.. » Sat, 28 Jun 2003 05:42:47



> How can I mail an attachment file + a few lines in body.  I have to
> use uuencode.

> I tried following but does not work.




Isn't this a FAQ? If not, why not?

At any rate, take a look at the last bazillion times it's been asked:

http://groups.google.com/groups?as_q=mail+attachment&num=100&as_scori...

 
 
 

mail an attachment + body

Post by llewell » Sat, 28 Jun 2003 13:31:44




>> How can I mail an attachment file + a few lines in body.  I have to
>> use uuencode.

>> I tried following but does not work.



> Isn't this a FAQ? If not, why not?

> At any rate, take a look at the last bazillion times it's been asked:

> http://groups.google.com/groups?as_q=mail+attachment&num=100&as_scori...

In less likely to wrap form:

http://groups.google.com/groups?as_q=mail+attachment&as_ugroup=comp.u...

 
 
 

mail an attachment + body

Post by Bill Marcu » Sat, 28 Jun 2003 09:37:01


On 26 Jun 2003 13:22:18 -0700, J M


> How can I mail an attachment file + a few lines in body.  I have to
> use uuencode.

> I tried following but does not work.



> TIA!

(1) Dos2unix is for text files, not for gif or other binary files.
(2) You can't direct input from a pipe and a file to the same command
at the same time.
You could try this:


but it would be easier to use a mail client such as pine or mutt (or almost
anything other than mail or mailx).

--
It is often the case that the man who can't tell a lie thinks he is the best
judge of one.
                -- Mark Twain, "Pudd'nhead Wilson's Calendar"

 
 
 

mail an attachment + body

Post by Rich » Sat, 28 Jun 2003 22:17:36


In infinite wisdom Bill Marcum answered:

> On 26 Jun 2003 13:22:18 -0700, J M
>   <jagma...@attbi.com> wrote:

>>How can I mail an attachment file + a few lines in body.  I have to
>>use uuencode.

>>I tried following but does not work.

>>dos2unix file.gif | uuencode file.gif | mailx -s "test" -r my@email
>>other@email < bodyfile.txt

>>TIA!

> (1) Dos2unix is for text files, not for gif or other binary files.
> (2) You can't direct input from a pipe and a file to the same command
> at the same time.
> You could try this:
> (cat bodyfile.txt; uuencode file.gif < file.gif) | mailx -s "test" my@email
> other@email

> but it would be easier to use a mail client such as pine or mutt (or almost
> anything other than mail or mailx).

I tried mutt. It does add an attachment, the filename is lost.

I found a better solution. From this web page...

        Sending files as email attachments

        http://home.clara.net/dwotton/unix/mail_files.htm

It's a bourne shell script that encodes with perl (which seems
pretty standard on most distributions today).

It has a few shortcomings, it could allow BCC, but does not. But
it's simple, fast, and it works.

What the hell, sometimes links go down and I think this useful
enough to be archived to google. I'm appending the script and
me at's off to it's author, Dave Wotton. It's well documented
and the file name is 'mail_files'.

One warning, it seems that netscape has wrapped a few lines.
You'll have to fix this before using it. It looks like only
comment lines are wrapped, simple enough to fix.

Rich

=================

#!/bin/sh

# script to email files as attachments.
# ------------------------------------

# Additional documentation for this script, including a brief introdcution
# to MIME can be found at:
http://home.clara.net/dwotton/unix/mail_files.htm

# Written: Dave Wotton, July 1998, (Cambridge UK)
#          This script comes with no warranty or support. You are
#          free to modify it as you wish, but please retain an
#          acknowledgement of my original authorship.

# Amended: Dave Wotton, 6/3/99
#          -t flag now optional. subject also optional
#
# Amended: Dave Wotton, 3/8/00
#          added -b and -u  options. By default a file-list which is not
#          preceded by a -n, -b, or -u flag is now NOT encoded (the previous
#          default was to base64 encode it.).
#
# Amended: Dave Wotton, 10/10/00
#          added a -c (cc:) option.
#          Added a tty -s test to prevent the prompt to enter the text body
#          being displayed when not connected to a tty. (The text body is
#          still required though. /dev/null will suffice.)
#
# Amended: Dave Wotton, 24/2/01
#          Now uses perl to perform the base64 encoding, as it comes as
#          standard on most modern Unixes. (You need the perl MIME package
#          though, which I believe is standard. )

# Amended: Dave Wotton, 22/09/01
#          Now creates a "To:" header and uses the sendmail -t flag to
#          duplicate this as the envelope recipients, rather than using the
#          user supplied list of addresses simply as envelope recipients.
#          This confused some mail clients, specifically Lotus Notes.

# Amended: Dave Wotton, 30/09/01
#          Now initialises the main variables, so that previously set
#          environment variable values (eg. $CC) aren't used instead.
#          Enable multiple occurrences of the -t and -c flags. Thanks to
#          Jason Judge for these suggestions.

# Usage:   mail_files [-t] mailid [ -c mailid ] [ -s subject ] [ -f
mailid ]
#          [-n file_list] [-u file_list] [-b file_list] file_list
#
#    -f      : The mailid of the sender ( defaults to your userid )
#              Only userids that have been defined as "trusted" in the
sendmail
#              config file can make use of the -f option. For
non-trusted users
#              any value specified by this parameter will be ignored by
#              sendmail.
#    -t      : The mailid of the recipient. Mandatory, no default
#              multiple mailids can be specified, separated by commas.
#    -c      : The mailid of any carbon-copy recipients. Optional.
#              multiple mailids can be specified, separated by commas.
#    -s      : The subject string. Optional, default = "Not specified".
#              Enclose in quotes.
#    -n      : no-encode: indicates a list of files which are NOT to be
base64
#              or uuencode encoded. Multiple files may be enclosed in double
#              quotes. Usual wildcard notation can be used. This option is
#              for completeness and can be omitted because the default
action
#              is not to encode the file-list.
#    -b      : base64 encoding: indicates a list of files which are to be
#              base64 encoded. Multiple files may be enclosed in double
quotes.
#              Usual wildcard notation can be used.
#    -u      : uuencode encoding: indicates a list of files which are to be
#              uuencode encoded. Multiple files may be enclosed in double
#              quotes. Usual wildcard notation can be used.
#  file_list : The list of files to send as attachments with no-encoding
#              (same as -n option, but the file list does not need to be
#              enclosed in quotes if more than one file specified).
#              Usual wildcard notation can be used.

# The program will also prompt for text to be supplied on standard input
# as the main text of the message.

# eg.
#      1) mail_files Dave.Wotton -b file9.gif t*.htm < /dev/null
#
#         email file9.gif as a base64 encoded attachment and the t*.htm
#         files unencoded.
#
#      2) mail_files Dave.Wotton -s "my test" -b "file1.gif file2.gif" \
#                    < /dev/null
#
#         email file1.gif and file2.gif as base64 encoded attachments.

# The script makes use of perl's MIME package to perform the base-64
# encoding/decoding.

# Note that files destined for Windows environments should have a name of
# the form aaaa.bbb where aaaa is up to 8 characters long, and bbb is a
# 3 character sufix. The suffix determines which program is used to
# display/process the data at the remote end.

# Simple text files can be emailed unencoded. Binary files, or text files
# with long lines ( ie > 1000 chars ) should use the  base64 or uuencode
# encoding procedures. Base64 is preferred because it is more universally
# supported. In particular, most PC mail-clients can automatically decode
# base64 encoded attachments. Note that simple text files with short lines
# which are destined for PC environments should not be base64 encoded.
# This is because PCs use a different line-break character to Unix.
# If the text is base64 encoded, the line-breaks are not converted
# automatically and so the data arrives at the remote end without
# line-breaks.

# set up a 'usage' routine
# ------------------------

usage()
{
   [ "$1" ] && ( echo $* ; echo "" )

   cat <<!
   Usage:   mail_files [-t] mailid [ -c mailid ] [ -s subject ] [ -f
mailid ]
            [-n file_list] [-u file_list] [-b file_list] file_list
!
   exit 4

}

# Initialise main variables ...
# -------------------------

FROM=$LOGNAME
SUBJ=${SUBJ:-"Not specified"}

TO="" ; CC="" ; SUBJ="" ; NOENC="" ; BASE64="" ; UUE=""

# First parse the command line options. Using getopts means the parameters
# can be supplied in any order. But first we handle the first parameter,
# which may be a recipient, without a -t flag...

case "$1" in
    -* ) : ;;                   # ignore it, let getopts handle flags
     * ) TO=$1 ; shift ;;
esac

while getopts f:s:t:c:n:b:u: OPT
do
      case $OPT in
          "f" ) FROM=$OPTARG ;;
          "t" ) TO="$TO,$OPTARG" ;;
          "c" ) CC="$CC,$OPTARG" ;;
          "s" ) SUBJ=$OPTARG ;;
          "n" ) NOENC="$NOENC $OPTARG" ;;
          "b" ) BASE64="$BASE64 $OPTARG" ;;
          "u" ) UUE="$UUE $OPTARG" ;;
           *  ) usage ;;
      esac
done

shift `expr $OPTIND - 1`

if [ "$TO" = "" ]
then
     usage "An addressee must be specified"
fi

# All remaining parameters are files not requiring encoding ...
# ---------------------------------------------------------

# Build up $FILES as the list of non-encoded files. Use sed to remove
# any leading space from the variable.

FILES=`echo $NOENC $*|sed 's/^ //'`

if [ "$BASE64" = "" -a "$FILES" = "" -a "$UUE" = "" ]
then
     usage "At least one file must be specified"
fi

# Remove leading commas from TO, CC  ...
# ---------------------------------

TO=`echo $TO | sed 's/^,//'`
CC=`echo $CC | sed 's/^,//'`

# Validate that the files exist ...
# -----------------------------

for F in $FILES $BASE64 $UUE
do
    if [ ! -r $F ]
    then
       echo "Error: File $F does not exist / is not readable."
       echo "Exiting. ( Mail not sent )."
       exit
    fi
done

tty -s && echo "Enter text of main message ( finish with CTRL-D ) ..."

# Now do the work ...
# ---------------

# The generated mail message is output onto standard out, which is then
# piped in to sendmail.

(
cat <<!
From: $FROM
Subject: $SUBJ
To: $TO
!

[ "$CC" ] && echo "Cc: $CC"

cat <<!
Mime-Version: 1.0
Content-Type: multipart/mixed; boundary="DMW.Boundary.605592468"

This is a Mime message, which your mail program may not understand. Parts
of the message will appear as text. If the remainder appears as random
characters in the message body, instead of as attachments, then you'll
have to extract these parts and decode them manually.

--DMW.Boundary.605592468
Content-Type: text/plain; name="message.txt"; charset=US-ASCII
Content-Disposition: inline; filename="message.txt"
Content-Transfer-Encoding: 7bit

!

# Read the standard input as the main text of the message ...
# -------------------------------------------------------

cat -

# Now process the non-encrypted attachments ...
# -----------------------------------------

if [ "$FILES" ]
then
     for F in $FILES
     do

        BASE=`basename $F`
...

read more »

 
 
 

mail an attachment + body

Post by Rich » Sat, 28 Jun 2003 22:31:20




>>How can I mail an attachment file + a few lines in body.  I have to
>>use uuencode.

>>I tried following but does not work.



> Isn't this a FAQ? If not, why not?

> At any rate, take a look at the last bazillion times it's been asked:

> http://groups.google.com/groups?as_q=mail+attachment&num=100&as_scori...

You know, I took a look at the first page and found very little that's
useful.

I agree with your point, a FAQ seems to be needed.

Rich

 
 
 

mail an attachment + body

Post by Martin Schoe » Sun, 29 Jun 2003 01:05:47




>>How can I mail an attachment file + a few lines in body.  I have to
>>use uuencode.

>>I tried following but does not work.



> Isn't this a FAQ? If not, why not?

> At any rate, take a look at the last bazillion times it's been asked:

> http://groups.google.com/groups?as_q=mail+attachment&num=100&as_scori...

Those huge scripts are brilliant and very handy, of course.

However, for me this little one works fine (error handling for home work):

+++
#!/bin/sh

RECEIVER=$1
SUBJECT=$2
FULL_PATH_TO_FILE=$3

/usr/bin/mail -s "$SUBJECT" $RECEIVER<<EOI
Hi!

Here's your file...

~<!uuencode
FULL_PATH_TO_FILE
~.
EOI
+++

Martin

 
 
 

mail an attachment + body

Post by Martin Schoe » Sun, 29 Jun 2003 01:07:44


arrgl...

Quote:>> http://groups.google.com/groups?as_q=mail+attachment&num=100&as_scori...

> However, for me this little one works fine (error handling for home work):

> +++
> #!/bin/sh

> RECEIVER=$1
> SUBJECT=$2
> FULL_PATH_TO_FILE=$3

> /usr/bin/mail -s "$SUBJECT" $RECEIVER<<EOI
> Hi!

> Here's your file...

> ~<!uuencode
> FULL_PATH_TO_FILE

$FULL_PATH_TO_FILE

Quote:> ~.
> EOI
> +++

Martin
 
 
 

mail an attachment + body

Post by Robert Bono » Sun, 29 Jun 2003 08:50:56




>On 26 Jun 2003 13:22:18 -0700, J M

>> How can I mail an attachment file + a few lines in body.  I have to
>> use uuencode.

>> I tried following but does not work.



>> TIA!

>(1) Dos2unix is for text files, not for gif or other binary files.
>(2) You can't direct input from a pipe and a file to the same command
>at the same time.
>You could try this:



better:
  uuencode file.gif <file.gif | cat bodyfile.txt - | \

- Show quoted text -

Quote:

>but it would be easier to use a mail client such as pine or mutt (or almost
>anything other than mail or mailx).

>--
>It is often the case that the man who can't tell a lie thinks he is the best
>judge of one.
>            -- Mark Twain, "Pudd'nhead Wilson's Calendar"

 
 
 

mail an attachment + body

Post by Heiner Steve » Wed, 02 Jul 2003 06:05:35




>>How can I mail an attachment file + a few lines in body.  I have to
>>use uuencode.

>>I tried following but does not work.



> Isn't this a FAQ? If not, why not?

> At any rate, take a look at the last bazillion times it's been asked:

> http://groups.google.com/groups?as_q=mail+attachment&num=100&as_scori...

I tried to contact Ted Timar (the maintainer of the UNIX FAQ) several
times, but got no response.

Somebody mentioned, that Ted would hand the FAQ over to somebody
else, but nothing seems to have happened.

Maybe somebody else feels he should maintain a FAQ particular
for comp.unix.shell? I'm sure many people would like to contribute.

Heiner