如何测试 SMTP 服务器?

Ale*_*lex 7 linux smtp

我收到了 SMTP 服务器信息和凭据,想测试它们是否有效。

如何使用命令行在 Linux 上轻松测试 SMTP 连接?

我知道我可以通过 telnet / openssl 来做到这一点,但这看起来很复杂。

那么如何查看SMTP服务器呢?

Ale*_*lex 16

该工具swaks在这里派上用场

在 Ubuntu 上是

apt install swaks
Run Code Online (Sandbox Code Playgroud)

然后您可以运行命令并会看到 SMTP 对话框

$ swaks --to foo@example.com --server smtp.ionos.de:587
=== Trying smtp.ionos.de:587...
=== Connected to smtp.ionos.de.
<-  220 kundenserver.de (mreue106) Nemesis ESMTP Service ready
 -> EHLO lafto
<-  250-kundenserver.de Hello example [<IP redacted>]
<-  250-8BITMIME
<-  250-AUTH LOGIN PLAIN
<-  250-SIZE 140000000
<-  250 STARTTLS
 -> MAIL FROM:<foo@example.com>
<** 530 Authentication required
 -> QUIT
<-  221 kundenserver.de Service closing transmission channel
=== Connection closed with remote host.
Run Code Online (Sandbox Code Playgroud)

正如您在这里看到的,它需要身份验证,这就是我们重新运行的原因

$ swaks --to foo@example.com --server smtp.ionos.de:587 --auth LOGIN
Username: foo
Password: bar
Run Code Online (Sandbox Code Playgroud)

查看手册页以获取更多信息


小智 5

\n

我知道我可以通过 telnet / openssl 做到这一点,但这看起来很复杂

\n
\n

这非常简单,您只需谷歌SMTP Commands即可,并且可以毫无问题地使用它们。由于您已经回答了自己的问题,因此您可以使用 SWAKS。这里有一些替代选项。

\n
\n

以下是一些 SMTP 命令:

\n

每个命令都用于通过 SMTP 协议在两个服务器之间的正常通信序列中,以传送电子邮件。

\n

HELO
\nIt\xe2\x80\x99s 第一个 SMTP 命令:启动识别发件人服务器的会话,通常后跟其域名。

\n

埃洛
\n启动对话的替代命令,其基础是服务器正在使用扩展 SMTP 协议。

\n

MAIL FROM
\n使用此 SMTP 命令,操作开始:发件人在 \xe2\x80\x9cFrom\xe2\x80\x9d 字段中注明源电子邮件地址,并实际开始电子邮件传输。

\n

RCPT TO
\n它标识电子邮件的收件人;如果有多个,则该命令只是逐个地址地重复。

\n

SIZE
\n此 SMTP 命令通知远程服务器有关附加电子邮件的估计大小(以字节为单位)。它还可用于报告服务器接受的消息的最大大小。

\n

DATA
\n使用 DATA 命令,电子邮件内容开始传输;it\xe2\x80\x99s 通常后面跟着服务器给出的 354 回复代码,授予开始实际传输的权限。

\n

VRFY
\n服务器被要求验证特定的电子邮件地址或用户名是否确实存在。

\n

TURN
\n此命令用于反转客户端和服务器之间的角色,而不需要运行新的连接。

\n

AUTH
\n使用 AUTH 命令,客户端向服务器验证自己的身份,并给出其用户名和密码。它\xe2\x80\x99是保证正确传输的另一层安全性。

\n

RSET
\n它通知服务器正在进行的电子邮件传输将被终止,尽管 SMTP 会话不会\xe2\x80\x99 被关闭(就像 QUIT 的情况一样)。

\n

EXPN
\n此 SMTP 命令要求确认邮件列表的标识。

\n

帮助
\n客户端\xe2\x80\x99s 请求一些对成功传输电子邮件有用的信息。

\n

QUIT
\n它终止 SMTP 对话。

\n
\n

OpenSSL、testssl.sh 和 GnuTLS

\n

您可以openssl s_client通过运行如下命令来使用 ,

\n
openssl s_client -starttls smtp -connect mail.example.com:587\n
Run Code Online (Sandbox Code Playgroud)\n

您还可以使用名为testssl.sh的工具在您的SMTP服务器上测试SSL/TLS,显然即使它是本地托管的。下载后,将其解压并进入testssl.sh文件夹并运行:

\n
./testssl.sh -t smtp mail.example.com:25\n
Run Code Online (Sandbox Code Playgroud)\n

GnuTLS如果安装了它,您也可以使用:

\n
gnutls-cli mail.example.com -p 25\n
Run Code Online (Sandbox Code Playgroud)\n

远程登录

\n

如果您的 SMTP 服务器没有 SSL/TLS,您可以使用telnet. Telnet 是最基本的工具,但它不支持 SSL/TLS。

\n
telnet mail.example.com 25\n
Run Code Online (Sandbox Code Playgroud)\n

PHP邮件程序

\n

如果您使用 PHP,则可以使用PHPMailer

\n
<?php\n// Import PHPMailer classes into the global namespace\n// These must be at the top of your script, not inside a function\nuse PHPMailer\\PHPMailer\\PHPMailer;\nuse PHPMailer\\PHPMailer\\SMTP;\nuse PHPMailer\\PHPMailer\\Exception;\n\n// Load Composer\'s autoloader\nrequire \'vendor/autoload.php\';\n\n// Instantiation and passing `true` enables exceptions\n$mail = new PHPMailer(true);\n\ntry {\n    //Server settings\n    $mail->SMTPDebug = SMTP::DEBUG_SERVER;                      // Enable verbose debug output\n    $mail->isSMTP();                                            // Send using SMTP\n    $mail->Host       = \'smtp.example.com\';                    // Set the SMTP server to send through\n    $mail->SMTPAuth   = true;                                   // Enable SMTP authentication\n    $mail->Username   = \'user@example.com\';                     // SMTP username\n    $mail->Password   = \'secret\';                               // SMTP password\n    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;         // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged\n    $mail->Port       = 587;                                    // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above\n\n    //Recipients\n    $mail->setFrom(\'from@example.com\', \'Mailer\');\n    $mail->addAddress(\'joe@example.net\', \'Joe User\');     // Add a recipient\n    $mail->addAddress(\'ellen@example.com\');               // Name is optional\n    $mail->addReplyTo(\'info@example.com\', \'Information\');\n    $mail->addCC(\'cc@example.com\');\n    $mail->addBCC(\'bcc@example.com\');\n\n    // Attachments\n    $mail->addAttachment(\'/var/tmp/file.tar.gz\');         // Add attachments\n    $mail->addAttachment(\'/tmp/image.jpg\', \'new.jpg\');    // Optional name\n\n    // Content\n    $mail->isHTML(true);                                  // Set email format to HTML\n    $mail->Subject = \'Here is the subject\';\n    $mail->Body    = \'This is the HTML message body <b>in bold!</b>\';\n    $mail->AltBody = \'This is the body in plain text for non-HTML mail clients\';\n\n    $mail->send();\n    echo \'Message has been sent\';\n} catch (Exception $e) {\n    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";\n}\n
Run Code Online (Sandbox Code Playgroud)\n

尽管这不是您问题的答案。您甚至可以在 PHPMailer 中轻松设置 DKIM:

\n
<?php\n\n/**\n * This example shows sending a DKIM-signed message with PHPMailer.\n * More info about DKIM can be found here: http://www.dkim.org/info/dkim-faq.html\n * There\'s more to using DKIM than just this code - check out this article:\n * @see https://yomotherboard.com/how-to-setup-email-server-dkim-keys/\n * See also the DKIM_gen_keys example code in the examples folder,\n * which shows how to make a key pair from PHP.\n */\n\n//Import the PHPMailer class into the global namespace\nuse PHPMailer\\PHPMailer\\PHPMailer;\n\nrequire \'../vendor/autoload.php\';\n\n//Usual setup\n$mail = new PHPMailer();\n$mail->setFrom(\'from@example.com\', \'First Last\');\n$mail->addAddress(\'whoto@example.com\', \'John Doe\');\n$mail->Subject = \'PHPMailer mail() test\';\n$mail->msgHTML(file_get_contents(\'contents.html\'), __DIR__);\n\n//This should be the same as the domain of your From address\n$mail->DKIM_domain = \'example.com\';\n//See the DKIM_gen_keys.phps script for making a key pair -\n//here we assume you\'ve already done that.\n//Path to your private key:\n$mail->DKIM_private = \'dkim_private.pem\';\n//Set this to your own selector\n$mail->DKIM_selector = \'phpmailer\';\n//Put your private key\'s passphrase in here if it has one\n$mail->DKIM_passphrase = \'\';\n//The identity you\'re signing as - usually your From address\n$mail->DKIM_identity = $mail->From;\n//Suppress listing signed header fields in signature, defaults to true for debugging purpose\n$mail->DKIM_copyHeaderFields = false;\n//Optionally you can add extra headers for signing to meet special requirements\n$mail->DKIM_extraHeaders = [\'List-Unsubscribe\', \'List-Help\'];\n\n//When you send, the DKIM settings will be used to sign the message\nif (!$mail->send()) {\n    echo \'Mailer Error: \' . $mail->ErrorInfo;\n} else {\n    echo \'Message sent!\';\n}\n\n
Run Code Online (Sandbox Code Playgroud)\n

Python

\n
\n

(取自https://www.tutorialspoint.com/python3/python_sending_email.htm,因为我不想提供链接,所以我只是将整个内容发布在这里,因为该页面可能随时出现 404 错误。)

\n
\n

Python提供了smtplib一个模块,该模块定义了一个 SMTP 客户端会话对象,该对象可用于将邮件发送到具有 SMTP 或 ESMTP 侦听器守护程序的任何 Internet 计算机。

\n

下面是创建一个 SMTP 对象的简单语法,该对象稍后可用于发送电子邮件 \xe2\x88\x92

\n
import smtplib\n\nsmtpObj = smtplib.SMTP( [host [, port [, local_hostname]]] )\nHere is the detail of the parameters \xe2\x88\x92\n
Run Code Online (Sandbox Code Playgroud)\n
    \n
  • host \xe2\x88\x92 这是运行 SMTP 服务器的主机。您可以指定主机的 IP 地址或域名(例如 example.com)。这是一个可选参数。

    \n
  • \n
  • port \xe2\x88\x92 如果您提供主机参数,则需要指定 SMTP 服务器侦听的端口。通常该端口为 25。

    \n
  • \n
  • local_hostname \xe2\x88\x92 如果您的 SMTP 服务器在本地计算机上运行,​​则您可以仅指定 localhost 该选项。

    \n
  • \n
\n

SMTP 对象有一个名为 的实例方法sendmail,该方法通常用于执行邮寄消息的工作。它需要三个参数\xe2\x88\x92

\n
    \n
  • 发件人 \xe2\x88\x92 包含发件人地址的字符串。

    \n
  • \n
  • 接收者 \xe2\x88\x92 字符串列表,每个接收者一个。

    \n
  • \n
  • 消息 \xe2\x88\x92 消息是按照各种 RFC 中指定的格式设置的字符串。

    \n
  • \n
\n

例子

\n

这是使用 Python 脚本发送一封电子邮件的简单方法。尝试一次\xe2\x88\x92

\n
#!/usr/bin/python3\n\nimport smtplib\n\nsender = \'from@fromdomain.com\'\nreceivers = [\'to@todomain.com\']\n\nmessage = """From: From Person <from@fromdomain.com>\nTo: To Person <to@todomain.com>\nSubject: SMTP e-mail test\n\nThis is a test e-mail message.\n"""\n\ntry:\n   smtpObj = smtplib.SMTP(\'localhost\')\n   smtpObj.sendmail(sender, receivers, message)         \n   print "Successfully sent email"\nexcept SMTPException:\n   print "Error: unable to send email"\n
Run Code Online (Sandbox Code Playgroud)\n

在这里,您在消息中放置了一封基本电子邮件,使用三重引号,并注意正确设置标题格式。电子邮件需要“发件人”、“收件人”和“主题”标头,并用空行与电子邮件正文分隔。

\n

要发送邮件,您可以使用 smtpObj 连接到本地计算机上的 SMTP 服务器。然后使用 sendmail 方法以及消息、发件人地址和目标地址作为参数(即使发件人和收件人地址位于电子邮件本身内,这些并不总是用于路由邮件)。

\n

如果您的本地计算机上没有运行 SMTP 服务器,则可以使用 smtplib 客户端与远程 SMTP 服务器进行通信。除非您使用网络邮件服务(例如 gmail 或 Yahoo! Mail),否则您的电子邮件提供商必须向您提供您可以提供的外发邮件服务器详细信息,如下所示 \xe2\x88\x92

\n
mail = smtplib.SMTP(\'smtp.gmail.com\', 587)\n
Run Code Online (Sandbox Code Playgroud)\n

使用 Python 发送 HTML 电子邮件\n当您使用 Python 发送文本消息时,所有内容都将被视为简单文本。即使您在文本消息中包含 HTML 标签,它也会显示为简单文本,并且 HTML 标签不会根据 HTML 语法进行格式化。但是,Python 提供了将 HTML 消息作为实际 HTML 消息发送的选项。

\n

发送电子邮件时,您可以指定 Mime 版本、内容类型和字符集以发送 HTML 电子邮件。

\n

例子

\n

以下是将 HTML 内容作为电子邮件发送的示例。尝试一次\xe2\x88\x92

\n
#!/usr/bin/python3\n\nimport smtplib\n\nmessage = """From: From Person <from@fromdomain.com>\nTo: To Person <to@todomain.com>\nMIME-Version: 1.0\nContent-type: text/html\nSubject: SMTP HTML e-mail test\n\nThis is an e-mail message to be sent in HTML format\n\n<b>This is HTML message.</b>\n<h1>This is headline.</h1>\n"""\n\ntry:\n   smtpObj = smtplib.SMTP(\'localhost\')\n   smtpObj.sendmail(sender, receivers, message)         \n   print "Successfully sent email"\nexcept SMTPException:\n   print "Error: unable to send email"\n
Run Code Online (Sandbox Code Playgroud)\n

以电子邮件形式发送附件\n要发送包含混合内容的电子邮件,需要将内容类型标头设置为多部分/混合。然后,可以在边界内指定文本和附件部分。

\n

边界以两个连字符开始,后跟一个唯一的数字,该数字不能出现在电子邮件的消息部分中。表示电子邮件最后部分的最终边界也必须以两个连字符结束。

\n

附件文件pack("m")在传输前应使用 Base 64 编码功能进行编码。

\n

示例\n以下是一个示例,它将文件/tmp/test.txt作为附件发送。尝试一次\xe2\x88\x92

\n
#!/usr/bin/python3\n\nimport smtplib\nimport base64\n\nfilename = "/tmp/test.txt"\n\n# Read a file and encode it into base64 format\nfo = open(filename, "rb")\nfilecontent = fo.read()\nencodedcontent = base64.b64encode(filecontent)  # base64\n\nsender = \'me@fromdomain.net\'\nreciever = \'admin@example.com\'\n\nmarker = "AUNIQUEMARKER"\n\nbody ="""\nThis is a test email to send an attachement.\n"""\n# Define the main headers.\npart1 = """From: From Person <me@fromdomain.net>\nTo: To Person <admin@example.com>\nSubject: Sending Attachement\nMIME-Version: 1.0\nContent-Type: multipart/mixed; boundary=%s\n--%s\n""" % (marker, marker)\n\n# Define the message action\npart2 = """Content-Type: text/plain\nContent-Transfer-Encoding:8bit\n\n%s\n--%s\n""" % (body,marker)\n\n# Define the attachment section\npart3 = """Content-Type: multipart/mixed; name=\\"%s\\"\nContent-Transfer-Encoding:base64\nContent-Disposition: attachment; filename=%s\n\n%s\n--%s--\n""" %(filename, filename, encodedcontent, marker)\nmessage = part1 + part2 + part3\n\ntry:\n   smtpObj = smtplib.SMTP(\'localhost\')\n   smtpObj.sendmail(sender, reciever, message)\n   print "Successfully sent email"\nexcept Exception:\n   print ("Error: unable to send email")\n
Run Code Online (Sandbox Code Playgroud)\n
\n

摇摆:

\n

要安装它:

\n
    \n
  • 乌班图:sudo apt install swaks
  • \n
  • CentOS:首先:sudo yum install epel-release、和sudo yum install swakssudo dnf install swaks
  • \n
  • 拱门Linux:sudo pacman -S swaks
  • \n
\n

然后您可以运行命令并会看到 SMTP 对话框:

\n
$ swaks --to foo@example.com --server smtp.example.com:587\n=== Trying smtp.example.com:587...\n=== Connected to smtp.example.com.\n<-  220 example.com (something) Foo ESMTP Service ready\n -> EHLO somenamehere\n<-  250-example.com Hello example [<IP redacted>]\n<-  250-8BITMIME\n<-  250-AUTH LOGIN PLAIN\n<-  250-SIZE 140000000\n<-  250 STARTTLS\n -> MAIL FROM:<foo@example.com>\n<** 530 Authentication required\n -> QUIT\n<-  221 example.com Service closing transmission channel\n=== Connection closed with remote host.\n
Run Code Online (Sandbox Code Playgroud)\n

正如您在这里看到的,它需要身份验证,这就是我们重新运行的原因

\n
$ swaks --to someone@example.com --server mail.example.com:587 --auth LOGIN\nUsername: yourusername\nPassword: yourpassword\n
Run Code Online (Sandbox Code Playgroud)\n

您还可以使用 AUTH PLAIN --auth PLAIN,具体取决于服务器支持的方法。\n使用 来查看手册页以获取更多信息man swaks

\n

MX工具箱

\n

您可以使用 MXToolBox 的电子邮件服务器测试进行一些有时可能有用的测试,但您无法指定您想要用它做什么。所以,你最好使用上面的东西。

\n

或者,只需使用mail命令...

\n