How to send email using simple SMTP commands via Gmail?

前端 未结 4 732
暖寄归人
暖寄归人 2020-11-28 01:21

For educational purposes, I need to send an email through an SMTP server, using SMTP\'s fundamental and simple rules.

I was able to do that using smtp4dev. I

4条回答
  •  遥遥无期
    2020-11-28 01:30

    As no one has mentioned - I would suggest to use great tool for such purpose - swaks

    # yum info swaks
    Installed Packages
    Name        : swaks
    Arch        : noarch
    Version     : 20130209.0
    Release     : 3.el6
    Size        : 287 k
    Repo        : installed
    From repo   : epel
    Summary     : Command-line SMTP transaction tester
    URL         : http://www.jetmore.org/john/code/swaks
    License     : GPLv2+
    Description : Swiss Army Knife SMTP: A command line SMTP tester. Swaks can test
                : various aspects of your SMTP server, including TLS and AUTH.
    

    It has a lot of options and can do almost everything you want.

    GMAIL: STARTTLS, SSLv3 (and yes, in 2016 gmail still support sslv3)

    $ echo "Hello world" | swaks -4 --server smtp.gmail.com:587 --from user@gmail.com --to user@example.net -tls --tls-protocol sslv3 --auth PLAIN --auth-user user@gmail.com --auth-password 7654321 --h-Subject "Test message" --body -
    === Trying smtp.gmail.com:587...
    === Connected to smtp.gmail.com.
    <-  220 smtp.gmail.com ESMTP h8sm76342lbd.48 - gsmtp
     -> EHLO www.example.net
    <-  250-smtp.gmail.com at your service, [193.243.156.26]
    <-  250-SIZE 35882577
    <-  250-8BITMIME
    <-  250-STARTTLS
    <-  250-ENHANCEDSTATUSCODES
    <-  250-PIPELINING
    <-  250-CHUNKING
    <-  250 SMTPUTF8
     -> STARTTLS
    <-  220 2.0.0 Ready to start TLS
    === TLS started with cipher SSLv3:RC4-SHA:128
    === TLS no local certificate set
    === TLS peer DN="/C=US/ST=California/L=Mountain View/O=Google Inc/CN=smtp.gmail.com"
     ~> EHLO www.example.net
    <~  250-smtp.gmail.com at your service, [193.243.156.26]
    <~  250-SIZE 35882577
    <~  250-8BITMIME
    <~  250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH
    <~  250-ENHANCEDSTATUSCODES
    <~  250-PIPELINING
    <~  250-CHUNKING
    <~  250 SMTPUTF8
     ~> AUTH PLAIN AGFhQxsZXguaGhMGdATGV4X2hoYtYWlsLmNvbQBS9TU1MjQ=
    <~  235 2.7.0 Accepted
     ~> MAIL FROM:
    <~  250 2.1.0 OK h8sm76342lbd.48 - gsmtp
     ~> RCPT TO:
    <~  250 2.1.5 OK h8sm76342lbd.48 - gsmtp
     ~> DATA
    <~  354  Go ahead h8sm76342lbd.48 - gsmtp
     ~> Date: Wed, 17 Feb 2016 09:49:03 +0000
     ~> To: user@example.net
     ~> From: user@gmail.com
     ~> Subject: Test message
     ~> X-Mailer: swaks v20130209.0 jetmore.org/john/code/swaks/
     ~>
     ~> Hello world
     ~>
     ~>
     ~> .
    <~  250 2.0.0 OK 1455702544 h8sm76342lbd.48 - gsmtp
     ~> QUIT
    <~  221 2.0.0 closing connection h8sm76342lbd.48 - gsmtp
    === Connection closed with remote host.
    

    YAHOO: TLS aka SMTPS, tlsv1.2

    $ echo "Hello world" | swaks -4 --server smtp.mail.yahoo.com:465 --from user@yahoo.com --to user@gmail.com --tlsc --tls-protocol tlsv1_2 --auth PLAIN --auth-user user@yahoo.com --auth-password 7654321 --h-Subject "Test message" --body -
    === Trying smtp.mail.yahoo.com:465...
    === Connected to smtp.mail.yahoo.com.
    === TLS started with cipher TLSv1.2:ECDHE-RSA-AES128-GCM-SHA256:128
    === TLS no local certificate set
    === TLS peer DN="/C=US/ST=California/L=Sunnyvale/O=Yahoo Inc./OU=Information Technology/CN=smtp.mail.yahoo.com"
    <~  220 smtp.mail.yahoo.com ESMTP ready
     ~> EHLO www.example.net
    <~  250-smtp.mail.yahoo.com
    <~  250-PIPELINING
    <~  250-SIZE 41697280
    <~  250-8 BITMIME
    <~  250 AUTH PLAIN LOGIN XOAUTH2 XYMCOOKIE
     ~> AUTH PLAIN AGFhQxsZXguaGhMGdATGV4X2hoYtYWlsLmNvbQBS9TU1MjQ=
    <~  235 2.0.0 OK
     ~> MAIL FROM:
    <~  250 OK , completed
     ~> RCPT TO:
    <~  250 OK , completed
     ~> DATA
    <~  354 Start Mail. End with CRLF.CRLF
     ~> Date: Wed, 17 Feb 2016 10:08:28 +0000
     ~> To: user@gmail.com
     ~> From: user@yahoo.com
     ~> Subject: Test message
     ~> X-Mailer: swaks v20130209.0 jetmore.org/john/code/swaks/
     ~>
     ~> Hello world
     ~>
     ~>
     ~> .
    <~  250 OK , completed
     ~> QUIT
    <~  221 Service Closing transmission
    === Connection closed with remote host.
    

    I have been using swaks to send email notifications from nagios via gmail for last 5 years without any problem.

提交回复
热议问题