Skip to content

Instantly share code, notes, and snippets.

@RafaelKr
Last active May 25, 2021 19:55
Show Gist options
  • Save RafaelKr/37fe66535bce3d3ce15ff5bc416e9956 to your computer and use it in GitHub Desktop.
Save RafaelKr/37fe66535bce3d3ce15ff5bc416e9956 to your computer and use it in GitHub Desktop.
Can quickly test email submission via SMTP encrypted with STARTTLS. Tested with gmx.net, probably works with some other providers.

Description

Script to quickly test email submission via SMTP encrypted with STARTTLS.

The SMTP server (Host), sender (From) and receiver (To) are specified as command line arguments, the password is requested after running the script.
The script then runs the openssl program and sends the commands required to send an email.

Note: Only works with STARTTLS (default port 587), not SSL (default port 465). Tested with gmx.net, probably works with some other providers, too.

Install

Make sure you have openssl and expect installed.
The minimum required version for base64 support in expect is 8.6

Ubuntu based Linux:

sudo apt update
sudo apt install openssl expect

Usage

  1. Download check-smtp.tcl
  2. Make it executable
  3. Run it with ./check-smtp.tcl <HOST:PORT> <FROM> <TO>
# Download
curl -O https://gist.githubusercontent.com/RafaelKr/37fe66535bce3d3ce15ff5bc416e9956/raw/02449b1a36dafccba73a1a339e341cd483fdfeef/check-smtp.tcl

# Make it executable
chmod +x check-smtp.tcl

# Use it!
./check-smtp.tcl mail.gmx.net:587 sender-address@gmx.net receiver-address@example.org
#!/usr/bin/expect
set timeout 60
set host [lindex $argv 0]
set user [lindex $argv 1]
set receiver [lindex $argv 2]
# request password from user on stdin
send_user "SMTP Password: "
expect_user -re "(.*)\n"
set password $expect_out(1,string)
# encode user and password for plain authentication
set encoded_auth [binary encode base64 "\000$user\000$password"]
set timeout 60
# send email!
spawn openssl s_client -starttls smtp -ign_eof -crlf -connect $host
expect "250 STARTTLS"
send "EHLO $user\n"
expect -re "250-AUTH .*PLAIN.*\n"
send "AUTH PLAIN $encoded_auth\n"
expect -re "235 .*\n"
send "MAIL FROM:$user\n"
expect -re "250 .*\n"
send "RCPT TO:$receiver\n"
expect -re "250 .*\n"
send "DATA\n"
expect -re "354 .*\n"
send "To: $receiver\n"
send "From: $user\n"
send "Subject: Test-Mail\n"
send "\n"
send "test\n.\n"
expect -re "250 .*\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment