Index ¦ Archives ¦ Atom

Sending emails with telnet and OpenSSL

You can send an email using telnet it's very useful for debugging. It's something I've done several times before but because it's not something I do regularly I always forget the exact syntax so I've written this as a Note to self to remember the commands.

Finding the mail server to connect to

nslookup
> set q=mx
> gmail.com
Server:         8.8.8.8
Address:        8.8.8.8#53

Non-authoritative answer:
gmail.com       mail exchanger = 5 gmail-smtp-in.l.google.com.
gmail.com       mail exchanger = 40 alt4.gmail-smtp-in.l.google.com.
gmail.com       mail exchanger = 30 alt3.gmail-smtp-in.l.google.com.
gmail.com       mail exchanger = 10 alt1.gmail-smtp-in.l.google.com.
gmail.com       mail exchanger = 20 alt2.gmail-smtp-in.l.google.com.

Sending email with Telnet

$ telnet gmail-smtp-in.l.google.com 25

HELO my-reverse-dns-address.example.com

MAIL FROM: Michael@my-reverse-dns-address.example.com
RCPT TO: abuse@gmail.com

DATA
Subject: Test Email

Lorem ipsum dolor sit amet, consectetur adipiscing elit.
molestie, fringilla turpis id, ultrices tortor,
ac consectetur massa imperdiet ac. Fusce ac porta orci.

.

if auth is required we can use base64 to work out the username and password or if base64 is not available (e.g. on windows) we can do it in python

import base64
username = base64.b64encode("michael@example.com")
password = base64.b64encode("open-sesame")

Once we have the username and password we can use them

$ telnet smtp.example.com 25

EHLO my-reverse-dns-address.example.com

AUTH LOGIN

bWljaGFlbEBleGFtcGxlLmNvbQ==

b3Blbi1zZXNhbWU=

MAIL FROM: my-reverse-dns-address.example.com
RCPT TO: michael@example.com

DATA
Subject: Test Email - Authenticated as michael@example.com

Lorem ipsum dolor sit amet, consectetur adipiscing elit.
molestie, fringilla turpis id, ultrices tortor,
ac consectetur massa imperdiet ac. Fusce ac porta orci.

.

We can use OpenSSL to send emails using the STARTSSL command (i.e. start a standard SMTP connection then upgrade to TLS)

$ openssl s_client -starttls smtp -crlf -connect smtp.example.com:25

EHLO my-reverse-dns-address.example.com

AUTH LOGIN

bWljaGFlbEBleGFtcGxlLmNvbQ==

b3Blbi1zZXNhbWU=

MAIL FROM: my-reverse-dns-address.example.com
RCPT TO: Michael@example.com

DATA
Subject: Test Email - Authenticated as michael@example.com

Lorem ipsum dolor sit amet, consectetur adipiscing elit.
molestie, fringilla turpis id, ultrices tortor,
ac consectetur massa imperdiet ac. Fusce ac porta orci.

.

Or We can use OpenSSL to send emails using SMTPS (i.e. start a TLS connection then do SMTP over it)

$ openssl s_client -connect smtps.example.com:465

EHLO my-reverse-dns-address.example.com

AUTH LOGIN

bWljaGFlbEBleGFtcGxlLmNvbQ==

b3Blbi1zZXNhbWU=

MAIL FROM: my-reverse-dns-address.example.com
RCPT TO: Michael@example.com

DATA
Subject: Test Email - Authenticated as michael@example.com

Lorem ipsum dolor sit amet, consectetur adipiscing elit.
molestie, fringilla turpis id, ultrices tortor,
ac consectetur massa imperdiet ac. Fusce ac porta orci.

.

Personally I prefer the idea of sending mail over SMTPS (port 465). I know it's not officially in the IANA spec, but it just makes a lot more sense to me to start an encrypted connection and then send email over it, than to start an unencrypted connection and then upgrade it to an encrypted one.

Creative Commons License
Content on this site is licensed under a Creative Commons Attribution 4.0 International License.
Built using Pelican. Based on a theme by Giulio Fidente on github.