• Sales: 1-800-961-2888
  • Support: 1-800-961-4454

Postfix - Using Telnet to Test Postfix


So far, we have sent test emails using the command line.

However, there is no real feedback from these clients as to how our mail server responded - did it respond with the correct details? Note that it is possible to send/receive mail with details that are not 100% correct - leading to issues down the line.

Let's look at telnet to check the server responses to mail requests.

Contents

Telnet basics

Telnet can act as the most basic mail client. It can do the same thing as the bigger clients you use on your workstation (such as Thunderbird, Outlook, Mail, etc) - it sends mail.

However, instead of being wrapped in a nice GUI it is a command line tool that can be used to diagnose our postfix installation (and to send mail!).

This is a primer for telnet. I won't be going into great detail of the why's and the how's of every aspect of telnet and the responses we (should) receive.

Setting up mail servers can be a complicated issue and there are some incredible large and detailed tomes and manuals available for those who need to get into the minute detail.

However, following the articles will allow you to understand the basics and know what to look for should you want more detail.

Email details

When you send an email you can use the analogy of physically posting a letter:

You greet the postman. You hand over the envelope which should have the address (recipient) and the sender's address - inside the envelope will be the actual letter, or the data.

Going with that analogy, we need a way of authenticating or confirming all those details.

Greeting

We'll start of the sequence by connecting to the mail server and greeting the postman.

On the Cloud Server enter:

telnet mail.democloud.com 25

This starts the telnet session on port 25.

You should receive a 220 response:

Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
220 mail.democloud.com ESMTP Postfix (Ubuntu)

Good. (Note: If you cannot telnet to port 25, try modifying your master.cf postfix file to allow port 587 communication, as well as opening 587 in iptables. Some internet providers block users from accessing port 25.)

Next we can greet the postman:

HELO test.democloud.com

Note the test subdomain doesn't exist - it is simply a way of greeting the mail server.

The mail server should respond with a confirmation of who it is:

250 mail.democloud.com

Envelope

Now we have greeted the postman and got the correct responses we can start to fill out the envelope for our letter.

We start by letting the server know who the sender is:

MAIL FROM:<jpierce@democloud.com>

If the mail server accepts the sender address, you will get another '250' output:

250 2.1.0 Ok

Good.

Now we can sort out to whom we are sending the letter:

RCPT TO:<demo@democloud.com>

We are hoping for a 250 response:

250 2.1.5 Ok

Again, good news.

You can add more recipients - unlike a physical letter, the same email can be sent to multiple recipients.

Data

Now we come to the 'meat' of the letter: the data.

To start the main body of the message:

DATA

The response is fairly sparse:

354 End data with <CR><LF>.<CR><LF>

Now you need to enter the subject and the body.

Note you need to physically type the word 'Subject:'

Subject: test message

Press enter/return and then you can type the body of the message:

This is the body of the message!

As when we used the 'mail' command in a previous article, we need to enter a single period (.) to indicate we have finished the body of the message.

As before, we are rather hoping for a 250 response:

.
250 2.0.0 Ok: queued as 9620FF0087

Nice.

Quit

Well, we're done with telnet for the moment so we can quit:

QUIT

This will dump you back at the bash prompt:

221 2.0.0 Bye
Connection closed by foreign host.
You have new mail in /var/mail/demo

Ooh!

I have new mail in /var/mail/demo.

Check test message

Let's use the 'tail' command to read the end of the mail file:

tail -n 15 /var/mail/demo

That particular command parses the last 15 lines of the /var/mail/demo file (which is where our mail is kept).

If you don't see all of the mail as shown below, you can adjust the number of lines to, say, 20.

In my case, the output was as follows:

From paul@democloud.com  Wed Aug  6 10:33:20 2008
Return-Path: <paul@democloud.com>
X-Original-To: demo@democloud.com
Delivered-To: demo@democloud.com
Received: from test.democloud.com (localhost [127.0.0.1])
        by mail.democloud.com (Postfix) with SMTP id 9620FF0087
        for <demo@democloud.com>; Wed,  6 Aug 2008 10:28:43 +0000 (UTC)
