AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 1049197
Accepted
Alex
Alex
Asked: 2021-01-10 06:58:36 +0800 CST2021-01-10 06:58:36 +0800 CST 2021-01-10 06:58:36 +0800 CST

如何测试 SMTP 服务器?

  • 772

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

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

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

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

linux smtp
  • 3 3 个回答
  • 5272 Views

3 个回答

  • Voted
  1. Best Answer
    Alex
    2021-01-10T06:58:36+08:002021-01-10T06:58:36+08:00

    该工具swaks在这里派上用场

    在 Ubuntu 上是

    apt install swaks
    

    然后您可以运行该命令,将看到 SMTP 对话框

    $ swaks --to [email protected] --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:<[email protected]>
    <** 530 Authentication required
     -> QUIT
    <-  221 kundenserver.de Service closing transmission channel
    === Connection closed with remote host.
    

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

    $ swaks --to [email protected] --server smtp.ionos.de:587 --auth LOGIN
    Username: foo
    Password: bar
    

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

    • 7
  2. user549144
    2021-01-10T07:06:19+08:002021-01-10T07:06:19+08:00

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

    这很简单,您只需 google SMTP 命令,就可以毫无问题地使用它们。当您回答了自己的问题后,您可以使用 SWAKS。这里有一些替代选项。


    这些是一些 SMTP 命令:

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

    HELO
    它是第一个 SMTP 命令:用于启动标识发送服务器的会话,通常后面跟它的域名。

    EHLO
    启动对话的替代命令,表明服务器正在使用扩展 SMTP 协议。

    MAIL FROM
    使用此 SMTP 命令开始操作:发件人在“发件人”字段中说明源电子邮件地址并实际开始电子邮件传输。

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

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

    DATA
    使用 DATA 命令开始传输电子邮件内容;一般后面跟一个服务器给出的354回复码,允许开始实际传输。

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

    TURN
    此命令用于转换客户端和服务器之间的角色,无需运行新的连接。

    AUTH
    使用 AUTH 命令,客户端向服务器验证自己的身份,并提供其用户名和密码。这是保证正确传输的另一层安全性。

    RSET
    它通知服务器正在进行的电子邮件传输将被终止,尽管 SMTP 对话不会关闭(如在 QUIT 的情况下)。

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

    帮助
    这是客户对成功传输电子邮件有用的一些信息的请求。

    QUIT
    它终止 SMTP 会话。


    OpenSSL、testssl.sh 和 GnuTLS

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

    openssl s_client -starttls smtp -connect mail.example.com:587
    

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

    ./testssl.sh -t smtp mail.example.com:25
    

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

    gnutls-cli mail.example.com -p 25
    

    远程登录

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

    telnet mail.example.com 25
    

    PHPMailer

    如果你使用 PHP,你可以使用PHPMailer:

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

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

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

    Python

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

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

    这是创建一个 SMTP 对象的简单语法,稍后可用于发送电子邮件 -

    import smtplib
    
    smtpObj = smtplib.SMTP( [host [, port [, local_hostname]]] )
    Here is the detail of the parameters −
    
    • host - 这是运行 SMTP 服务器的主机。您可以指定主机的 IP 地址或域名,例如 example.com。这是一个可选参数。

    • port - 如果您提供主机参数,那么您需要指定一个端口,SMTP 服务器正在侦听该端口。通常这个端口是 25。

    • local_hostname - 如果您的 SMTP 服务器在本地计算机上运行,​​那么您可以仅指定 localhost 选项。

    SMTP 对象有一个名为 的实例方法sendmail,它通常用于完成邮寄消息的工作。它需要三个参数 -

    • 发件人 - 带有发件人地址的字符串。

    • 接收者 - 字符串列表,每个接收者一个。

    • 消息 - 作为字符串的消息,格式为各种 RFC 中指定的格式。

    例子

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

    #!/usr/bin/python3
    
    import smtplib
    
    sender = '[email protected]'
    receivers = ['[email protected]']
    
    message = """From: From Person <[email protected]>
    To: To Person <[email protected]>
    Subject: SMTP e-mail test
    
    This is a test e-mail message.
    """
    
    try:
       smtpObj = smtplib.SMTP('localhost')
       smtpObj.sendmail(sender, receivers, message)         
       print "Successfully sent email"
    except SMTPException:
       print "Error: unable to send email"
    

    在这里,您在消息中放置了一个基本的电子邮件,使用三引号,注意正确格式化标题。电子邮件需要 From、To 和 Subject 标头,并用空行与电子邮件正文分开。

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

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

    mail = smtplib.SMTP('smtp.gmail.com', 587)
    

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

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

    例子

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

    #!/usr/bin/python3
    
    import smtplib
    
    message = """From: From Person <[email protected]>
    To: To Person <[email protected]>
    MIME-Version: 1.0
    Content-type: text/html
    Subject: SMTP HTML e-mail test
    
    This is an e-mail message to be sent in HTML format
    
    <b>This is HTML message.</b>
    <h1>This is headline.</h1>
    """
    
    try:
       smtpObj = smtplib.SMTP('localhost')
       smtpObj.sendmail(sender, receivers, message)         
       print "Successfully sent email"
    except SMTPException:
       print "Error: unable to send email"
    

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

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

    附件pack("m")在传输前应使用函数进行编码,使其具有 base 64 编码。

    示例 下面是一个示例,它将文件/tmp/test.txt作为附件发送。尝试一次 -

    #!/usr/bin/python3
    
    import smtplib
    import base64
    
    filename = "/tmp/test.txt"
    
    # Read a file and encode it into base64 format
    fo = open(filename, "rb")
    filecontent = fo.read()
    encodedcontent = base64.b64encode(filecontent)  # base64
    
    sender = '[email protected]'
    reciever = '[email protected]'
    
    marker = "AUNIQUEMARKER"
    
    body ="""
    This is a test email to send an attachement.
    """
    # Define the main headers.
    part1 = """From: From Person <[email protected]>
    To: To Person <[email protected]>
    Subject: Sending Attachement
    MIME-Version: 1.0
    Content-Type: multipart/mixed; boundary=%s
    --%s
    """ % (marker, marker)
    
    # Define the message action
    part2 = """Content-Type: text/plain
    Content-Transfer-Encoding:8bit
    
    %s
    --%s
    """ % (body,marker)
    
    # Define the attachment section
    part3 = """Content-Type: multipart/mixed; name=\"%s\"
    Content-Transfer-Encoding:base64
    Content-Disposition: attachment; filename=%s
    
    %s
    --%s--
    """ %(filename, filename, encodedcontent, marker)
    message = part1 + part2 + part3
    
    try:
       smtpObj = smtplib.SMTP('localhost')
       smtpObj.sendmail(sender, reciever, message)
       print "Successfully sent email"
    except Exception:
       print ("Error: unable to send email")
    

    斯瓦克斯:

    要安装它:

    • Ubuntu:sudo apt install swaks
    • CentOS: 首先: sudo yum install epel-release, 和sudo yum install swaksorsudo dnf install swaks
    • Arch Linux:sudo pacman -S swaks

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

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

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

    $ swaks --to [email protected] --server mail.example.com:587 --auth LOGIN
    Username: yourusername
    Password: yourpassword
    

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

    MX工具箱

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

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

    • 4
  3. Honey Singh
    2021-01-10T09:00:57+08:002021-01-10T09:00:57+08:00

    可以通过多种方式进行测试:

    1. 如前所述,使用 python 库。
    2. 使用 sendmail 客户端也可以测试 smpt 服务器。
    3. 您也可以设置 postfix 和 dovcot 来执行邮件操作。
    • 0

相关问题

  • 多操作系统环境的首选电子邮件客户端

  • 你最喜欢的 Linux 发行版是什么?[关闭]

  • 更改 PHP 的默认配置设置?

  • 保护新的 Ubuntu 服务器 [关闭]

  • (软)Ubuntu 7.10 上的 RAID 6,我应该迁移到 8.10 吗?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve