#!/bin/bash MAILID=$1 [ -z "$MAILID" ] && echo "Usage : $0 [mailid]" && exit 1 DEBUG=0 [ "$2" == "-d" ] && DEBUG=1 MAX_LOG=9 HOSTNAME=$( hostname -f ) function search_sender () { MAILID=$1 MAILLOG=$2 [ ! -f "$MAILLOG" ] && MAILLOG=$MAILLOG.gz [ -f "$MAILLOG" ] && zgrep $MAILID $MAILLOG|grep 'from='|sed 's/^.*\: from=<\([^>]*\)>.*$/\1/'|head -n 1 } function search_sender_from_mailid() { MAILID=$1 [ $DEBUG -eq 1 ] && echo "Search sender from Mail ID $MAILID ..." res=$( search_sender $MAILID /var/log/mail.log ) if [ -n "$res" ] then echo "$res" exit 0 else [ $DEBUG -eq 1 ] && echo "Not found from log file /var/log/mail.log. Try with archive (Max : $MAX_LOG)" for i in $( seq 1 $MAX_LOG ) do res=$( search_sender $MAILID /var/log/mail.log.$i ) if [ -n "$res" ] then [ $DEBUG -eq 1 ] && echo "Found from log file /var/log/mail.log.$i" echo "$res" exit 0 else [ $DEBUG -eq 1 ] && echo "Not found from log file /var/log/mail.log.$i" fi done [ $DEBUG -eq 1 ] && echo "Not found in archive (Max : $MAX_LOG)" fi } if [ $( mailq|egrep -c ^$MAILID ) -ne 0 ] then [ $DEBUG -eq 1 ] && echo "Mail $MAILID still in mailq. Try to detect original mail ID ..." ORIGMAILID=$( postcat -q "$MAILID" |grep "by $HOSTNAME"|tail -n 1|sed 's/^.*by .* with.* id \([A-Z0-9]*\).*$/\1/' ) if [ -n "$ORIGMAILID" ] then if [ "$ORIGMAILID" != "$MAILID" ] then [ $DEBUG -eq 1 ] && echo "Original mail ID : $ORIGMAILID" search_sender_from_mailid "$ORIGMAILID" [ $DEBUG -eq 1 ] && echo "Sender not found from original mail ID" exit 1 else [ $DEBUG -eq 1 ] && echo "Mail ID and original mail ID are equals" fi else [ $DEBUG -eq 1 ] && echo "Fail to detect original mail ID. Continue with mail ID in parameter" fi fi search_sender_from_mailid "$MAILID" [ -n "$ORIGMAILID" ] && echo "Sender not found" && exit 1 [ $DEBUG -eq 1 ] && echo "Sender not found. Try to detect original mail ID with message ID in log" MSGID=$( mailid_to_messageid "$MAILID" ) if [ -z "$MSGID" ] then [ $DEBUG -eq 1 ] && echo "Message ID not found from log." exit 1 fi [ $DEBUG -eq 1 ] && echo "Message ID found : $MSGID. Try to detect original mail ID with this message ID" ORIGMAILID=$( messageid_to_original_mailid "$MSGID" ) if [ -z "$ORIGMAILID" ] then [ $DEBUG -eq 1 ] && echo "Original mail ID not found from message ID" exit 1 fi if [ "$ORIGMAILID" == "$MAILID" ] then [ $DEBUG -eq 1 ] && echo "Original mail ID is same as mail ID in parameter" exit 1 fi search_sender_from_mailid "$ORIGMAILID" [ $DEBUG -eq 1 ] && echo "Sender not found from original mail ID" exit 1