SMTP

From miki
Revision as of 13:05, 10 January 2014 by Mip (talk | contribs) (→‎Using mail)
Jump to navigation Jump to search

Testing SMTP

Tools

Same as for testing IMAP.

Test scripts

Simple test script found by Googling...

> telnet server 25

220 srv1.dpetri.net Microsoft ESMTP MAIL Service, Version: 5.0.2195.5329 ready at Sun, 15
Sep 2002 23:51:06 +0200
helo srv1.dpetri.net
250 srv1.dpetri.net Hello [192.168.0.100]
mail from: admin@petri.co.il
250 2.1.0 admin@petri.co.il....Sender OK
rcpt to: danielp@dpetri.net
250 2.1.5 danielp@dpetri.net
data
354 Start mail input; end with <CRLF>.<CRLF>
subject: this is a test
Hi Daniel
I'm trying to test this connection from Telnet.
Let me know if you get this message.

.
250 2.6.0 <SRV1zNQZO0KheDSZeTd00000002@srv1.dpetri.net> Queued mail for delivery
quit
221 2.0.0 srv1.dpetri.net Service closing transmission channel

Using mail

Install either package:

sudo apt-get install mailutils                  # ...OR...
sudo apt-get install heirloom-mailx

Then test smtp:

mail localuser                                   # Send e-mail to localuser (on local machine)
mail -r sender localuser@localhost               # Send e-mail with given sender

AUTH PLAIN test script

See [1] (more information at [2])

First we need to compute the authentication string for the PLAIN authentication (this requires perl with MIME64 module installed):

# Don't forget to escape any '@' in the perl string or it shall be interpret as an array variable!!!
perl -MMIME::Base64 -e 'print encode_base64("\000jms1\@jms1.net\000not.my.real.password")' 
# AGptczFAam1zMS5uZXQAbm90Lm15LnJlYWwucGFzc3dvcmQ=

Example of script:

nc smtp.server.org 587
220 smtp.server.org ESMTP Postfix
ehlo griffin.hell
250-smtp.priorweb.be
250-PIPELINING
250-SIZE
250-VRFY
250-ETRN
250-STARTTLS
250-AUTH PLAIN LOGIN
{{{1}}}
250-ENHANCEDSTATUSCODES
250 8BITMIME
AUTH PLAIN AGptczFAam1zMS5uZXQAbm90Lm15LnJlYWwucGFzc3dvcmQ=
235 2.7.0 Authentication successful
mail from: jms1@jms1.net
250 2.1.0 Ok
rcpt to: jms1@jms1.net
250 2.1.5 Ok
data
354 End data with <CR><LF>.<CR><LF>
from: Jimmy <jms1@jms1.net>
to: Jimmy <jms1@jms1.net>
subject: test

test
.
250 ok 1311459496 qp 15928
quit
221 2.0.0 Bye

Note: EHLO is the more advanced helo command, that also lists the features supported by the server.

Expect script for testing SMTP

From [3]. Usage:

expect mail.exp smtp.server.org from@address.org to@address.org

The script (mail.exp):

#!/usr/bin/expect
set mailserver [lrange $argv 0 0]
set from [lrange $argv 1 1]
set to [lrange $argv 2 2]

spawn telnet $mailserver 25
expect "failed" {
                send_user "$mailserver: connect failed\n"
                exit    
        } "2?? *" {
        } "4?? *"   {
                exit
        } "refused" {
                send_user "$mailserver: connect refused\n"
                exit
        } "closed" {
                send_user "$mailserver: connect closed\n"
                exit
        } timeout {
                send_user "$mailserver: connect to port 25 timeout\n"
                exit
        }
send "HELO foo.com\r"
expect "2?? *" {
} "5?? *" {
        exit
} "4?? *" {
        exit
}
send "MAIL FROM: <$from>\r"
expect "2?? *"  {
} "5?? *" {
        exit
} "4?? *" {
        exit
}
send "RCPT TO: <$to>\r"
expect "2?? *" {
} "5?? *" {
        exit
} "4?? *" {
        exit
}
send "DATA\r"
expect "3?? *" {
} "5?? *" {
        exit
} "4?? *" {
        exit
}
send "From: $from\r"
send "To: $to\r"
send "Subject: test\r"
send "This is a test message\r"
send ".\r"
expect "2?? *" {
} "5?? *" {
        exit
} "4?? *" {
        exit
}
send "QUIT\r"
exit

Bash script for testing SMTP

This uses the TCP/UDP devices available in Bash:

#! /bin/bash
# 
# Usage: smtp.sh mail.server.org from@address.org to@address.org

MAILSERVER=$1
FROM=$2
TO=$3

exec 5<>/dev/tcp/${MAILSERVER}/25
cat <&5 &
sleep 1
echo -e "HELO dummy.domain" >&5
sleep 1
echo -e "MAIL FROM: $FROM" >&5
sleep 1
echo -e "RCPT TO: $TO" >&5
sleep 1
echo -e "DATA" >&5
sleep 1
echo -e "subject: test bash" >&5
echo -e "" >&5
echo -e "test bash" >&5
echo -e "." >&5
sleep 1
echo -e "QUIT" >&5
sleep 1