使用telnet或netcat发送附加文件的电子邮件

我经常使用telnetnetcat来连接smtp服务器来发送电子邮件作为testing。

有谁知道你会如何使用telnet或netcat发送电子邮件, 但也附上一个文件 ? 有可能有更好的方法,但我仍然想知道:-)

我会很高兴的解决scheme,使用一个小小的bashshell来实现目标,但不想使用任何其他工具…

    好吧,以每个人的意见为出发点,我想出了这个愚蠢的混乱:-) …

    { sleep 5; echo 'ehlo'; sleep 3; echo 'MAIL FROM:<[email protected]>'; sleep 3; echo 'RCPT TO: <kyle@test_dest.com>'; sleep 3; echo 'DATA'; sleep 3; echo -e 'To:[email protected]\nMIME-Version: 1.0 (mime-construct 1.9)\nContent-Type: application/zip\nContent-Transfer-Encoding: base64\n\n'; dd if=/dev/urandom bs=4 count=10 2>/dev/null | openssl base64; echo '.'; } | telnet mx1.testdest.com 25 

    伊克。 你将不得不base64编码的附件,并创buildMIME头。

    而不是每次都“随时”生成一条新消息,只需要给自己发送一封来自“真正的”电子邮件程序的非常简短的示例消息(利用写这些消息的人员所做的工作来放置附件进入正确的编码和创buildMIME头)。

    将该消息保存到一个带有标题的文本文件(当然,删除传输标题),然后将其修改/复制/粘贴到远程会话的telnet或netcat中。

    尽pipe手工testingSMTP服务器是可行的,但使用为此devise的工具将更容易。

    这篇文章解释了SWAKS 。 swaks是专为smtp服务器testing而devise的。 支持附件,authentication和encryption!

    当我正在寻找相同的东西时, 并从这里的awnsers和额外的研究,我设法使这个剧本。

     #!/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 

    你可能想要添加的一件事是身份validation。 我不需要它,所以我还没有添加它。

    我认为它只需要md5sumnetcat文件awkbase64命令,编号猜测它们在大多数系统中是非常标准的。

    这是我正在做什么用bash发送电子邮件。 我用它来给我一个日志文件和外部IP地址,随时使用它:

     #!/bin/bash # Send email from bash with attachment # by Psirac - www.subk.org [email protected] [email protected] mailserver=smtp.test.com mylogin=`echo 'MYUSERNAME' | openssl base64` mypassword=`echo 'MYPASSWORD' | openssl base64` myip=`wget -qO- icanhazip.com` myfile=`cat /tmp/mytest.log | openssl base64` mydate=`date` exec 9<>/dev/tcp/$mailserver/25 echo "HELO routeur.tripfiller" >&9 read -r temp <&9 #echo "$temp" echo "auth login" >&9 read -r temp <&9 #echo "$temp" echo "$mylogin=" >&9 read -r temp <&9 #echo "$temp" echo "$mypasswd=" >&9 read -r temp <&9 #echo "$temp" echo "Mail From: $from" >&9 read -r temp <&9 #echo "$temp" echo "Rcpt To: $to" >&9 read -r temp <&9 #echo "$temp" echo "Data" >&9 read -r temp <&9 #echo "$temp" echo "To:$to" >&9 echo "MIME-Version: 1.0" >&9 echo "Subject: Test mail sended at $mydate" >&9 echo "From: $from" >&9 echo "To: $to" >&9 echo "Content-Type: multipart/mixed; boundary=sep" >&9 echo "--sep" >&9 echo "Content-Type: text/plain; charset=UTF-8" >&9 echo "Here your text..." >&9 echo "External IP adress: $myip" >&9 echo "--sep" >&9 echo "Content--Type: text/x-log; name=\"mytest.log\"" >&9 echo "Content-Disposition: attachment; filename=\"mytest.log\"" >&9 echo "Content-Transfer-Encoding: base64" >&9 echo "" >&9 echo "$myfile" >&9 echo "--sep--" >&9 echo "." >&9 read -r temp <&9 echo "$temp" echo "quit" >&9 read -r temp <&9 echo "$temp" 9>&- 9<&- #echo "All Done. See above for errors" exit 0 

    希望对你有好处;)

    psirac。

    Telnet – 发送带有多个附件的电子邮件

    猫attachment.zip |  base64> zip.txt
     cat attachment.pdf |  base64> pdf.txt
    
     #Content-Type:text / csv;  name =“$ FILE”#CSV文件
     #Content-Type:application / x-msdownload;  name =“$ FILE”#为可执行文件 
     #Content-Type:text / xml;  name =“$ FILE”#用于xml文件或者尝试application / xml
    
     telnet smtp.server.dom 25
    
     HELO
    邮件来自:[email protected]
     RCPT TO:[email protected]
    数据
    主题:testing电子邮件
    来自:[email protected]
    致:[email protected]
     MIME版本:1.0
    内容types:multipart / mixed;  boundary =“X  -  =  -  =  -  =  - 文本边界”
    
     --X  -  =  -  =  -   -   - 文本边界
    内容types:文本/纯文本
    
    把你的信息放在这里
    
     --X  -  =  -  =  -   -   - 文本边界
    内容types:应用程序/邮编;  NAME = “file.zip”
    内容传输编码:base64
    内容处理:附件; 文件名= “file.zip”
    
     UEsDBBQAAAAIAG1 + zEoQa ....复制/粘贴zip.txt
    
     --X  -  =  -  =  -   -   - 文本边界
    内容types:text / pdf;  NAME = “file.pdf”
    内容传输编码:base64
    内容处理:附件; 文件名= “file.pdf”
    
     UEsDBBQAAAAIAG1 + zEoQa ....复制/粘贴pdf.txt
    
     --X  -  =  -  =  -   -   - 文本边界
     。
    
    放弃
    

    您需要查看SMTP协议规范。 对于技术规范来说,这是一个令人惊讶的轻量级阅读,并将帮助您了解电子邮件过程的工作原理。

    具体来说,要认识到附件被转换成MIMEtypes并以文本编码,所以你想通过telnet发送的附件必须被转换成文本并通过telnet协议传输。

    如果你正在testing的是'附件是否交付',你可能会使用附件的pre-MIME标准:uuencode。 与MIME不同,创build消息要简单得多。 不像MIME,它不需要任何头文件。 但是,并非所有的邮件客户端都将uuencoded文件识别为附件,所以我build议您testing一下是否可以使用它。 如果是这样的话,你可以为自己省下很多努力。 如果没有,那么通过perl或者其他方法预先构build你的MIMEed消息,然后通过类似于NetCat的方式来pipe理它,这可能是一个很好的select。

    值得一看。

    这个工作有一个很棒的Perl脚本。 你可以在这里find它:

    http://www.logix.cz/michal/devel/smtp-cli/

    smtp-cli v2.9

    脚本来自作者:Michal Ludvig(c)2003-2011 http://smtp-cli.logix.cz

    我自己使用它,这是伟大的,感谢米哈尔;)