该项目由John Jetmore维护,遵循 GNU GPLv2开源协议。
下载之后得到swaks-20240103.0.tar.gz压缩包,解压之后将其中swaks文件拷贝到/usr/bin目录,并设置可执行权限。示例命令:
# 解压文件
tar -zxvf swaks-20240103.0.tar.gz
# 拷贝文件至/usr/bin目录
cd swaks-20240103.0 && cp swaks /usr/bin
# 设置可执行权限
chmod 755 /usr/bin/swaks
# 执行swaks命令查看帮助
swaks --help
- --server:指定SMTP服务器进行邮件发送。
指定收件人和发件人:
- --to <收件人地址>:指定邮件的收件人。
- --from <发件人地址>:指定邮件的发件人。
邮件主题:
- --header "Subject: xxx":指定邮件主题。
- --h-Subject xxx:与上面效果相同。
邮件正文:
- --body <邮件正文内容>:指定邮件正文的内容,默认是纯文本内容。
- --attach-type text/plain --attach-body 'body text' :与上述--body效果相同。如需发送html格式的邮件,可以参考下文的示例。
使用身份验证:Swaks支持通过SMTP认证(例如:PLAIN、LOGIN、CRAM-MD5等)来连接邮件服务器。如果你的SMTP服务器需要认证,可以使用以下选项:
- --auth LOGIN:使用LOGIN认证。
- --auth PLAIN:使用PLAIN认证。
- --auth CRAM-MD5:使用CRAM-MD5认证,依赖Digest::MD5模块。
- --auth DIGEST-MD5:使用DIGEST-MD5认证,依赖Authen::SASL模块。
- --auth CRAM-SHA1:使用 CRAM-SHA1认证依赖Digest::SHA模块。
- --auth-user <用户名>:指定用户名。
- --auth-password <密码>:指定密码。
启用TLS加密:Swaks也支持通过TLS加密与SMTP服务器的连接。关于TLS部分,Swaks提供了诸多功能,建议查看帮助文档获取更多信息。
- --tls:使用TLS加密连接。
指定SMTP端口:如果SMTP服务器使用非默认端口(默认是25),你可以指定端口号:
- --port <端口号>:指定SMTP服务的端口(例如,465用于SSL,587用于TLS)。
发送带附件的邮件:Swaks支持发送带附件的邮件。
- --attach @<附件路径>:选项指定附件文件路径,注意@符号。
swaks --to t1@mailabc.cn --from demo@mailabc.cn --server mail.mailabc.cn --auth LOGIN --auth-user "[email protected]" --auth-password "BiC9E7YjbdDI7TkH" --header "Subject: Test" --body "This is a test message"
[root@localhost ~]
*** DEPRECATION WARNING: Relying on IO::Socket::INET to send via inet sockets. Install IO::Socket::IP instead.
=== Trying mail.mailabc.cn:25...
=== Connected to mail.mailabc.cn.
<- 220 mailabc.cn
-> EHLO localhost
<- 250-mail
<- 250-PIPELINING
<- 250-AUTH LOGIN PLAIN
<- 250-AUTH=LOGIN PLAIN
<- 250-STARTTLS
<- 250-SMTPUTF8
<- 250 8BITMIME
-> AUTH LOGIN
<- 334 dXNlcm5hbWU6
-> ZGVtb0BtYWlsYWJjLmNu
<- 334 UGFzc3dvcmQ6
-> QmlDOUU3WWpiZERJN1RrSA==
<- 235 Authentication successful
-> MAIL FROM:<[email protected]>
<- 250 Mail OK
-> RCPT TO:<[email protected]>
<- 250 Mail OK
-> DATA
<- 354 End data with <CR><LF>.<CR><LF>
-> Date: Thu, 17 Apr 2025 21:10:05 +0800
-> To: [email protected]
-> From: [email protected]
-> Subject: Test
-> Message-Id: <20250417211005.011543@localhost>
-> X-Mailer: swaks v20240103.0 jetmore.org/john/code/swaks/
->
-> This is a test message
->
->
-> .
<- 250 Mail OK queued as AQAAfwDnTiYNcQBoUAEAAA--.32S2
-> QUIT
<- 221 Bye
=== Connection closed with remote host.
swaks --to [email protected] --from [email protected] --server mail.mailabc.cn --auth LOGIN --auth-user "[email protected]" --auth-password "BiC9E7YjbdDI7TkH" --tls --header "Subject: Test2" --body "This is a TLS test message"
cpan install Net::SSLeay
swaks --to [email protected] --from [email protected] --server mail.mailabc.cn --auth LOGIN --auth-user "[email protected]" --auth-password "BiC9E7YjbdDI7TkH" --header "Subject: Test3" --body "This is a attach test message" --attach @/tmp/file.txt
swaks --to [email protected] --from [email protected] --server mail.mailabc.cn --auth LOGIN --auth-user "[email protected]" --auth-password "BiC9E7YjbdDI7TkH" --header "Subject: Test4" --body "This is a attach test message" --attach @/tmp/file.txt --attach @/tmp/file1.txt
swaks --to [email protected] --from [email protected] --server mail.mailabc.cn --auth LOGIN --auth-user "[email protected]" --auth-password "BiC9E7YjbdDI7TkH" --tls --header "Subject: HTML TEST" --attach-type text/html --attach-body "<html><body><h1>This is a html test</h1></body></html>"
下面给一个示例,将如下eml内容保存到文件test.eml。
Date: Thu, 17 Apr 2025 20:56:09 +0800
From: "demo" <[email protected]>
To: t1 <t1@163.com>
Subject: =?GB2312?B?suLK1GVtbLei0MU=?=
X-Priority: 3
X-Has-Attach: no
X-Mailer: Foxmail 7.2.25.375[cn]
Mime-Version: 1.0
Message-ID: <202504171256097486540@mailabc.cn>
Content-Type: multipart/alternative;
boundary="----=_001_NextPart683426000518_=----"
This is a multi-part message in MIME format.
------=_001_NextPart683426000518_=----
Content-Type: text/plain;
charset="GB2312"
Content-Transfer-Encoding: base64
1eLKx9K7t+JIVE1MuPHKvbLiytTTyrz+oaMNCg==
------=_001_NextPart683426000518_=----
Content-Type: text/html;
charset="GB2312"
Content-Transfer-Encoding: quoted-printable
<html><head><meta http-equiv=3D"content-type" content=3D"text/html; charse=
t=3DGB2312"><style>body { line-height: 1.5; }body { font-size: 14px; font-=
family: "Microsoft YaHei UI"; color: rgb(0, 0, 0); line-height: 1.5; }</st=
yle></head><body>=0A<div><font color=3D"#ff0000"><span id=3D"_FoxCURSOR"><=
/span>=D5=E2=CA=C7=D2=BB=B7=E2HTML=B8=F1=CA=BD=B2=E2=CA=D4=D3=CA=BC=FE=A1=
=A3</font></div>=0A</body></html>
------=_001_NextPart683426000518_=------
其中 --data "$(cat /tmp/test.eml)" 表示将/tmp/test.eml 作为DATA指令的内容发送。
swaks --to [email protected] --from [email protected] --server mail.mailabc.cn --auth LOGIN --auth-user "[email protected]" --auth-password "BiC9E7YjbdDI7TkH" --tls --data "$(cat /tmp/test.eml)"
最后,感兴趣的同学可以思考下面的问题:
1.邮件系统该如何拒绝这种MAIL FROM用户和信头中用户不一致的发信呢?
2.针对上述示例,启用SPF检查可以阻断这个发信吗?
如果想了解答案或进一步探讨该工具的用法,可以关注公众号留言。
1. Github项目主页:https://github.com/jetmore/swaks
2. 作者主页:https://jetmore.org/john
3. Swaks下载地址:https://jetmore.org/john/code/swaks/files/swaks-20240103.0.tar.gz
原文始发于微信公众号(mailabc):邮件SMTP测试利器Swaks
- 左青龙
- 微信扫一扫
-
- 右白虎
- 微信扫一扫
-
评论