How Do You Automate a telnet Session to Send a Postfix Email on a Linux Server?

Problem scenario
You want to create a script that will send an email from a Linux server. You want it to use telnet. How do you automatically send an email using telnet?

Solution
Prerequisite
Set up Postfix. If you are running SUSE, see this posting. If you are running a RedHat derivative, see this posting.

Procedures
Use this script, but change the email addresses before you run it. source@from.com is the email that you want the email to appear to be from. destination@goingto.com is the email address that you are sending to.

#!/bin/sh

HOST=$(hostname -f)' 25'
CMD1='mail from: source@from.com'
CMD2='rcpt to: destination@goingto.com'
CMD3='DATA'
CMD4='basic text here'
CMD5="."
CMD6="quit"
(
echo open "$HOST"
sleep 2
echo "$CMD1"
echo "$CMD2"
echo "$CMD3"
echo "$CMD4"
echo "$CMD5"
echo "$CMD6"
sleep 2
echo "exit"
) | telnet

An Alternative Way To Send Email
Another way to do this is with Python; to learn how to use Python and not install anything else, see this external posting.

Leave a comment

Your email address will not be published. Required fields are marked *