mybin

my ~/bin
git clone https://a3nm.net/git/mybin/
Log | Files | Refs | README

msmtpQ (7636B)


      1 #!/bin/bash
      2 
      3 #--------------------------------------------------------------
      4 #
      5 #  msmtpQ : queue funtions to use the msmtp queue,
      6 #           as it was defined by Martin Lambers
      7 #  Copyright (C) 2008 Chris Gianniotis
      8 #
      9 #  This program is free software: you can redistribute it and/or modify
     10 #  it under the terms of the GNU General Public License as published by
     11 #  the Free Software Foundation, either version 3 of the License, or (at
     12 #  your option) any later version.
     13 #
     14 #--------------------------------------------------------------
     15 
     16 #--------------------------------------------------------------
     17 # 
     18 # This version has tiny modifications by Marianne Promberger to the original 
     19 # found here: 
     20 # http://www.mail-archive.com/msmtp-users@lists.sourceforge.net/msg00005.html 
     21 # 
     22 # -- cleaned up accidental linebreaks in comments
     23 # -- changed shell from /bin/sh to /bin/bash
     24 # -- changed logfile location
     25 # 
     26 # marianne@promberger.info
     27 #--------------------------------------------------------------
     28 
     29 #--------------------------------------------------------------
     30 # the msmtp queue contains unique filenames of the following form :
     31 #   two for each mail in the queue
     32 #
     33 # create new unique filenames of the form :
     34 #   MLF: ccyy-mm-dd-hh.mm.ss[-x].mail   -- mail file
     35 #   MSF: ccyy-mm-dd-hh.mm.ss[-x].msmtp  -- msmtp command line file
     36 # where x is a consecutive number only appended for uniqueness
     37 #   if you send more than one mail per second
     38 #--------------------------------------------------------------
     39 
     40 
     41 # msmtpQ is meant to be used directly by an email client - in 'sendmail' mode
     42 # there is a separate log file for all events & operations on the msmtp
     43 #   queue that is defined below
     44 
     45 
     46 ## !! please define the following two vars before using the msmtpq & msmtpQ
     47 ## !! routines
     48 
     49 # set the queue var to the location of the msmtp queue directory
     50 #   if the queue dir doesn't yet exist, better to create it (0700)
     51 #     before using this routine (it will only complain ...)
     52 #
     53 Q=~/msmtp.queue                     # the queue - modify this to reflect where
     54 				     # you'd like it to be
     55 
     56 # set the queue log var to the location of the msmtp queue log file
     57 #   where it is or where you'd like it to be
     58 #     ( note that the LOG setting could be the same as the )
     59 #     ( 'logfile' setting in .msmtprc - but there may be   )
     60 #     ( some advantage in keeping the two logs separate    )
     61 #   if you don't want the log at all please unset (comment out) this var
     62 #
     63 LOG=~/logs/msmtp-queue            # the log   - modify to taste ...
     64 
     65 umask 077                            # set secure permissions on created
     66 				     # directories and files
     67 
     68 usage() {
     69   echo
     70   echo '  usage : msmtpQ'
     71   echo '          msmtpQ -h   this helpful blurt'
     72   echo
     73   echo '  any/all args on the msmtpQ command'
     74   echo '    line are passed through to msmtp'
     75   echo '    (not to mention the mail body text'
     76   echo '     passed via standard in ...)'
     77   echo
     78   exit 0
     79 }
     80 
     81 # log a message, possibly an error
     82 # usage : log_msg [ -e ] msg [ msg ] ...
     83 #  opts : -e <exit code>  an error ; log msg & terminate w/prejudice
     84 #
     85 log_msg() {
     86   local ERR ARG RC PFX
     87 
     88   if [ "$1" == '-e' ] ; then
     89     ERR='t'                          # set error flag
     90     RC="$2"                          # take error exit code
     91     shift 2                          # shift opt & its arg off
     92   fi
     93 
     94   if [ -n "$LOG" ] ; then            # logging not suppressed
     95     PFX="$(/bin/date +'%Y %b %d %H:%M:%S')"  # time stamp prefix - "2008 Mar 13
     96 					     # 03:59:45 "
     97     for ARG ; do                     # each msg line out
     98       echo "$PFX : $ARG" >> "$LOG"   # send it to log
     99     done
    100   fi
    101 
    102   if [ -n "$ERR" ] ; then            # an error ; leave w/error return
    103     if [ "$RC" != 0 ] ; then         # exit code != 0 => display the exit code
    104       [ -n "$LOG" ] && \
    105         echo "    exit code = $RC" >> "$LOG" # logging ok ; send exit code to log
    106       exit $RC                       # exit w/exit code
    107     else                             # exit code == 0 => don't display an exit code
    108       exit 1
    109     fi
    110   fi
    111 }
    112 
    113 # verify that the msmtp queue is present
    114   # the first version can be used if you'd like to create the queue dir
    115   # if it's not found ; I'd rather just be warned if it's not there
    116 check_queue() {                      # make certain queue dir is present
    117   #if [ ! -d "$Q" ] ; then            # queue dir not present ; create it
    118         #  /bin/mkdir -p "$Q" || log_msg -e "could not create msmtp queue dir [ $Q ]"
    119         #  log_msg "created msmtp queue dir [ $Q ]"
    120   #fi
    121   [ -d "$Q" ] || \
    122     log_msg -e 0 "can't find msmtp queue [ $Q ]"
    123 }                                    # queue dir not present - complain
    124 
    125 # enqueue a mail
    126 enqueue_mail() { # <-- all mail args ; mail text via TMP
    127   local BASE="${Q}/$(/bin/date +%Y-%m-%d-%H.%M.%S)"  # make base filename for queue
    128   local -i INC RC                    # increment counter for basename collision
    129 
    130   if [ -f "$BASE.*" ] ; then         # ensure base filename is unique
    131     INC=1                            # initial increment
    132           while [ -f "${BASE}-${INC}.*" ] ; do   # base filename exists
    133       (( ++INC ))                    # filename exists ; bump increment
    134           done
    135           BASE="${BASE}-${INC}"            # unique ; set base filename
    136   fi
    137 
    138   # write msmtp command line to .msmtp queue file
    139   echo "$@" > "${BASE}.msmtp"
    140   RC=$?                              # take exit code
    141   [ $RC != 0 ] && \
    142     log_msg -e $RC "queueing - writing msmtp cmd line [ $* ] to [ ${BASE}.msmtp ] : failed"
    143 
    144   # mv temp fil to be mail body fil
    145   /bin/mv "$TMP" "${BASE}.mail"
    146   RC=$?                              # take exit code
    147   if [ $RC == 0 ] ; then             # queueing was successful
    148     log_msg "enqueued mail as : $BASE [ $* ] : successful"
    149   else                               # write failed
    150     log_msg -e $RC "queueing - creating mail body file [ ${BASE}.mail ] : failed"
    151   fi
    152 }
    153 
    154 # send a mail (if possible, otherwise enqueue it)
    155 # if send is successful, msmtp itself will log it (if enabled in .msmtprc)
    156 send_mail() {    # <-- all mail args ; mail text via TMP
    157   local -i RC                        # msmtp return code
    158 
    159   #if /bin/ping -qnc 1 -w 2 64.233.183.147 &> /dev/null ; then  # ping ip addr of
    160   #      						       # www.google.com
    161   #  /bin/cat "$TMP" | /usr/bin/msmtp "$@" > /dev/null  # send mail using temp file
    162   #  RC=$?                            # take exit code
    163   #  if [ $RC == 0 ] ; then           # send was successful
    164   #    log_msg "mail sent for [ $* ] : successful"
    165   #    exit 0                         # msmtp will log it
    166   #  else                             # send not ok - log msg
    167   #    log_msg "mail sent for [ $* ] : unsuccessful ; msmtp exit code was $RC"
    168   #    enqueue_mail "$@"              # enqueue the mail
    169   #  fi
    170   #else                               # not connected to net ; log msg
    171   log_msg "mail sent for [ $* ] : enqueued"
    172   enqueue_mail "$@"                # enqueue the mail
    173   log_msg "running msmtpq -r in background"
    174   nohup ~/bin/msmtpq -r &
    175   log_msg "done!"
    176   #fi
    177 }
    178 
    179 cleanup() {                          # remove temporary message file
    180   [ -e "$TMP" ] && /bin/rm -f "$TMP"
    181 }
    182 
    183 #
    184 ## -- entry point
    185 #
    186 
    187 [ -z "$1" ] && log_msg 'msmtpQ was invoked with no cmd line args ; why was that ?'
    188 
    189 check_queue                          # check that queue directory is present ...
    190 
    191 trap cleanup 0 1 2 3 6 9 14 15
    192 TMP="$(/bin/mktemp -qt msmtpQ.tmp.XXXXXXXXXX)" || \
    193       log_msg -e 'could not create temp file'  # make temp file
    194 /bin/cat > "$TMP"                    # take piped mail into temp fil
    195 
    196 send_mail "$@"                       # send the mail if possible, queue it if not
    197