Subject: test message
Message-Id: <20080806103059.9620FF0087@mail.democloud.com>
Date: Wed,  6 Aug 2008 10:28:43 +0000 (UTC)
From: paul@democloud.com
To: undisclosed-recipients:;
 
This is the body of the message!

Well that's pretty cool.

Summary

It goes without saying (even though I am going to say it) that there is far, far more to it than the basics shown above.

It is simply impossible to go into all the details of what is possible with email headers and messages, encodings and the details of telnet.

However, this introduction should show how mail works and what steps are taken when mail is sent and received.

Perhaps you can begin to see why some legitimate mail may be seen as spam email - if any of the steps above failed or gave the wrong response then warning flags are raised.

A wrong hostname will mean mail sent from the Cloud Server does not identify itself properly. Improperly formatted addresses or message bodies can raise eyebrows and flags.

Now that we've tested that mail is working properly, let's make sure that we're not an open relay.

Previous Article
Next Article
Table of Contents



© 2011-2013 Rackspace US, Inc.

Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License


See license specifics and DISCLAIMER

6 Comments

"(Note: If you cannot telnet to port 25, try modifying your master.cf postfix file to allow port 587 communication, as well as opening 587 in iptables. Some internet providers block users from accessing port 25.)"

Is the article for rackspace users or for someone else? :-) Having said that the command
telnet mail.mydomain.com 25 gives the following error
Trying 174.132.240.146...
telnet: Unable to connect to remote host: Connection refused
My /etc/hosts file does contain the line 99.99.99.99 sas-studious.net (my ip number followed by the domain name)

The reference to port 25 possibly being blocked by ISPs is there because a lot of users will be walking through the steps from a home desktop machine. ISPs sometimes block traffic to port 25 so it's harder to use their service to send spam through open relays.

A "connection refused" error means that either no service is listening to port 25 on that machine, or iptables is blocking it and refusing the connection. The latter is unlikely since iptables is normally set up to deny connections rather than refuse them, which means the connection would timeout instead of being refused.

You should be able to rule out iptables and your ISP by installing telnet on your server and trying to telnet to port 25 from there. If you get the same result, then postfix isn't listening for connections on port 25.

My domain is registered with another (provider 1) and I am trying to setup the mail in rackspace. But IMHO this kind of setup needs to be addressed in the article. So let me explain my DNS entries. The A records point to the ip of the VPS hosted on rackspace. The CNAME has the following entries
mx1.sas-studious.net mx1.provider1.com
mx2.sas-studious.net mx2.provider1.com

However the MX records contain the following
10 sas-studious.net mx1.sas-studious.net
20 sas-studious.net mx2.sas-studious.net

The MX entries, I guess, has been set while I went through the steps in your article to set up the DNS in rackspace as part of setting up Postfix, and must have got replicated in all the other DNS'

Thanks for posting that, Sridhar. This article is primarily about testing a postfix installation rather than about setting up mail for a domain, which is why it doesn't really cover that situation specifically. The intent is to assist users who've set up their own postfix installation to relay from another machine, which isn't something you'd want to do if you have another service handling your domain's email. Indeed, I'd recommend going that route whenever possible, since mail servers can be a pain to set up and maintain.

I didn't mean that this very article should contain the procedure for setting up the mail for a domain. What I intended to convey was that the thread of articles on setting up the mail services should address it. Hope I have explained myself. Apologies for the misleading comment.

Aha! Sorry for the misunderstanding. We could definitely use a broader range of mail server articles. What you describe sounds like it could have been taken care of with a very simple postfix install instead of the more complex series that has you set up local mail users you're unlikely to use, since you have a third-party service. The "Setting up a mail relay" article might apply a bit to this, but I'll make a note to put something more general together. Thanks for pointing out the omission.

Add new comment