How to send email using simple SMTP commands via Gmail?

前端 未结 4 733
暖寄归人
暖寄归人 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:47

    Based on the existing answers, here's a step-by-step guide to sending automated e-mails over SMTP, using a GMail account, from the command line, without disclosing the password.

    Requirements

    First, install the following software packages:

    • Expect
    • OpenSSL
    • Core Utils (base64)

    These instructions assume a Linux operating system, but should be reasonably easy to port to Windows (via Cygwin or native equivalents), or other operating system.

    Authentication

    Save the following shell script as authentication.sh:

    #!/bin/bash
    
    # Asks for a username and password, then spits out the encoded value for
    # use with authentication against SMTP servers.
    
    echo -n "Email (shown): "
    read email
    echo -n "Password (hidden): "
    read -s password
    echo
    
    TEXT="\0$email\0$password"
    
    echo -ne $TEXT | base64
    

    Make it executable and run it as follows:

    chmod +x authentication.sh
    ./authentication.sh
    

    When prompted, provide your e-mail address and password. This will look something like:

    Email (shown): bob@gmail.com
    Password (hidden): 
    AGJvYkBnbWFpbC5jb20AYm9iaXN0aGViZXN0cGVyc29uZXZlcg==
    

    Copy the last line (AGJ...==), as this will be used for authentication.

    Notification

    Save the following expect script as notify.sh (note the first line refers to the expect program):

    #!/usr/bin/expect
    
    set address "[lindex $argv 0]"
    set subject "[lindex $argv 1]"
    set ts_date "[lindex $argv 2]"
    set ts_time "[lindex $argv 3]"
    
    set timeout 10
    spawn openssl s_client -connect smtp.gmail.com:465 -crlf -ign_eof 
    
    expect "220" {
      send "EHLO localhost\n"
    
      expect "250" {
        send "AUTH PLAIN YOUR_AUTHENTICATION_CODE\n"
    
        expect "235" {
          send "MAIL FROM: \n"
    
          expect "250" {
            send "RCPT TO: <$address>\n"
    
            expect "250" {
              send "DATA\n"
    
              expect "354" {
                send "Subject: $subject\n\n"
                send "Email sent on $ts_date at $ts_time.\n"
                send "\n.\n"
    
                expect "250" {
                    send "quit\n"
                }
              }
            }
          }
        }
      }
    }
    

    Make the following changes:

    1. Paste over YOUR_AUTHENTICATION_CODE with the authentication code generated by the authentication script.
    2. Change YOUR_EMAIL_ADDRESS with the e-mail address used to generate the authentication code.
    3. Save the file.

    For example (note the angle brackets are retained for the e-mail address):

    send "AUTH PLAIN AGJvYkBnbWFpbC5jb20AYm9iaXN0aGViZXN0cGVyc29uZXZlcg==\n"
    send "MAIL FROM: \n"
    

    Lastly, make the notify script executable as follows:

    chmod +x notify.sh
    

    Send E-mail

    Send an e-mail from the command line as follows:

    ./notify.sh recipient@domain.com "Command Line" "March 14" "15:52"
    

提交回复
热议问题