Sponsored Content
Top Forums Shell Programming and Scripting Auto emails from within the program Post 303021185 by nobbyall on Monday 6th of August 2018 04:00:01 AM
Old 08-06-2018
Runs through all the motions but will not send anything.

Sometimes it picks up the selected email but others it select default. Auto.

My Deputy code is below.

Code:
# #!/bin/bash

# This file must be amended to suit the RosterDiary deputy's email account.
# Set up Thunderbird for RosterDiary's email account.
# Start Thunderbird
# Write a message to send from this account.
# Make sure you are OFFLINE.

# Sending the message whilst OFFLINE will send it to the Thunderbird Admin "outbox" folder.
# Retrieve this so you can view it,  ascertain the details below.
# Key them here, below, SAVE THE FILE!
#
#
#

SendFromAccount="hotmail.com"
XIdentityKey="id2"      # X-Identity-Key:
XAccountKey="account3"  # X-Account-Key:
SendFromField="Paul Overington <user.name@hotmail.com>" # From:
ReplyTo="user.name@hotmail.com"
FCCField="imap://user.namer%40hotmail.com@imap-mail.outlook.com/Sent"    
UserAgent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:52.0) Gecko/20100101 Thunderbird/52.9.1"
OutboxFileName="imap://user.name%40hotmail.com@imap-mail.outlook.com/Outbox" # "Outbox" for results

------ Post updated at 03:00 AM ------

This is the ./conv09 for the sending of emails.

Code:
#!/bin/bash

# Create emails for individual users.
# The attachement files must already be located in each user's directory...
# WARNING - User files with _ (Underscore) characters in the name will mess up...

if [ ! -r "./conv.conf" ]; then
  echo "$0: ./conv.conf is missing"
  exit 1
fi
. "./conv.conf"

if [ ! -r "$0.conf" ]; then
  echo "$0: $0.conf is missing"
  exit 1
fi
. "$0.conf"

if [ -r "$0.conf.$hostname" ]; then
  . "$0.conf.$hostname"

  for ReqdVar in SendFromAccount XIdentityKey XAccountKey SendFromField ReplyTo FCCField UserAgent OutboxFileName
  do
    if [ -z "`eval echo '$'$ReqdVar`" ]; then
      ERROR "$ReqdVar= is not set in \"$0.conf.$hostname\""
      exit
    fi
  done
else
  ERROR "$0.conf.$hostname was not found"
cat << 'UntilHere'
The file is expected to contain the following variables:

  SendFromAccount=
  XIdentityKey=  # X-Identity-Key:
  XAccountKey=   # X-Account-Key:
  SendFromField= # From:
  ReplyTo=
  FCCField=
  UserAgent=
  OutboxFileName=

These can be ascertained by creating a dummy email using Mozilla Thunderbird,
then viewing the file in a text editor.  All values are mandatory.

Multiple "SendFromAccount" blocks can be specified, eg Hotmail, gMail, etc

FOR EXAMPLE:
case "$SendFromAccount" in
  "Hotmail")

  XIdentityKey=  # X-Identity-Key:
  XAccountKey=   # X-Account-Key:
  SendFromField= # From:
  ReplyTo=
  FCCField=
  UserAgent=
  OutboxFileName=
  ;;

  *)
  WARNING "Unrecognised SendFromAccount \"$SendFromAccount\""
  ;;
esac


UntilHere

fi





echo ""
echo "$ATTRBOLD### `basename $0` ###$ATTRCLEAR : Create Emails"

Boundary="^ boundary=" # This is a grep string for "drafts"
HTMLindent="    " # 4 Spaces
NewLine="<br>\\n"
NBSpace="\&nbsp;"
FSWidth=8

if [ "$1" = "--append" ]; then
  OutboxMode="append"
else
  OutboxMode="overwrite"
fi

# Stick a : on the end to make the cut work if the there is no delimiter
emlFileName="${emlFileName}:"
AttachMode="`echo "$emlFileName" | cut -f 2 -d ':'`"
FileSpec="`echo "$emlFileName" | cut -f 3 -d ':'`"
emlFileName="`echo "$emlFileName" | cut -f 1 -d ':'`"
[ -z "$AttachMode" ] && AttachMode="NoAttach"
if [ "$AttachMode" = "Auto" ]; then
  [ -z "$FileSpec" ] && FileSpec='*'
fi

echop "HOSTNAME" "$hostname"
echop "UserListFileName" "$UserListFileName"
echop "emlFileName" "$emlFileName"
echop "AttachMode" "$AttachMode"
if [ "$AttachMode" = "Auto" ]; then
  echop "UsersDir" "$UsersDir"
  echop "SubDir" "$SubDir"
  echop "FileSpec" "$FileSpec"
#  echop "Boundary" "$Boundary"
fi

echop "SendFromAccount" "$SendFromAccount" 
echop "OutboxFileName" "$OutboxFileName"
echop "OutboxMode" "$OutboxMode" # overwrite/append

if [ "$CRON" = "1" ]; then
  :
else
  echo ""
  WARNING "Please check all of the above settings, then:"
  pressenter
fi

if [ ! -r "$UserListFileName" ]; then
  echo ""
  ERROR "File not found: UserListFileName \"$UserListFileName\""
  echo "Check conv.conf"
  exit 1
fi

if [ ! -r "$emlFileName" ]; then
  echo ""
  ERROR "File not found: emlFileName \"$emlFileName\""
  echo "Check conv.conf"
  exit 1
fi

get_mimetype()
{
  # warning: assumes that the passed file exists
  file --mime-type "$1" | sed 's/.*: //' 
}

MakeFileSpecFileList() # NB Not for '*'
{
  [ -f "$0.tmp.$UserWholeName.filelist" ] && rm -f "$0.tmp.$UserWholeName.filelist"
  WhatsLeft="${FileSpec};" # Graft on a ';' to make the cut work correctly
  while true;
  do
    ThisSearchTerm="`echo "$WhatsLeft" | cut -d ';' -f 1 | sed 's/ \t//'`"
    WhatsLeft="`echo "$WhatsLeft" | cut -d ';' -f 2- | sed 's/ \t//'`"
  
    find "$UsersDir/$UserWholeName/$SubDir" -name "$ThisSearchTerm" -print | sort | sed 's/^/  /' > "$0.tmp.$UserWholeName.filelist.tmp"
    if [ `cat "$0.tmp.$UserWholeName.filelist.tmp" | wc -l` -eq 0 ]; then
      WARNING "No files found for \"$ThisSearchTerm\""
      WarningCount=$((WarningCount+1))
    else
      cat "$0.tmp.$UserWholeName.filelist.tmp" >> "$0.tmp.$UserWholeName.filelist"
    fi 
    rm -f "$0.tmp.$UserWholeName.filelist.tmp"
  
    if [ -z "$WhatsLeft" ]; then
      break
    fi
  done

  if [ -f "$0.tmp.$UserWholeName.filelist" ]; then
    sort -u "$0.tmp.$UserWholeName.filelist" > "$0.tmp.$UserWholeName.filelist.tmp" # Remove duplicates
    mv -f "$0.tmp.$UserWholeName.filelist.tmp" "$0.tmp.$UserWholeName.filelist"
  fi
}

FixDate() # The dates from stat are reversed...
{
YYYY=`echo $StatDate | cut -d '-' -f 1`
MM=`echo $StatDate | cut -d '-' -f 2`
DD=`echo $StatDate | cut -d '-' -f 3`
StatDate="$DD-$MM-$YYYY"
}

