The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers > Answers to Frequently Asked Questions > How do I send email?
Google UNIX.COM


How do I send email? How do I send an mail attachment from the command line?

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Sending multiple attachments deo2k8 Shell Programming and Scripting 1 01-07-2008 09:12 PM
Problem with multiple excel attachments using mailx ramanam2004 UNIX for Advanced & Expert Users 2 05-03-2006 10:07 PM
Help ! How to get elm to send multiple attachments - in batch mode anarvan UNIX for Dummies Questions & Answers 1 09-14-2005 05:41 AM
Email Attachments fabbas UNIX for Dummies Questions & Answers 1 10-18-2004 07:36 AM
multiple attachments edog FAQ Submission Queue 5 01-06-2003 10:09 AM

 
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-20-2002
Registered User
 

Join Date: Sep 2001
Location: Green Bay, WI
Posts: 66
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
multiple attachments

how can you send multiple attachments in 1 email, usually I just use uuencode to send 1 attachment.

thanks
Forum Sponsor
  #2 (permalink)  
Old 12-20-2002
Kelam_Magnus's Avatar
Unix does a body good.
 

Join Date: Aug 2001
Location: DFW McKinney, TX,
Posts: 1,069
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Edog,

Perderabo, I believe has a script that is will allow attachments. The search tool seems to be broken when specifying a user name.

Here is the link. I had it saved in my favorites.

MAIL question for HP-Unix O/S

This will work for 1 attachment. but you can probably zip or use some other way the files you wish to mail.

__________________
My brain is your brain
  #3 (permalink)  
Old 12-20-2002
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,252
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
mimesender -- part one

Kelam_Magnus, I have a small confession to make... I have been holding out on you. That mimetool script has a big brother named mimesender. I've been a little reluctant to release it because it can override the "from" address. But other ways to override the "from" address have been posted here. And it is (nearly) Christmas.

So, here is mimesender. It can handle hundreds of attachments and they can be either ascii or binary. I see that I must post this script in two parts. Here is part one.
Code:
#! /usr/bin/ksh
#!/usr/local/bin/bash


#
#  This script takes ascii files, assembles them into a mime compliant
#  message and mails them.  You can send ascii attachments.  A recipient
#  with a mime aware user agent will be able to use the agent's built-in
#  tools for attachments.  On the other hand, a recipient with an older
#  non-mime user agent will see a message that is completely in ascii and
#  will be able to easily chop the pieces up manually.
#
#
#  Options 
#
#  -t to address (required)
#  -T to name    (optional)
#  -f from addr  (optional)
#  -F from name  (optional)
#  -b body       (optional)
#  -s subject    (optional)
#  -A attachment (optional)
#  -B attachment (optional)
#  -X attachment (optional)
#  -D description(optional)
#
#  mimesender -t "Joe Blow" -T jblow@abc.com  -b body.txt  -A attach.txt
#
#  Here body.txt and attach.txt are files.  The name of the body file
#  isn't too important.  The name of the attachment file is important 
#  since it will be sent as well as the contents.  A Microsoft OS uses
#  the name to figure out what to do.  It knows what a .txt file is but
#  it will get mixed up with a .junk file. You can use the -D option to 
#  send one a different name like this:
#  mimesender -t jblow@abc.com -T "Joe Blow" -b body -A junk -D junk.txt
#
#  You can use several -t -T pairs to indicate multiple recipients.  The number
#  of pairs is limits by your shell's limit on array elements.
#
#  Here the attachment will come from a local file called "junk" but will
#  be sent with a name of "junk.txt".
#
#  Either the body or an attachment can be - in which case it will be read
#  from the script's standard in.  If you do this with an attachment, a -D
#  would be especially useful.  Only one item, either the body or an 
#  attachment can come from standard in.
#
#  Multiple attachments can be sent by using several -A options.  A -D 
#  always refers to the last -A ( or -B or -X, see below) that preceded 
#  it.  Each attachment specified by a -A can be followed by a -D to 
#  rename it at the recieving end.  The number of  -A and -D pairs is 
#  limited only by your shells limit on an array.  ksh will
#  always allow at least 512 elements.  bash allows more than that. 
#
#  -B is like -A, but it is for binary attachments.  They will be uuencoded
#  before being sent.
#
#  -X is like a hybrid of -A and -B.  The attachment is ascii, but it will 
#  be uuencoded anyway.  But before it is uuencoded, it will have the 
#  newline characters replaced with LF/CR pairs.
#
#  By default, this script will use the unix id which is running it as the
#  "From:" address.  And it will poke around in the GCOS field of /etc/passwd
#  to get the name of the user.  You can override the the name with the -F 
#  option.  This is fairly innocuous.  Many version of unix allow you to run
#  a command to update your GCOS field, and you can use that to change your
#  name, send some mail, and change it back.  The -F just saves you some time.
#  You can use -F to send mail under your screen name, or whatever.
#
#  I have also included a -f to override the address on the "From:" line.  
#  The -f may or may not work, it depends on your MTA's security policy.  If 
#  it works at all, you must use a local address.  If you use a -f, the script
#  will also output a "Sender:" line with the real id running the script.  With
#  or without that "Sender:" line, the MTA's that I have tested ensure that a
#  mail expert on the receiving system can determine the real sender.  Use -f
#  with caution, please.  You must ensure that the address you specify can
#  receive replys.  
#
#  -d is a secret debug option.

