Send an email via command line (Windows & Linux, using Telnet)

I recently had to send thousands of automatically generated emails. Granted that you have a mail server that is properly managed and won’t get blacklisted (so your mails don’t end up in a spambox on the way), you can automate the sending in a script doing the SMTP transaction procedurally.

The full telnet communication to send an email with a title and content is as follows:

Example sending an email from my professional email to my personal one.
Start by executing “telnet <your_SMTP_server> 25”, then:

EHLO lancaster.ac.uk
MAIL FROM:<p.ciholas@lancaster.ac.uk>
RCPT TO:<pierre@ciholas.fr>
DATA
Subject:Test Succeeded!

And now there is a title!
.
QUIT

Note that the 2 “enters” after the subject are here to respect the RFC 882.

Here is the full ouput for this conversation:

220 mh-0-0.lancs.ac.uk ESMTP Exim 4.88 Tue, 06 Jun 2017 02:23:37 +0100 (PBTX)
EHLO lancaster.ac.uk
250-mh-0-0.lancs.ac.uk Hello dyn-*-*.vpn.local [*.*.*.*]
250-SIZE 52428800
250-8BITMIME
250-PIPELINING
250-STARTTLS
250 HELP
MAIL FROM:<p.ciholas@lancaster.ac.uk>
250 OK
RCPT TO:<pierre@ciholas.fr>
250 Accepted
DATA
354 Please start mail input.
Subject:Test Succeeded!

And now there is a title!
.
250 Mail queued for delivery.
quit
221 Closing connection. Good bye.

Connection to host lost.

If you have an authentified SMTP you might want to have a look at the AUTH LOGIN command or other commands that might be useful to do that. I’m sure you know how to use Google and can find that easily.

Leave a Reply

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