MakeHTMLFileList()
{
  Tag=$1
  [ -f "$0.tmp.$UserWholeName.HTMLFileList" ] && rm -f "$0.tmp.$UserWholeName.HTMLFileList"
  touch "$0.tmp.$UserWholeName.HTMLFileList"

  if [ "$Tag" = "UnzippedList" ]; then
    while read ThisFile
    do
      if [ -f "$ThisFile" ]; then
        WholeDateStamp=`stat --format="%y" "$ThisFile"  | cut -d '.' -f 1`
        StatDate="`echo $WholeDateStamp | cut -d ' ' -f 1`"
        FixDate
        StatTime="`echo $WholeDateStamp | cut -d ' ' -f 2`"
        [ "$OSdetected" = "Darwin" ] && StatTime=`echo "$StatTime" | sed 's/.0000000000//'`
        StatSize=`stat --format="%s" "$ThisFile"`
        StatName="`stat --format="%n" "$ThisFile"`"
        StatName="`basename $StatName`"
        echo "$StatDate $StatTime $StatSize $StatName" >> "$0.tmp.$UserWholeName.HTMLFileList"
      fi
    done < "$0.tmp.$UserWholeName.filelist"
  elif [ "$Tag" = "ZipFile" ]; then
    if [ -f "$UsersDir/$UserWholeName/$ProjectName.$UserWholeName.zip" ]; then
      WholeDateStamp=`stat --format="%y" "$UsersDir/$UserWholeName/$ProjectName.$UserWholeName.zip" | cut -d '.' -f 1`
      StatDate="`echo $WholeDateStamp | cut -d ' ' -f 1`"
      FixDate
      StatTime="`echo $WholeDateStamp | cut -d ' ' -f 2`"
      [ "$OSdetected" = "Darwin" ] && StatTime=`echo "$StatTime" | sed 's/.0000000000//'`

      StatSize=`stat --format="%s" "$UsersDir/$UserWholeName/$ProjectName.$UserWholeName.zip"`
      StatName="`stat --format="%n" "$UsersDir/$UserWholeName/$ProjectName.$UserWholeName.zip"`"
      StatName="`basename $StatName`"
      echo "$StatDate $StatTime $StatSize $StatName" > "$0.tmp.$UserWholeName.HTMLFileList"
    fi
  else
    ERROR "Invalid Tag sent to MakeHTMLFileList"
    exit 1
  fi
  
  if [ -z "$0.tmp.$UserWholeName.HTMLFileList" ]; then
    return
  fi

  cat "$0.tmp.$UserWholeName.HTMLFileList" | sort --field-separator ' ' -k 4 > "$0.tmp.$UserWholeName.HTMLFileList.tmp"
  mv -f "$0.tmp.$UserWholeName.HTMLFileList.tmp" "$0.tmp.$UserWholeName.HTMLFileList"

  AttachFileCount=0
  HTMLFileList="<tt>{{NewLine}}"
  while read fileLineIn
  do
    FileDate=`echo "$fileLineIn" | cut -d ' ' -f 1`
    FileTime=`echo "$fileLineIn" | cut -d ' ' -f 2`
    FileSize=`echo "$fileLineIn" | cut -d ' ' -f 3`
    FileName="`echo "$fileLineIn" | cut -d ' ' -f 4-`"
  
    FileSize=`commaformat $FileSize`
    Len=${#FileSize}

    while [ $Len -le $FSWidth ]
    do
      FileSize="${NBSpace}$FileSize"
      Len=$((Len+1))
    done
    HTMLFileList="${HTMLFileList}${HTMLindent}${FileDate} ${FileTime} ${FileSize} bytes - ${FileName}{{NewLine}}"
    AttachFileCount=$((AttachFileCount+1))
  
  done < "$0.tmp.$UserWholeName.HTMLFileList"

  if [ "$Tag" = "UnzippedList" ]; then
    if [ $AttachFileCount -eq 1 ]; then
      HTMLFileList="${HTMLFileList}${HTMLindent}""You have 1 file!</tt>{{NewLine}}{{NewLine}}"
    else
      HTMLFileList="${HTMLFileList}${HTMLindent}""You have $AttachFileCount files!</tt>{{NewLine}}{{NewLine}}"
    fi
  else
    HTMLFileList="${HTMLFileList}${HTMLindent}</tt>{{NewLine}}"
  fi

### Warning! Filenames with _ chars in them will cause a sed warning...
  cat "$0.tmp.$UserWholeName" | sed "s_{{$Tag}}_`echo -n "$HTMLFileList"`_" > "$0.tmp.$UserWholeName.tmp"

  mv -f "$0.tmp.$UserWholeName.tmp" "$0.tmp.$UserWholeName" 

  cat "$0.tmp.$UserWholeName" | sed "s_{{NewLine}}_${NewLine}_g" > "$0.tmp.$UserWholeName.tmp"
  mv -f "$0.tmp.$UserWholeName.tmp" "$0.tmp.$UserWholeName" 
  
  rm -f "$0.tmp.$UserWholeName.HTMLFileList"
}

if [ $AttachMode = "Auto" ]; then
  BoundaryLine=`cat "$emlFileName" | grep -e "$Boundary"`
  BoundaryLine=`echo $BoundaryLine | cut -d '=' -f 2 | tr -d '"'  | tr -d '\r'`

  if [ -z "$BoundaryLine" ]; then
    echo "No boundary line found \"$Boundary\""
    echo "Make sure the template email had an attachment..."
    exit 1
  fi
fi

rm -f "$0.tmp.newfile"
touch "$0.tmp.newfile"

# The original first "From - " line will inherit the emlFileName's datetamp
# From - Wed Jun 04 00:00:00 2014
CurrentDateStr="`date -r "$emlFileName" +"%a %b %d %T %Y"`"
cat "$emlFileName" | sed "s/^From -.*/From - $CurrentDateStr/" > "$emlFileName.tmp0"
mv -f "$emlFileName.tmp0" "$emlFileName"

# Adjust the "Date: " line to reflect the current date/time
#Date: Mon, 29 Dec 2014 17:47:33 +0000
CurrentDateStr="`date +"%a, %d %b %Y %T %z"`"

cat "$emlFileName" | sed "s/^Date:.*/Date: $CurrentDateStr/" > "$emlFileName.tmp0"
/bin/cp -f "$emlFileName.tmp0" "$emlFileName"

# Incorporate all the global "Include=Files"
WarningCount=0
grep -m 1 --quiet -e "{{Include=" "$emlFileName.tmp0"
while [ $? -eq 0 ];
do
  IncludeFile=`grep -m 1 -e "{{Include=" "$emlFileName.tmp0" | cut -f 2 -d '=' | sed 's/}}//'`
  if [ -f "$EmailDir/$IncludeFile" ]; then
    LineNo=`grep -n -e "{{Include=$IncludeFile" "$emlFileName.tmp0" | cut -f 1 -d ':'`
    head --lines=$((LineNo-1)) "$emlFileName.tmp0" > "$emlFileName.tmp1"
    cat "$EmailDir/$IncludeFile" >> "$emlFileName.tmp1"
    tail --lines=+$((LineNo+1)) "$emlFileName.tmp0" >> "$emlFileName.tmp1"

    mv -f "$emlFileName.tmp1" "$emlFileName.tmp0" 
  else
    WARNING "Include file not found: $EmailDir/$IncludeFile"
    WarningCount=$((WarningCount+1))
  fi
  # Remove the {{Include=}} place-holder
  cat "$emlFileName.tmp0"  | sed "/{{Include=$IncludeFile}}/d" > "$emlFileName.tmp1"
  mv -f "$emlFileName.tmp1" "$emlFileName.tmp0" 

  grep -m 1 --quiet -e "{{Include=" "$emlFileName.tmp0"
done

# Remove comments from the modified shell file,
# these must start in the first column
cat "$emlFileName.tmp0" | grep -v -e "^#" > "$emlFileName.tmp1"
mv -f "$emlFileName.tmp1" "$emlFileName.tmp0"

# Include the .conf file Mozilla tagging for whichever account is being used
cat "$emlFileName.tmp0" | sed "s/{{XIdentityKey}}/${XIdentityKey}/" > "$emlFileName.tmp1"
cat "$emlFileName.tmp1" | sed "s/{{XAccountKey}}/${XAccountKey}/" > "$emlFileName.tmp0"
cat "$emlFileName.tmp0" | sed "s/{{SendFromField}}/${SendFromField}/" > "$emlFileName.tmp1"
if [ -z "$ReplyTo" ]; then
  cat "$emlFileName.tmp1" | sed "/Reply-To:/d" > "$emlFileName.tmp0"
else
  cat "$emlFileName.tmp1" | sed "s/{{ReplyTo}}/${ReplyTo}/" > "$emlFileName.tmp0"
fi

cat "$emlFileName.tmp0" | sed "s^{{UserAgent}}^${UserAgent}^" > "$emlFileName.tmp1"
cat "$emlFileName.tmp1" | sed "s^{{FCCField}}^${FCCField}^" > "$emlFileName.tmp0"

rm -rf "$emlFileName.tmp1"

Today=`date +%Y/%m/%d` # Feed this string into date -d and you get 0000 for the time
DayNoToday=$((`date +%s -d $Today`/86400 ))
TwoWeeks=$((DayNoToday+15)) # Two weeks plus a day, because we started at 00:00
TwoWeeks=$((TwoWeeks*86400))
TwoWeeks="`date -d "@$TwoWeeks" "+%d/%m/%Y"`"

UserCount=0
while read userLineIn
do
  if [ -z "$userLineIn" ]; then
    continue
  fi

  if [ "${userLineIn:0:6}" = "break;" ]; then
    INFORMATION "break; encountered..."
    break
  fi

  userLineIn="`echo "${userLineIn}" | cut -d '#' -f 1 | sed 's/^[ \t]*//;s/[ \t]*$//'`"
  if [ -z "$userLineIn" ]; then
    continue
  fi

  UserCount=$((UserCount+1))
  UserFirstName="`echo "$userLineIn" | cut -d ',' -f 1`"
  
  echo "$UserFirstName" | grep --quiet -e ";"
  if [ $? -eq 0 ]; then
    UserSalutationName="`echo "$UserFirstName" | cut -d ';' -f 2`" # Must cut this first...
    UserFirstName="`echo "$UserFirstName" | cut -d ';' -f 1`"
  else
    UserSalutationName="$UserFirstName"
  fi
  
  UserLastName="`echo "$userLineIn" | cut -d ',' -f 2`"
  UserEmailAddress="`echo "$userLineIn" | cut -d ',' -f 3`"
  UserSearchStr="`echo "$userLineIn" | cut -d ',' -f 4`"
  UserWholeName="${UserFirstName}${UserLastName}"

# NB UserSearchStr could be used with flags to override the default data encoding...
  echo ""
  echo "User $UserCount"
  echo "$UserFirstName $UserLastName"
  echo "  $UserEmailAddress" 

  if [ -z "$UserEmailAddress" ] || [ "$UserEmailAddress" = "noemail" ]; then
    WARNING "User has a blank email address or it is set to \"noemail\"."
    WARNING "Skipping this user..."
    UserCount=$((UserCount-1))
    continue
  fi
 
  rm -f "$0.tmp.$UserWholeName"
  cp -f "$emlFileName.tmp0" "$0.tmp.$UserWholeName" ### First use of main emlFileName, post global SnR

  # http://forensicswiki.org/wiki/Thunderbird_Header_Format
  DatePart=$(printf "%x" `date +%s` | tr '[a-f]' '[A-F]')
  RandomPart=$((RANDOM%10))'0'$((RANDOM%10))'0'$((RANDOM%10))'0'$((RANDOM%10))
  # eg MessageID_Base="53B6D94E.3030709"

  MessageID_Base="$DatePart.$RandomPart"
  UserMessageIDLine="<${MessageID_Base}@${SendFromAccount}>"

  cat "$0.tmp.$UserWholeName" | sed "s/{{MessageID}}/$UserMessageIDLine/" > "$0.tmp.$UserWholeName.tmp"
  mv -f "$0.tmp.$UserWholeName.tmp" "$0.tmp.$UserWholeName" 

  # Need to do this first in case there are "tags" in the Personal Message file...
  grep --quiet -e "{{PersonalMessage}}" "$0.tmp.$UserWholeName"
  if [ $? -eq 0 ]; then
    if [ -f "$EmailDir/PM.$UserWholeName.eml" ]; then
      WARNING "Personal Message file PM.$UserWholeName.eml found!"
      WarningCount=$((WarningCount+1))
      LineNo=`grep -n -e "{{PersonalMessage}}" "$0.tmp.$UserWholeName" | cut -f 1 -d ':'`
      head --lines=$((LineNo-1)) "$0.tmp.$UserWholeName" > "$0.tmp.$UserWholeName.tmp"
      cat "$EmailDir/PM.$UserWholeName.eml" >> "$0.tmp.$UserWholeName.tmp"
      tail --lines=+$((LineNo+1)) "$0.tmp.$UserWholeName" >> "$0.tmp.$UserWholeName.tmp"
      mv -f "$0.tmp.$UserWholeName.tmp" "$0.tmp.$UserWholeName" 
      mv -f "$EmailDir/PM.$UserWholeName.eml" "$EmailDir/PM.$UserWholeName#.eml"
    else
      # Remove the {{PersonalMessage}} place-holder
      #echo "  No Personal Message file"
      cat "$0.tmp.$UserWholeName" | sed "/{{PersonalMessage}}/d" > "$0.tmp.$UserWholeName.tmp"
      mv -f "$0.tmp.$UserWholeName.tmp" "$0.tmp.$UserWholeName" 
    fi    
  fi

  cat "$0.tmp.$UserWholeName" | sed "s/{{ProjectName}}/$ProjectName/" > "$0.tmp.$UserWholeName.tmp"
  mv -f "$0.tmp.$UserWholeName.tmp" "$0.tmp.$UserWholeName" 
  
  cat "$0.tmp.$UserWholeName" | sed "s/{{VerNo}}/$VerNo/" > "$0.tmp.$UserWholeName.tmp"
  mv -f "$0.tmp.$UserWholeName.tmp" "$0.tmp.$UserWholeName" 

  cat "$0.tmp.$UserWholeName" | sed "s/{{DateStamp}}/$DateStamp/" > "$0.tmp.$UserWholeName.tmp"
  mv -f "$0.tmp.$UserWholeName.tmp" "$0.tmp.$UserWholeName" 

  cat "$0.tmp.$UserWholeName" | sed "s/{{SubDir}}/$SubDir/" > "$0.tmp.$UserWholeName.tmp"
  mv -f "$0.tmp.$UserWholeName.tmp" "$0.tmp.$UserWholeName" 

  cat "$0.tmp.$UserWholeName" | sed "s/{{SalutationName}}/$UserSalutationName/" > "$0.tmp.$UserWholeName.tmp"
  mv -f "$0.tmp.$UserWholeName.tmp" "$0.tmp.$UserWholeName" 

  cat "$0.tmp.$UserWholeName" | sed "s/{{FirstName}}/$UserFirstName/" > "$0.tmp.$UserWholeName.tmp"
  mv -f "$0.tmp.$UserWholeName.tmp" "$0.tmp.$UserWholeName" 
  
  cat "$0.tmp.$UserWholeName" | sed "s/{{LastName}}/$UserLastName/" > "$0.tmp.$UserWholeName.tmp"
  mv -f "$0.tmp.$UserWholeName.tmp" "$0.tmp.$UserWholeName" 
  
  cat "$0.tmp.$UserWholeName" | sed "s/{{EmailAddress}}/$UserEmailAddress/" > "$0.tmp.$UserWholeName.tmp"
  mv -f "$0.tmp.$UserWholeName.tmp" "$0.tmp.$UserWholeName" 

  cat "$0.tmp.$UserWholeName" | sed "s/{{UserSearchStr}}/$UserSearchStr/" > "$0.tmp.$UserWholeName.tmp"
  mv -f "$0.tmp.$UserWholeName.tmp" "$0.tmp.$UserWholeName" 

  cat "$0.tmp.$UserWholeName" | sed "s/{{DatedUsers}}/$DatedUsers/" > "$0.tmp.$UserWholeName.tmp"
  mv -f "$0.tmp.$UserWholeName.tmp" "$0.tmp.$UserWholeName" 

  cat "$0.tmp.$UserWholeName" | sed "s/{{DatedRosters}}/$DatedRosters/" > "$0.tmp.$UserWholeName.tmp"
  mv -f "$0.tmp.$UserWholeName.tmp" "$0.tmp.$UserWholeName" 

  cat "$0.tmp.$UserWholeName" | sed "s/{{TotalUsers}}/$TotalUsers/" > "$0.tmp.$UserWholeName.tmp"
  mv -f "$0.tmp.$UserWholeName.tmp" "$0.tmp.$UserWholeName" 

  cat "$0.tmp.$UserWholeName" | sed "s/{{TotalRosters}}/$TotalRosters/" > "$0.tmp.$UserWholeName.tmp"
  mv -f "$0.tmp.$UserWholeName.tmp" "$0.tmp.$UserWholeName" 

  cat "$0.tmp.$UserWholeName" | sed "s^{{TwoWeeks}}^$TwoWeeks^" > "$0.tmp.$UserWholeName.tmp"
  mv -f "$0.tmp.$UserWholeName.tmp" "$0.tmp.$UserWholeName" 

  if [ $AttachMode = "Auto" ]; then
    MakeFileSpecFileList
  fi

  if [ ! -s "$0.tmp.$UserWholeName.filelist" ]; then
    if [ $AttachMode = "Auto" ]; then
      WARNING "No files found for any search term..."
      WarningCount=$((WarningCount+1))
    fi
  else
    MakeHTMLFileList "ZipFile"
    MakeHTMLFileList "UnzippedList"

    NewBoundaryLine="${BoundaryLine}.$UserCount"
    cat "$0.tmp.$UserWholeName" | sed "s/$BoundaryLine/$NewBoundaryLine/" > "$0.tmp.$UserWholeName.tmp"
    mv -f "$0.tmp.$UserWholeName.tmp" "$0.tmp.$UserWholeName" 

    # Remove the existing final boundary line
    cat "$0.tmp.$UserWholeName" | sed "s/--${NewBoundaryLine}--//" > "$0.tmp.$UserWholeName.tmp"
    mv -f "$0.tmp.$UserWholeName.tmp" "$0.tmp.$UserWholeName" 

    # Now to add the attachements...
    if [ ! -d "$UsersDir/$UserWholeName/${SubDir}" ]; then
      echo "Cannot find user directory $UsersDir/$UserWholeName/${SubDir}"
      echo "Aborting attachment collection..."
      WarningCount=$((WarningCount+1))

      echo "" >> "$0.tmp.newfile"
      cat "$0.tmp.$UserWholeName" >> "$0.tmp.newfile"
      rm -f "$0.tmp.$UserWholeName"
    else
      if [ "$FileSpec" = '*' ]; then
        UserZipFile="`find "$UsersDir/$UserWholeName" -name '*.zip' -print`"
        if [ -z "$UserZipFile" ]; then
          UserUnzippedFiles="`find "$UsersDir/$UserWholeName/$SubDir" -type f -name '*' ! -name '*.zip' -print`"
          
          echo "$UserUnzippedFiles" > "$0.tmp.$UserWholeName.filelist" 
        else
          echo "$UserZipFile" > "$0.tmp.$UserWholeName.filelist"
        fi
      else
        : # MakeFileSpecFileList output will be remain as created
      fi
    fi

    ItemCount=0
    if [ ! -d "$UsersDir/$UserWholeName/${SubDir}" ]; then
      : # Warning message already done
    else
      echo "  Adding attachment(s):"
      while read AttachmentFile
      do
        echo "  AttachmentFile: $AttachmentFile"
        if [ ! -r "$AttachmentFile" ]; then
          WARNING "Cannot find file \"$AttachmentFile\""
          WarningCount=$((WarningCount+1))
          continue
        fi 
        AttachContentDisposition="attachment;"
        AttachFileName=`basename "$AttachmentFile"`
 
        # At the moment, everything is going to be base64 encoded...
        fileext=${AttachmentFile##*.}
        case $fileext in
        
          "zip")
            AttachContentType="application/zip;"
            AttachContentTransferEncoding="base64"
            ;; 

          "ods")
            AttachContentType="application/vnd.oasis.opendocument.spreadsheet;"
            AttachContentTransferEncoding="base64"
            ;; 

          "pdf")  
            AttachContentType="application/pdf;"
            AttachContentTransferEncoding="base64"
            ;;
            
          "txt")
            AttachContentType="text/plain; charset=UTF-8;"
            AttachContentTransferEncoding="base64"
            ;;
            
          "csv")
            AttachContentType="text/plain; charset=UTF-8;"
            AttachContentTransferEncoding="base64"
