使用 telnet 或 netcat 发送带有附件的电子邮件

Kyl*_*ndt 15 smtp telnet netcat

我经常使用telnet或netcat连接smtp服务器发送邮件作为测试。

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

我会很高兴使用一个小的 bash shell 来实现目标的解决方案,但不想使用任何其他工具......

Kyl*_*ndt 10

好的,所以使用每个人的评论作为起点,我想出了这个愚蠢的烂摊子:-) ...

{ 
    sleep 5; 
    echo 'ehlo';
    sleep 3;
    echo 'MAIL FROM:<Test@test.com>';
    sleep 3; 
    echo 'RCPT TO: <kyle@test_dest.com>';
    sleep 3;
    echo 'DATA';
    sleep 3;
    echo -e 'To:kyle@testdest.com\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
Run Code Online (Sandbox Code Playgroud)


Eva*_*son 8

哎呀。您将不得不对附件进行 base64 编码并创建 MIME 标头。

与其每次“即时”生成一条新消息,不如从“真实”电子邮件程序中给自己发送一个非常简短的示例消息(利用编写它的人所做的工作来放置附件)可能会更容易转换为正确的编码并创建 MIME 标头)。

将该消息保存到带有标题的文本文件中(当然,删除传输标题),然后只需将其修改/复制/粘贴到 telnet 或 netcat 中以备将来使用。


hay*_*lci 6

虽然手动测试 SMTP 服务器是可能且可行的,但使用为此设计的工具会容易得多。

这篇文章解释了 SWAKS。swaks 专为 smtp 服务器测试而设计。支持附件、认证和加密!


小智 5

我在寻找相同的东西时偶然发现了这个条目。从这里的 awnsers 和一些额外的研究,我设法制作了这个脚本。

#!/bin/sh

# Default reception
TOEMAIL="myEmail@domain.ltd";
# 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 receiver@domain.com"
        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
Run Code Online (Sandbox Code Playgroud)

您可能想要添加的一件事是身份验证。我不需要它所以我没有添加它。

我认为它只需要md5sumnetcatfileawkbase64命令,我猜它们在大多数系统中都是非常标准的。


小智 5

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

猫附件.zip | Base64 > zip.txt
猫附件.pdf | Base64 > pdf.txt

# 内容类型:文本/csv;name="$FILE" # 对于 CSV 文件
# 内容类型:application/x-msdownload;name="$FILE" # 可执行文件
# 内容类型:text/xml; name="$FILE" # 对于 xml 文件或尝试 application/xml

telnet smtp.server.dom 25

直升机
邮件来自:email@server.com
RCPT 至:email@server.com
数据
主题:测试电子邮件
来自:email@server.com
至:email@server.com
MIME 版本:1.0
内容类型:多部分/混合;边界=“X-=-=-=-文本边界”

--X-=-=-=-文本边界
内容类型:文本/纯文本

将您的留言放在这里...

--X-=-=-=-文本边界
内容类型:应用程序/zip;名称=“文件.zip”
内容传输编码:base64
内容处置:附件;文件名=“文件.zip”

UEsDBBQAAAAIAG1+zEoQa...复制/粘贴 zip.txt

--X-=-=-=-文本边界
内容类型:文本/pdf;名称=“文件.pdf”
内容传输编码:base64
内容处置:附件;文件名=“文件.pdf”

UEsDBBQAAAAIAG1+zEoQa...复制/粘贴 pdf.txt

--X-=-=-=-文本边界
。

辞职

  • 欢迎来到服务器故障。非常欢迎像您这样发布答案的人来到这个网站!:) 格拉茨 (3认同)