SMTP, testing via Telnet
When troubleshooting problems with SMTP service - your own, or others - it is frequently very helpful to be able to "speak" to the SMTP server directly, rather than going through a mail client which won't necessarily tell you exactly what the SMTP server is saying. You can easily do this with the telnet client.
Note that many ISPs do not allow outbound connections on port 25 to any SMTP server but their own - if you get timeouts when trying to connect to port 25, you should try port 587, which is the standard ESMTP port. (Port 587 connections normally require SMTP AUTH, which is covered below.)
Testing an SMTP service via telnet
ph34r# telnet localhost 25 Trying 127.0.0.1... Connected to localhost.localdomain. Escape character is '^]'. 220 ESMTP HELO justtesting 250 MAIL FROM: email@example.com 250 ok RCPT TO: firstname.lastname@example.org 250 ok DATA 354 go ahead To: email@example.com From: firstname.lastname@example.org Subject: this is a test message Date: Thu, 21 Jun 2007 11:11:40 -0400 Just testing SMTP functionality by telnetting in to port 25. I'll end this message now by entering in a line with nothing but a period in it and hitting return. . 250 ok 1103093638 qp 87827 QUIT 221 Connection closed by foreign host.
Okay - our SMTP server just accepted a telnet connection, responded like a mailserver, and accepted a nice little test email for delivery. (Any response other than a 250 ok would represent an error of one sort or another.)
Testing SMTP AUTH via telnet
# apt-get install metamail
Now we need to generate base64-encoded strings to use with the PLAIN and LOGIN methods.
# printf 'email@example.com' | mimencode dXNlckBkb21haW4uY29t # printf 'password' | mimencode cGFzc3dvcmQ=
Those two are for the LOGIN method. The next one is for the PLAIN method. If you're impatient to just see if something works, this will gripe you less since it's only a single copy and paste. =)
# printf '\firstname.lastname@example.org\0password' | mimencode AHVzZXJAZG9tYWluLmNvbQBwYXNzd29yZA==
OK, let's telnet in:
# telnet localhost 25 Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. 220 mail.server.local ESMTP Postfix (Ubuntu)
Great, we got a banner. OK, now let's tell it we want to use extended SMTP with the "ehlo" command:
ehlo test 250-mail.server.local 250-PIPELINING 250-SIZE 10240000 250-VRFY 250-ETRN 250-STARTTLS 250-AUTH DIGEST-MD5 PLAIN LOGIN 250-AUTH=DIGEST-MD5 PLAIN LOGIN 250-ENHANCEDSTATUSCODES 250-8BITMIME 250 DSN
Alright. Good. Notice that we support three AUTH methods: DIGEST-MD5, PLAIN, and LOGIN. Let's try PLAIN first, using the string we generated for it above:
AUTH PLAIN AHVzZXJAZG9tYWluLmNvbQBwYXNzd29yZA== 235 2.0.0 Authentication successful quit 221 2.0.0 Bye
Excellent! If we want to try the LOGIN method, telnet back in and ehlo just as we did before, then:
AUTH LOGIN 334 VXNlcm5hbWU6 dXNlckBkb21haW4uY29t 334 UGFzc3dvcmQ6 cGFzc3dvcmQ= 235 2.0.0 Authentication successful quit 221 2.0.0 Bye
Again, excellent. (If you were curious, the 334 and 235 SMTP messages are also Base64 encoded, and decode to "Username:" and "Password:" when run through mimencode -u.)