#############################################################
#                                                           #
#    Section 0 ---  Customization                           #
#                                                           #
#############################################################
#
#  You may need to customize a few things:
#  1  You must set the first line above to be the path to 
#     a shell.  mimesender will work with either ksh or bash.
#     The variable "this_shell" should then automatically set
#     itself to the shell you used based on the existence or
#     non-existence on the bash variable BASH_VERSION.  The 
#     rest of the script will check this_shell as needed.  Note
#     that a recent version of bash will be needed as it must
#     support arrays.  If you have both ksh and bash available,
#     go with ksh for the best performance.
#
#  2  Your PATH must contain the commands cat and id and sed
#     For binary attachment, your PATH must also include 
#     uuencode
#
#  3  MTA must be set to the path to your Mail Transfer Agent
#
#  4  BOUNDARY must be a string that is not used in the files
#     you want to mail.  Actually it must not appear on a line
#     by itself prepended with two hyphens.  This script can 
#     mail itself.
#
#  5  pwentry must be set to the user's entry from /etc/passwd
#     If you are using nis or nis+, or something, you must use
#     niscat or whatever.  pwentry will be treated as a bunch of
#     colon delimited fields.  Field one will be used for the 
#     sender's email address.  Field four will be treated as bunch
#     of comma delimited sub-fields.  The first one will be used as
#     the sender's name.  This is the standard way that unix mailers
#     have always worked.
#     

[[ -n $BASH_VERSION ]] && this_shell=bash || this_shell=ksh
PATH=/usr/bin
export PATH
MTA=/usr/lib/sendmail
BOUNDARY='=== This is the boundary between parts of the message. ===--'
pwentry=$(grep "^$(id -un):" /etc/passwd)

################################################################
####  In theory you should not need to touch anything else  ####
################################################################


#############################################################
#                                                           #
#    Section 0 ---  Initialization                          #
#                                                           #
#############################################################

#
#  For shell independence, mimesender does its outputting via the functions:
#  scribe_out and scribe_err.  Here we define these functions for the
#  current shell.
#

if [[ $this_shell = ksh ]] ; then
        scribe_out() { print - ${1+"$@"} ; }
        scribe_err() { print -u2 - error: ${1+"$@"} ; }
else
        scribe_out() { echo -E ${1+"$@"} ; }
        scribe_err() { echo -E error: ${1+"$@"} >&2 ; }
fi



#
#  Debugging features

DEBUG=0
DEBUGFILE=mimesender$$

if [[ $this_shell = ksh ]] ; then
        scribe_debug() { ((DEBUG)) && print -u2 - DEBUG: ${1+"$@"} ; }
else
        scribe_debug() { ((DEBUG)) && echo -E DEBUG: ${1+"$@"} >&2 ; }
fi

debug_mta() 
{
	exec 4>&1 >$DEBUGFILE
	scribe_out
	scribe_out
	while (($#)) ; do scribe_out mta arg = "$1" ; shift ; done
	scribe_out
	scribe_out start of mail
	while read l ; do scribe_out "$l" ; done
	scribe_out end of mail
	exec 1>&4 4>&-
}

#
#  Initialize variables

VERSION=0.0
IFS=""

if [[ $this_shell = ksh ]] ; then
	set -A TADDR 
	set -A TNAME 
	set -A ATTACH 
	set -A FORMAT 
	set -A DESC
else
	declare -a TADDR TNAME ATTACH FORMAT DESC
fi

((naddr=0))
((nattach=0))
((stdin_inuse=0))
((error=0))
((npart=0))

preamble1="\
        This message is in MIME format.  But if you can see this,
        you aren't using a MIME aware mail program.  You shouldn't 
        have too many problems because this message is entirely in
        ASCII and is designed to be somewhat readable with old 
        mail software."
preamble2="
        This message is in MIME format.  But if you can see this,
        you aren't using a MIME aware mail program.  Some parts of
        of this message have been uuencoded for transport.  On a Unix
        system, the program uudecode should be used to restore the file."
PREAMBLE="$preamble1"

AType="text/plain"
BType="application/octet-stream"

if [[ $this_shell = ksh ]] ; then
	CR="$(print \\r)"
else
	CR="$(echo -e \\r)"
fi


#############################################################
#                                                           #
#    Section 1 ---  Parameter Parsing and Checking          #
#                                                           #
#############################################################

#
#  Parse the command arguments

while getopts ':df:F:t:T:b:A:B:D:X:s:' opt ; do
	case $opt in
	d)	DEBUG=1
		MTA=debug_mta
		scribe_debug Debug Mode is on...no mail will be sent
		;;
	f)	FADDR=$OPTARG
		;;
	F)	FNAME=$OPTARG
		;;
	t)
		TADDR[naddr]=$OPTARG
		TNAME[naddr]=""
		((naddr=naddr+1))
		;;
	T)
		if ((naddr)) ; then
			TNAME[naddr-1]=$OPTARG
		else
			scribe_err -T must follow -t
			((error=error+1))
		fi
		;;
	b)
		BODY=$OPTARG
		((npart=npart+1))
		;;
	A|B|X)
		[[ $opt = A ]] || PREAMBLE="$preamble2"
		ATTACH[nattach]=$OPTARG
		if [[ $OPTARG = - ]] ; then
			DESC[nattach]=stdin
		else
			DESC[nattach]=$OPTARG
		fi
		FORMAT[nattach]=$opt
		((nattach=nattach+1))
		((npart=npart+1))
		;;
	D)
  #4 (permalink)  
Old 12-20-2002
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,252
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
mimesender -- part two

Code:
		if ((nattach)) ; then
			DESC[nattach-1]=$OPTARG
		else
			scribe_err -D $optarg must follow a -A argument
			((error=error+1))
		fi
		;;
	s)
		SUBJECT=$OPTARG
		;;
	\?)
		scribe_err what is -${OPTARG}?
		((error=error+1))
		;;
	:)
		scribe_err $OPTARG need an argument
		((error=error+1))
		;;
	esac
done

#
#  Parameter error checking: an address is required

if ((!naddr)) ; then
	scribe_err "-t ADDRESS is required"
	((error=error+1))
fi


#
#  Parameter error checking: If BODY  was specified, it
#  must exist and be readable

if [[ ! -z $BODY ]] ; then
	if [[ $BODY != - ]] ; then
		if [[ ! -f $BODY ||  ! -r $BODY ]] ; then
			scribe_err  "-b $BODY is not a readable file"
			((error=error+1))
		fi
	else
		stdin_inuse=1
	fi
fi

#
#  Parameter error checking: If ATTACH was specified, it
#  must exist and be readable.  
#  Also we can use stdin only once

i=0
while ((i"
	else
		scribe_out "From: $myname <${FADDR}>"
	fi
	scribe_out  "Sender: $myname <${myaddr}>"
elif [[ -n $FNAME ]] ; then
	scribe_out "From: $FNAME <${myaddr}>"
else
	scribe_out  "From: $myname <${myaddr}>"
fi

#
#  Next we will do the "To:" Addresses

((i=0))
Line=""
while ((i1)) ; then
	scribe_out  'Content-Type: multipart/mixed; '
	scribe_out  '    boundary='\"$BOUNDARY\"
	scribe_out 
	scribe_out  "$PREAMBLE"
	scribe_out 
fi

#
#  Output message body if we have one.

if [[ -n $BODY ]] ; then
	if ((npart>1)) ; then
		scribe_out  "--${BOUNDARY}"
	fi
	scribe_out  'Content-Type: text/plain; charset=US-ASCII'
	scribe_out 
	if [[ $BODY = - ]] ; then
		cat
	else
		cat $BODY
	fi
else
	scribe_out
fi

#############################################################
#                                                           #
#    Section 3 ---  Attachments                             #
#                                                           #
#############################################################
#

#
#  Loop on our attachments... 

i=0
while ((i1)) ; then
		scribe_out  "--${BOUNDARY}"
	fi

#
#  Build attachment header

	if [[ ${FORMAT[i]} = B ]] ; then
		TYPE=$BType
	else
		TYPE=$AType
	fi
	scribe_out  'Content-Type: '${TYPE}'; name="'${ATTACH[i]}\"

	if [[ ${FORMAT[i]} != A ]] ; then
		scribe_out "Content-Transfer-Encoding: x-uue"
	fi

	scribe_out  'Content-Disposition: attachment; filename="'${DESC[i]}\"
	scribe_out 

#
#  Build a pipeline to process the attachment

	if [[ ${ATTACH[i]} = - ]] ; then
		PIPELINE="cat"
	else
		PIPELINE="cat ${ATTACH[i]}"
	fi
	[[ ${FORMAT[i]} = X ]] && PIPELINE=${PIPELINE}"| sed \"s/$/\${CR}/\""
	[[ ${FORMAT[i]} != A ]] && PIPELINE=${PIPELINE}"|uuencode ${DESC[i]}"
	scribe_debug PIPELINE = "$PIPELINE"
	eval $PIPELINE

	scribe_out 
	((i=i+1))
done

#############################################################
#                                                           #
#    Section 4 ---  Send Mail                               #
#                                                           #
#############################################################
#

#
#  If in multipart mode, do final boundary

if ((npart>1)) ; then
	scribe_out  "--${BOUNDARY}--"
fi

#
#  Here is where we send the mail

} | eval $MTA ${TADDR[@]}

if ((DEBUG)) ; then
	while read l ; do scribe_debug "$l" ; done < $DEBUGFILE
	rm $DEBUGFILE
fi

exit 0
  #5 (permalink)  
Old 12-20-2002
Neo's Avatar
Neo Neo is offline
Administrator
 

Join Date: Sep 2000
Location: Asia Pacific
Posts: 4,061
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
I rebuilt the search database and now the 'search by username' seems to be working fine. Neo
  #6 (permalink)  
Old 01-06-2003
Kelam_Magnus's Avatar
Unix does a body good.
 

Join Date: Aug 2001
Location: DFW McKinney, TX,
Posts: 1,069
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
holding out on us...

Quoted from Perderabo: Kelam_Magnus, I have a small confession to make... I have been holding out on you. That mimetool script has a big brother named mimesender.

Now I see the truth Perderabo!! Aha!!

You sneaky devil. Holding out on us huh?
__________________
My brain is your brain
Google UNIX.COM
 

Tags
sendmail

Thread Tools
Display Modes


The 50 most popular UNIX and Linux searches.
Google Search Cloud for The UNIX and Linux Forums
421 service not available, remote server has closed connection ^m automate ftp autosys awk trim bash eval bash for loop boot: cannot open kernel/sparcv9/unix command copy/move folder in unix curses.h cut command in unix daemon process find grep find mtime find null character in a unix file from ip can we get machine name +unix glance unix grep multiple lines grep or grep recursive how to redirect console logs in unix inaddr_any inappropriate ioctl for device lynx javascript mailx attachment mget mtime perl array length ping port remove first character from string in k shell replace space by comma , perl script scp recursive segmentation fault(coredump) sftp script snoop unix stale nfs file handle syn_sent tar exclude tar extract to folder unix unix .profile unix forum unix forums unix internals unix interview questions unix mtime unix simulator unix.com vi substitute while loop within while loop shell script


All times are GMT -7. The time now is 12:25 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101