Skip to content

Instantly share code, notes, and snippets.

@taikedz
Last active April 22, 2019 17:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save taikedz/d887da50fee7abda7c102f4d48d02b37 to your computer and use it in GitHub Desktop.
Save taikedz/d887da50fee7abda7c102f4d48d02b37 to your computer and use it in GitHub Desktop.
Mail testing tool

Mail server testing

A quick script and data file that allows a test of a non-secured mail server

Typically useful to check if a service is actually working, you'll need to activate non-login non-SSL on your server

For obvious reasons, do NOT leave your SMTP server in that state for too long on the open Internet!

Example use of command:

./testmail.sh mail-session.txt mail.company.tld
# vim: set filetype=conf :
# Comment lines are ignored
# Empty lines are part of the session
#
# Use comments for spacing
#
#
# Send EHLO to initiate session
EHLO sender.company.tld
# From address - any will do
MAIL FROM: test@company.tld
# Please set this to your own address...!
RCPT TO: me@company.tld
# Declare that we're going to send the actual mail data
# the above was routing info, of sorts
DATA
# Anything you want
From: noreply@company.tld
# This can be anything too! The routing was set above!
To: you@company.tld
Subject: Test mail
# Empty line to signal start of message
# Start of message
Hello
This is a test message
# Single dot to end message
.
# Actually close the session
QUIT
#!/usr/bin/env bash
set -euo pipefail
TELNET=(:)
die() {
echo "$*" >&2
exit 1
}
printhelp() {
cat <<EOHELP
"$0" MAILFILE SERVER [PORT]
Use MAILFILE as a SMTP session
Sending to SERVER
Through PORT (default 25)
EOHELP
if [[ -n "$*" ]]; then exit "$1"; fi
}
getbin() {
local cmd
for cmd in "$@"; do
if which "$cmd" 2>&1 >/dev/null; then
echo "$cmd"
return
fi
done
}
set_telnet() {
local command="$(getbin telnet busybox)"
if [[ "$command" = telnet ]]; then
TELNET=(: telnet)
elif [[ "$command" = busybox ]]; then
TELNET=(: busybox telnet)
else
die "No telnet client available"
fi
}
main() {
local mailfile="${1:-}"; shift || printhelp 1
local server="${1:-}"; shift || printhelp 1
local port="${1:-}"; shift || port=25
set_telnet
[[ "$port" =~ ^[0-9]+ ]] || die "Port '$port' is not a number"
set -x
grep -Pv '^\s*#' "$mailfile" | tee /dev/stderr | "${TELNET[@]:1}" "$server" "$port"
}
main "$@"
@taikedz
Copy link
Author

taikedz commented Apr 22, 2019

Had a problem with telnet this time, so here's an alternative (needs bbrun from https://gitlab.com/taikedz/bash-builder)

#!/usr/bin/env bbrun

set -euo pipefail

#%include std/out.sh
#%include std/syntax-extensions.sh

SEC=0.5

$%function readsession(sessionfile) {
    out:info "Print every $SEC seconds"

    while read line; do
        echo "$line"|tee -a /dev/stderr

        sleep $SEC
    done < <(grep -Pv '^#' "$mailfile")
}

$%function main(mailrelay sessionfile) {
    out:info "Server: $mailrelay"
    out:info "Session: $sessionfile"
    nc -C "$mailrelay" 25  < <(readsession "$sessionfile")| tee output.smtp

    local expected="Queued mail for delivery"

    (grep -q "$expected" output.smtp && out:info "Success.") || out:fail "Did not find the expected message: '$expected'"
}

main "$@"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment