"Fossies" - the Fresh Open Source Software Archive

Member "msmtp-1.8.5/scripts/find_alias/find_alias_for_msmtp.sh" (2 May 2019, 2346 Bytes) of package /linux/privat/msmtp-1.8.5.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Bash source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "find_alias_for_msmtp.sh": 1.8.4_vs_1.8.5.

    1 #!/bin/bash
    2 # Jim Lofft 06/19/2009
    3 # find_alias_for_msmtp.sh
    4 # popper email server was replaced by exchange. I replaced sendmail with this script
    5 # which scans /etc/alias file for a valid domain email address and calls msmtp
    6 # (https://marlam.de/msmtp)
    7 #
    8 # changed a little by Ovidiu Constantin <ovidiu@mybox.ro> && http://blog.mybox.ro/
    9 #
   10 
   11 DEFAULTEMAIL="you@domain.com"
   12 
   13 v_recipient=0
   14 v_msg=$(cat)       # email message contents
   15 MSMTP=$(which msmtp || echo "/usr/local/bin/msmtp")
   16 
   17 #
   18 if [  "$1" = '-i'  ] ; then
   19    v_recipient=$2 # mailx calls sendmail with -i as the 1st param and the recipient as the second param
   20 elif [ "$1" = '-t' ] ; then  # most other programs call sendmail with a -t option
   21         v_to=$(echo "$v_msg" | grep -m 1 'To: ')
   22         v_recipient=${v_to:4:50}
   23 else
   24    v_recipient=$1 # no parameter, sendmail was called with the recipient as parameter
   25 fi
   26 #
   27 
   28 if [ $v_recipient != 0 ] ; then
   29 
   30 # trim spaces from recipient email address
   31  v_recipient="${v_recipient// /}"
   32 
   33 # see if this email is to a @ domain.com
   34  v_domain=$(expr index "{$v_recipient}" @)
   35 
   36 # if this email isn't to a domain, then it's a local email, so
   37 # look up the recipient in the aliases file
   38 
   39         if [ "$v_domain" = 0 ]; then
   40 
   41                 v_find_alias=$(grep -- "$v_recipient": /etc/aliases | awk '{print $2}')  #grep alias file
   42                 v_alias_domain=$(expr index "{$v_find_alias}" @) # grep for an @domain address
   43 
   44                         if [ "$v_alias_domain" = 0 ]; then      # we didn't find an @, grep alias again
   45 
   46                                 v_next_alias=$(grep -- "$v_find_alias": /etc/aliases | awk '{print $2}')
   47                                 v_alias_domain=$(expr index "{$v_next_alias}" @)
   48 
   49                                  if [ "$v_alias_domain" = 0 ]; then # email someone important if no @ alias is found
   50                                     v_recipient=$DEFAULTEMAIL
   51                                  else
   52                                     v_recipient=$v_next_alias
   53                                  fi
   54                         else
   55                            v_recipient=$v_find_alias
   56                         fi
   57         fi
   58   # Send msmtp email
   59   echo "$v_msg" | $MSMTP -i $v_recipient
   60 else
   61   # we're not sure who this email is for, just send it to msmtp and see what happens..
   62   echo "$v_msg" | $MSMTP $1 $2 $3 $4 $5
   63 fi