eu peguei essa entrada enquanto procurava algo do mesmo.
e dos telespectadores aqui e mais alguma pesquisa eu consegui fazer este script.
#!/bin/sh
# Default reception
TOEMAIL="[email protected]";
# Default Subject
SUBJECT="You got mail - $DATE";
# Default Contents
MSGBODY="Hello, this is the default message body";
# Default Attachment
#ATTACHMENT="/tmp/testoutput"
# Default smtp server
mailserver="smtp.server.ltd"
mailserverPort="25"
showUsage() {
echo "$0 -a /file/to/attach [-m /message/file] [-M \"Message string\"] -s \"subject\" -r [email protected]"
echo
echo "The attachment (-a) is required, if no attachment is used then rather use sendmail directly."
}
fappend() {
echo "$2">>$1;
}
DATE='date'
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# This might need correction to work on more places, this is tested at a ubuntu 13.10 machine. #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
domain='grep search /etc/resolv.conf | awk '{print $2;}''
computer='hostname'
user='whoami'
FREMAIL="$user@$computer.$domain"
while getopts "M:m:a:s:r:" opt; do
case $opt in
s)
SUBJECT="$OPTARG - $DATE"
;;
r)
TOEMAIL="$OPTARG"
;;
m)
MSGBODY='cat $OPTARG'
;;
M)
MSGBODY="$OPTARG"
;;
a)
ATTACHMENT="$OPTARG"
;;
:)
showUsage
;;
\?)
showUsage
;;
esac
done
if [ "$ATTACHMENT" = "" ]; then
showUsage
exit 1
fi
MIMETYPE='file --mime-type -b $ATTACHMENT'
TMP="/tmp/tmpmail_"'date +%N';
BOUNDARY='date +%s|md5sum|awk '{print $1;}''
FILENAME='basename $ATTACHMENT'
DATA='cat $ATTACHMENT|base64'
rm $TMP 2> /dev/null
fappend $TMP "EHLO $computer.$domain"
fappend $TMP "MAIL FROM:<$FREMAIL>"
fappend $TMP "RCPT TO:<$TOEMAIL>"
fappend $TMP "DATA"
fappend $TMP "From: $FREMAIL"
fappend $TMP "To: $TOEMAIL"
fappend $TMP "Reply-To: $FREMAIL"
fappend $TMP "Subject: $SUBJECT"
fappend $TMP "Content-Type: multipart/mixed; boundary=\"$BOUNDARY\""
fappend $TMP ""
fappend $TMP "This is a MIME formatted message. If you see this text it means that your"
fappend $TMP "email software does not support MIME formatted messages."
fappend $TMP ""
fappend $TMP "--$BOUNDARY"
fappend $TMP "Content-Type: text/plain; charset=UTF-8; format=flowed"
fappend $TMP "Content-Disposition: inline"
fappend $TMP ""
fappend $TMP "$MSGBODY"
fappend $TMP ""
fappend $TMP ""
fappend $TMP "--$BOUNDARY"
fappend $TMP "Content-Type: $MIMETYPE; name=\"$FILENAME\""
fappend $TMP "Content-Transfer-Encoding: base64"
fappend $TMP "Content-Disposition: attachment; filename=\"$FILENAME\";"
fappend $TMP ""
fappend $TMP "$DATA"
fappend $TMP ""
fappend $TMP ""
fappend $TMP "--$BOUNDARY--"
fappend $TMP ""
fappend $TMP "."
fappend $TMP "quit"
netcat $mailserver $mailserverPort < $TMP >> $TMP
rc="$?"
if [ "$rc" -ne "0" ]; then
echo "Returncode: $rc"
echo "Please inspect $TMP"
else
rm $TMP;
fi
Uma coisa que você pode querer adicionar é a autenticação. Eu não preciso disso, então eu não adicionei.
Acho que só é necessário md5sum , netcat , , awk e arquivo64 comandos, acho que eles são bastante padrão na maioria dos sistemas.