#            AttachContentTransferEncoding="7bit"
            ;;

          "png")
            AttachContentType="image/png;"
            AttachContentTransferEncoding="base64"
            ;;
            
          "jpg")
            AttachContentType="image/jpeg;"
            AttachContentTransferEncoding="base64"
            ;;

          *)
            # get_mimetype() # Take a guess here? 

            WARNING "Unrecognised file extension \"$fileext\""
            WarningCount=$((WarningCount+1))
            continue
            ;;
            # get_mimetype() # Take a guess here? 
        esac

        echo "--${NewBoundaryLine}" >> "$0.tmp.$UserWholeName" 
        echo "Content-Type: $AttachContentType" >> "$0.tmp.$UserWholeName" 
        echo " name=\"$AttachFileName\"" >> "$0.tmp.$UserWholeName" 
        echo "Content-Transfer-Encoding: $AttachContentTransferEncoding" >> "$0.tmp.$UserWholeName" 
        echo "Content-Disposition: $AttachContentDisposition" >> "$0.tmp.$UserWholeName" 
        echo " filename=\"$AttachFileName\"" >> "$0.tmp.$UserWholeName" 
        echo "" >> "$0.tmp.$UserWholeName" 

        if [ "$AttachContentTransferEncoding" = "base64" ]; then
          # The default wrap is 76, Thunderbird uses 72
          base64 --wrap=72 "$AttachmentFile" > "$0.tmp.$UserWholeName.encoded"
          cat "$0.tmp.$UserWholeName.encoded" >> "$0.tmp.$UserWholeName"
          rm -f "$0.tmp.$UserWholeName.encoded"
        else
          WARNING "Unrecognised encoding type \"$AttachContentTransferEncoding\""
          WarningCount=$((WarningCount+1))
          continue
        fi
        ItemCount=$((ItemCount+1))

      done < "$0.tmp.$UserWholeName.filelist"
      rm -f "$0.tmp.$UserWholeName.filelist"
    fi

    echo "${NewBoundaryLine}--" >> "$0.tmp.$UserWholeName" 
  fi #if [ $AttachMode = "Auto" ]; then

  echo "" >> "$0.tmp.newfile" 
  cat "$0.tmp.$UserWholeName" >> "$0.tmp.newfile"
  rm -f "$0.tmp.$UserWholeName"

  if [ "$AttachMode" = "Auto" ]; then
    echo "ItemCount: $ItemCount"
  fi

done < "$UserListFileName"

echo ""
rm -f "$emlFileName.tmp0"

OBdir="`dirname "$OutboxFileName"`"
[ ! -d "$OBdir" ] && mkdir -p "$OBdir"
[ -f "$OutboxFileName.msf" ] && rm -f "$OutboxFileName.msf"

if [ "$OutboxMode" = "overwrite" ]; then
  mv -f "$0.tmp.newfile" "$OutboxFileName"
else
  cat "$0.tmp.newfile" >> "$OutboxFileName"
  rm -f "$0.tmp.newfile"
fi

unix2dos "$OutboxFileName"

echo ""
echo "Users    : $UserCount"
echo "Warnings : $WarningCount"
exit 0

Massive I know.

Last edited by RudiC; 08-06-2018 at 05:37 AM.. Reason: Obfuscated real mail user account
 

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

auto startup of a program

how do i make a program run at system startup? is there a hirarchy i have to consider? is there any equivalent to an NT "Service" in unix? how do you set it up? thanx Ron (2 Replies)
Discussion started by: exoron
2 Replies

2. UNIX for Dummies Questions & Answers

how to capture bounced emails

Hi, I use mailx to send short notifications to a bunch of users ( in Lotus Notes ). The undeliverable emails are going to the Notes' Dead Letter basket and creating a problem. I am told by the Notes Admin that I need to specify a 'Return address' so that the bounced emails can go there. I can not... (1 Reply)
Discussion started by: ray1972
1 Replies

3. UNIX for Dummies Questions & Answers

Creating emails

Hi, I posted here 2 months ago, saying I wanted to learn UNIX for my work running a small ISP. I had the problem of practically no technical support for our system. Many courses later, I can just about do everything I need to do. The server is a FreeBSD i386. I can now create email accounts for... (4 Replies)
Discussion started by: ferret495
4 Replies

4. UNIX for Dummies Questions & Answers

duplicate emails

Hi all new here. Question. My mail server is sending out duplicate emails but its not system wide. We have several virtual host that use our email server and some are having the duplicate emails issue and some are not. For example in the office some people are getting duplicate emails but i am... (1 Reply)
Discussion started by: mcraul
1 Replies

5. UNIX for Dummies Questions & Answers

Emails on HP-UX

Hi Guys I am running Oracle database on HP-UX, every now and then I get emails that are sent to me by root. When check with the sysadmin know one have a clue as to where does the emails come from. I need to stop this emails...how do I check which script is sending me the emails. Thanks ... (0 Replies)
Discussion started by: Phuti
0 Replies

6. Programming

Sending emails in C program for multiple machines on Linux/OSX

Hey guys, i am creating a tool that'll run a couple network test, generate a report then email the report. Now i a bit stuck with the email sending part... I tried at first a script which worked on some machines but then it'll work fine on some machine and act up on others... I can't really rely... (2 Replies)
Discussion started by: Jess83
2 Replies

7. UNIX for Dummies Questions & Answers

send emails

hi guys I need to send emails to some accounts 1. I need sendmail up and running right? 2. I need my local sendmail to use an existing SMTP server to send emails (10.x.x.x)? in that case what I need to configure to send emails? since using mail myaccount@companyame.com is not sending emails... (3 Replies)
Discussion started by: karlochacon
3 Replies

8. Solaris

Emails issue

Dear all, I am facing an issue in my data Center environment related to mails being generated by users on my machines. Let me walk you through the issue one by one. Each system (100+ servers in my DC) has a user sysmon on it that sends mails to a specific Email ID. The user runs scripts... (8 Replies)
Discussion started by: Junaid Subhani
8 Replies

9. UNIX for Beginners Questions & Answers

Howto auto boot SPARC | How to auto supply "start /SYS" and "start /SP/console" commands

When I power ON my T4-1, I got a prompt -> where I have to start /SYS and start /SP/console. How can I auto supply these two commands ? (3 Replies)
Discussion started by: z_haseeb
3 Replies
All times are GMT -4. The time now is 06:50 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy