Knowledge Base

Preserving for the future: Shell scripts, AoC, and more

smtp1.sh (Source)

#!/bin/sh
# File: smtp1.sh
# Location: stackbin
# Author: bgstack15
# Startdate: 2024-08-05-2 14:05
# SPDX-License-Identifier: GPL-3.0-only
# Title: Send authenticated email with openssl s_client
# Purpose: demo cli smtp auth
# History:
# References:
#    https://stackoverflow.com/questions/1546367/how-to-send-mail-with-to-cc-and-bcc
#    https://szclsya.me/posts/net/send-email-with-netcat/
#    https://serverfault.com/questions/1101104/how-to-send-an-email-with-openssl-and-microsoft-exchange-online
#    https://woshub.com/sending-email-via-telnet-using-smtp-authentication/
#    https://learn.microsoft.com/en-us/exchange/mail-flow/test-smtp-telnet?view=exchserver-2019
#    https://stackoverflow.com/questions/14640560/openssl-to-negotiate-ssl-encryption-for-starttls
#    https://thelinuxcode.com/openssl-s-client/
#    https://www.stevenrombauts.be/2018/12/test-smtp-with-telnet-or-openssl/
#    https://stackoverflow.com/questions/44250054/send-email-with-netcat
# Improve:
# Dependencies:
#    dep-fedora: openssl, coreutils
#    an smtp account and server
# Documentation:
slowcat() {
   while read REPLY ; do sleep .05; echo "$REPLY"; done
}
{
   message1="$( printf '%s' 'exampleuser@example.com' | base64 )"
   message2="$( cat ~/.config/smtp1 )"
   printf '%s\n' "EHLO exampleaddress.com"
   printf '%s\n' "AUTH LOGIN"
   printf '%s\n' "${message1}"
   printf '%s\n' "${message2}"
   # Everybody, so TO, CC, BCC is a RCPT TO. The To, CC, BCC headers are the decorations visible to the mail client.
   cat <<-EOF
MAIL FROM:<exampleuser@example.com>
RCPT TO:<user2@local.example.com>
RCPT TO:<user3@anotherlocal.examplelong.com>
DATA
From: [marco polo] <exampleaddress.com>
To: <user3@anotherlocal.examplelong.com>
BCC: <user2@local.example.com>
Date: Mon, 5 Aug 2024 17:31:32 +0000
Subject: Hello from netcat
sample message here
.
QUIT
EOF
# | slowcat | nc -v mail.example.net 587
} | slowcat | openssl s_client -crlf -connect mail.example.net:587 -starttls smtp -ign_eof