[X-Unix] Simple commandline mail client

William H. Magill magill at mcgillsociety.org
Tue Feb 3 10:03:34 PST 2004


On 03 Feb, 2004, at 00:17, Peter Marreck wrote:

> Here's my situation- I merely want to be able to enter some SMTP  
> settings (server, login, etc.) in a config file (or as additional  
> parameters on a commandline) so I can do something like "mail  
> me at me.com 'this is the subject' 'this is the body'" so I can include  
> it in a script. I don't want to have to mess with installing or  
> configuring mail SERVERS such as Postfix etc., and I don't want  
> something that will automatically break with the next OS upgrade. I  
> also don't necessarily want to applescript mail.app. Does anyone know  
> of such a CL app?

As was mentioned, the tool is called sendmail it is intended for use in  
a script.

Here's a typical generic usage... (I use it for emailing log files.)

Postfix has a link to sendmail and follows the same usages, so the  
script below
works with both a complete sendmail installation or a postfix  
installation.
It's written in ksh, and is extremely portable.

Modify the variables...
      MAIL_ADDR="xxx at xxx.xxx"
      HOST2="xxx.xxx.xxx.xxx"
      param=xxx

The script constructs the outgoing message from a header (MAIL_TXT) and  
body
(MAIL_TMP1 and REPORT).
The body can be anything, it is simply appended (cat >>) to the header.

It leaves behind a file $REPORT which contains what it sent.
Note: lines in the original script are longer than 80 chars, some might  
fold...
This line in the script:
# ---------------------------------------------------------------------*
is 72 characters long.

------------------<cut here>------------------
#!/bin/zsh
emulate ksh
#
# ------------------------------------- define the mailer to be used
export MAILER="/usr/sbin/sendmail -t"
#                                       where to mail output reports
export MAIL_ADDR="xxx at xxx.xxx"
#                                       where it originated
export HOST2="xxx.xxx.xxx.xxx"
#                                       report name
export param=xxx
# -------------------------------------
#
export REPORT=$param
export LOGFILE=$param
# ------------------------------------- some scratch files
export TMP_DIR=/tmp

export TMP1=$TMP_DIR/mailit.tmp1.$$
export TMP2=$TMP_DIR/mailit.tmp2.$$
export TMP3=$TMP_DIR/mailit.tmp3.$$

export MAIL_TXT=$TMP_DIR/mailit.$$.0
export MAIL_TMP1=$TMP_DIR/mailit.$$.1
export MAIL_TMP2=$TMP_DIR/mailit.$$.2

export RPTDIR=/var/log

export REPORT=$RPTDIR/outgoing.$REPORT.$ISODATE
# ---------------------------------------------------------------------*
# -------------------------------------  prime the mail file
         print "To: $MAIL_ADDR"                                           
 >  $MAIL_TXT
         print "From: The-Log-Meister"                                    
 >> $MAIL_TXT
         print "Subject: xx                                      "        
 >> $MAIL_TXT
         print "-------"                                                  
 >> $MAIL_TXT
         print " "                                                        
 >> $MAIL_TXT
# ---------------------------------------------------------------------*
#               Define the basic message environment
# -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  *
print "         Periodic report: $LOGFILE.  From $HOST2"                 
                  > $MAIL_TMP1
print "         Date: `date`              $progname"                     
                 >> $MAIL_TMP1
print "  
======================================================================== 
= "     >> $MAIL_TMP1
print " "                                                                
                 >> $MAIL_TMP1

# ---------------------------------------------------------------------*
#       Prepare the outgoing message
# -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  *
if [ -f $MAIL_TMP1 ]; then
         cat $MAIL_TMP1 >> $MAIL_TXT
         cat $REPORT    >> $MAIL_TXT
fi
print " End of periodic report: $LOGFILE."                               
                 >> $MAIL_TXT
print " "                                                                
                 >> $MAIL_TXT

#                       use ED to edit the subject into the message
#                       [replace the line containing "Subject:"]
#
ed - $MAIL_TXT >/dev/null <<%
/Subject:/
.c
Subject: Cron: Periodic report: $LOGFILE.  From: $HOST2
.
w
q
%
set -vx
# -------------------------------------
#                       done adding subject, save report then send it
$CP  $MAIL_TXT $REPORT
#                       done adding subject, send it
$MAILER < $MAIL_TXT
#                       erase the input files
ls -als $REPORT

ls -als  $TMP1 $TMP2 $TMP3 $MAIL_TMP1 $MAIL_TMP2 $MAIL_TXT

$RM -f   $TMP1 $TMP2 $TMP3 $MAIL_TMP1 $MAIL_TMP2 $MAIL_TXT
# ---------------------------------------------------------------------*
#
------------------<cut here>------------------

T.T.F.N.
William H. Magill
# Beige G3 - Rev A motherboard - 768 Meg
# Flat-panel iMac (2.1) 800MHz - Super Drive - 768 Meg
# PWS433a [Alpha 21164 Rev 7.2 (EV56)- 64 Meg]- Tru64 5.1a
# XP1000 - [Alpha EV6]
magill at mcgillsociety.org
magill at acm.org
magill at mac.com



More information about the X-Unix mailing list