not good:
3 phases :
1 --> the mail is for admin or CRM ? --> forward to admin or crm && return 0
2 --> the mail is crap --> delete it && return 0
3 --> none of the previous --> send to bdd by api rest call http
so the sendmail is not always the final decision ...
I feel that the originale recipient is kept so the mail is looping ...
for now, my code is as follow :
#!/bin/bash
#
LOGFILE=/var/log/filter_dyn_v2.log
touch $LOGFILE
exec >> $LOGFILE
exec 2>&1
echo "Parametres : $@"
# je bascule le message dans le descripteur 3
msg=$(mktemp /tmp/msg.XXXXXX) || exit 75
cat > $msg || { rm $msg; exit 75; }
exec 3< $msg || { rm $msg; exit 75; }
rm $msg
# je traite les paramètres de l'appel
sender=${1:-}
shift
recipient=${1:-}
shift
queue_id=${1:-$RANDOM}
shift
CRM="contact_a...@mondomaine.com"
ADMIN="stephane.me...@mondomaine.com"
API_PATH="http://api.mondomaine.com/api/V1.0/add_mail"
atraiter()
{
echo "[$queue_id] appel api"
perl -e 'open IN, "<&", 3; seek(IN, 0, 0);'
lemail=$(cat <&3)
lefrom=$( echo "$lemail" | grep -E "^From:.*" )
leto=$( echo "$lemail" | grep -E "^To:.*" )
lesujet=$( echo "$lemail" | grep -E "^Subject:.*" )
lereplyto=$( echo "$lemail" | grep -E "^Reply-To:.*" )
echo "curl -s -X POST -F \"queue_id=$queue_id\" -F
\"sujet=$lesujet\" -F \"mailfrom=$lefrom\" -F \"mailto=$leto\" -F
\"replyto=$lereplyto\" -F \"body=lemail\" \"$API_PATH\""
curl -s -X POST -F "queue_id=$queue_id" -F "sujet=$lesujet" -F
"mailfrom=$lefrom" -F "mailto=$leto" -F "replyto=$lereplyto" -F
"body=$lemail" "$API_PATH"
if [ "0" -ne "$?" ]
then
echo "[$queue_id] ERREUR CURL : $?"
return 75
fi
echo
echo "[$queue_id] CURL OK"
return 0
}
renvoi()
{
# renvoi du mail a un destinataire
local destinataire=$1
echo "[$queue_id] j'envoi le mail a $destinataire"
perl -e 'open IN, "<&", 3; seek(IN, 0, 0);'
set -- "$@" "$additional_recipient"
exec /usr/sbin/sendmail -vv -i "$@" <&3
return $?
}
now=$(date +"%m/%d/%Y")
timenow=$(date +"%Hh%Mm%Ss")
echo "[$queue_id] $now $timenow"
perl -e 'open IN, "<&", 3; seek(IN, 0, 0);'
if grep -Ei "(operation numero|indice de
frequence|signaletique|stephane@domaine)" <& 3 >/dev/null; then
renvoi $CRM
exit $?
fi
perl -e 'open IN, "<&", 3; seek(IN, 0, 0);'
if grep -Ei "(assistance admin|help admin)" <& 3 >/dev/null; then
renvoi $ADMIN
exit $?
fi
#trash
perl -e 'open IN, "<&", 3; seek(IN, 0, 0);'
if grep -Ei "(demande rejet|application refusee|Content-Type:
application\/zip)" <& 3 >/dev/null; then
echo "[$queue_id] $now $timenow non pris en compte, je trash"
exit 0
fi
# j'enregistre tout le reste dans la base
atraiter
exit $?
a lot simplier than before, but still I feel to make it bad with my :
lemail=$(cat <&3) but the easiest way to get the content for the API.
Stéphane
Le 22/12/2016 à 19:12, Viktor Dukhovni a écrit :
On Dec 22, 2016, at 12:36 PM, Wietse Venema <wie...@porcupine.org> wrote:
# Final command. Shell will remove the temp file and exit with
# Sendmail's exit code.
/usr/sbin/sendmail "$@"
And do not forget the '--' in
pipe ... argv=/path/to/script -f ${sender} -- ${recipient}
And the missing "< $msg" in the final command:
/usr/sbin/sendmail "$@" < $msg