Creating a fake SMTP service with Tcl

Sometimes it's useful to have an SMTP service that doesn't actually send email.

That's easy to do using Tcl.

Here's the script to use:

package require smtpd 
 
catch [wm withdraw .] 
catch [console show] 
 
proc deliver {sender recipients data} { 
    set mail "To: $recipients\nFrom: $sender\nDate: [clock format [clock seconds]]" 
    append mail "\n" [join $data "\n"] 
 
    puts $mail 
} 
 
::smtpd::configure -deliver ::deliver 
 
::smtpd::start 

Here's what sending email from the command line looks like:

C:\>telnet localhost 25

220 VM-ANAKIN-DEV03 tcllib smtpd 1.4.0; Fri, 11 Sep 2009 14:45:36 -0700

helo this.is.a.test

250 VM-ANAKIN-DEV03 Hello this.is.a.test [127.0.0.1], pleased to meet you

mail from:<someone@example.com>

250 OK

rcpt to:<recipient@example.com>

250 OK

data

354 Enter mail, end with "." on a line by itself

X-Some-Heaer: some value

This is the body of my email message.

.

250 yXpG8IaY4jiI Message accepted for delivery

quit

221 VM-ANAKIN-DEV03 Service closing transmission channel

Connection to host lost.

C:\>

And here's the output in the Tcl console:

To: <recipient@example.com>

From: <someone@example.com>

Date: Fri Sep 11 2:46:24 PM Pacific Daylight Time 2009

Return-Path: <someone@example.com>

Received: from this.is.a.test [127.0.0.1]

    by VM-ANAKIN-DEV03 with tcllib smtpd (1.4.0)

    id yXpG8IaY4jiI; Fri, 11 Sep 2009 14:46:01 -0700

X-Some-Heaer: some value

This is the body of my email message.


Feedback

No comments posted yet.


Post a comment





 

Please add 4 and 5 and type the answer here: