74 lines
2.8 KiB
Raw Permalink Normal View History

2022-11-27 07:42:28 -05:00
==== SSH session that runs sendmail instead of an interactive shell ====
=== What? ===
A wrapper for your local mail transfer agent (MTA) that initiates an SSH
session when your pubnix email address appears in the "From:" header.
=== Why? ===
You want to send email from your pubnix address without running the risk
that the receiving server will reject it due to a mismatch between the
"From" header and your ISP's IP block, but ...
* the low volume of your outgoing mail cannot justify the cost of an
upgraded membership, or
* your Internet connection has too high a latency to be running a
full-screen editor within an SSH session.
=== Where? ===
a *BSD, Linux, or WSL environment with a client that generates
MIME-formatted mail (as expected by sendmail-compatible MTAs such as
=== How? ===
- Set up [[ssh public key authentication|SSH public key authentication]],
if you have not already done so. This step can be omitted if you
prefer to type your password for each outgoing mail.
- Save the wrapper script below somewhere in your $PATH. Change the
value of $MTA to the mail transport agent that you would otherwise be
using for outgoing mail, the value of $PATH if any of the commands live in
non-standard locations, and of course all the hard-coded email addresses
in the script.
- Configure your mail user agent (MUA) to call the wrapper script,
rather than calling your MTA directly.
#!/usr/bin/env bash
# msmtp.wrap - bypass local in favor of remote sendmail,
# if certain headers are found
draft="$(mktemp -t sendmail.XXXXXX)"
cat /dev/stdin > "$draft"
sender="$(grep '^From:' "$draft" | head -n 1 | cut -d: -f2)"
case "$sender" in
sendmail="ssh $Account0 sendmail -t" ;;
sendmail="ssh $Account1 sendmail -t" ;;
sendmail="$MTA $*" ;;
< "$draft" $sendmail; status=$?
rm -f "$draft"; exit $status
=== What Next? ===
The possibilities for customization of this wrapper are only limited by
your imagination. You can parse the outgoing messages to look for
particular recipients, subject lines, etc., and define a custom $sendmail
command to handle each case. On the pubnix side, the arguments following
''ssh $Account0'' will be passed to your usual login shell after the
''-c'' option(( see the sshd(8) man-page, [[ ]] )),
so you will enjoy all the environment variables defined in
''$HOME/.bash_login'' if you use bash as your login shell. Save your
custom scripts in a directory that your pubnix login shell considers part of
$PATH, and you can then perform all kinds of tasks (like updating your
website) without leaving your mail client.