Created
July 30, 2020 05:01
-
-
Save eNeRGy164/edd3becb0b3acd6c0f634fca3b108a5e to your computer and use it in GitHub Desktop.
Getting email recipient addresses from maildir
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/sh | |
# | |
# Get a directory name as input and grep the address the mail is sent to | |
# | |
# Based on a script by Joerg Reinhardt, but don't blame him for my mistakes | |
# Check for argument or help argument respectively | |
if [[ ($1 == "") || | |
($1 == "-h") || | |
($1 == "--help") || | |
($1 == "-help") ]]; then | |
echo "Usage: "$0" <Mail-directory> <Email-domain>"; | |
fi; | |
# Make regex comparison case insensitive | |
shopt -s nocasematch | |
# Check if parameter is a directory | |
if [[ -d $1 ]]; then | |
# Set target filename | |
dirname=`echo $1 | awk '{while(substr($0,length($0),1)=="/"){$0=substr($0,1,length($0)-1);}print $0;}'`; | |
dumpfile=$dirname'.dump'; | |
# Check if directory is empty | |
if [[ `find $dirname -type f` == "" ]]; then | |
echo $dirname": directory empty." | |
exit 1; | |
fi; | |
regexDomain=$2+"$"; | |
# Collect files inside maildir directories | |
find $1 -regex '.*/cur/.*' -type f -print0 | while read -d $'\0' i; do | |
# Output progress information | |
echo -n -e \\r" " | |
echo -n -e \\rprocessing "$i" | |
# Look for senders email address in the order | |
# 'To:' | |
# 'X-Original-To:' | |
# 'Delivered-To:' | |
fromline=`grep '^To: ' "$i"`; | |
# Parse 'To:' field | |
from=`echo $fromline | awk 'BEGIN{FS="<";}{if($0~/</) {pos=index($2,">");if(pos!=0) {print substr($2,1,pos-1);}} else {pos=index($0,":");print substr($0,pos+1);}}'`; | |
if [[ $from == "" || ! $from =~ $regexDomain ]]; then | |
# Parse 'X-Original-To:' Field | |
fromline=`grep '^X-Original-To: ' "$i"`; | |
from=`echo $fromline | awk 'BEGIN{FS="Line:";}{print $2;}'`; | |
if [[ $from == "" || ! $from =~ $regexDomain ]]; then | |
fromline=`grep '^Delivered-To: ' "$i"`; | |
# Parse 'Delivered-To:' field | |
from=`echo $fromline | awk 'BEGIN{FS="<";}{if($0~/</) {pos=index($2,">");if(pos!=0) {print substr($2,1,pos-1);}} else {pos=index($0,":");print substr($0,pos+1);}}'`; | |
if [[ $from == "" || ! $from =~ $regexDomain ]]; then | |
# Looks like this email was not send to the user | |
continue; | |
fi; | |
fi; | |
fi; | |
echo $from | tr '[:upper:]' '[:lower:]' >> $dumpfile; | |
done; | |
echo; | |
else | |
echo $1": not a directory."; | |
fi; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment