Script that email

To my own future reference, and to whom it might prove useful, I post a little Tcl/expect script, used to (rudimentarily) automate email sending. Usage shouldn’t be too hard to figure out🙂

#! /usr/bin/expect

if { $argc < 2 } {
        puts "Usage: ./$argv0 MTA_IP_addr MTA_port \[rcpt\]"
        exit
}
spawn telnet [lindex $argv 0] [lindex $argv 1]
set rcpt "oscar@foo.tld"
if { $argc == 3 } {
        set rcpt [lindex $argv 2]
}
expect "220"
send "ehlo a\n"
expect "250 DSN"
send "mail from: <>\n"
expect "250 2.1.0 Ok"
send "rcpt to: <$rcpt>\n"
expect {
        "250 2.1.5 Ok" {
                send "data\n"
                expect "354 End data with <CR><LF>.<CR><LF>"
                set ts [exec "/bin/date" +%s]
                send "Subject: foo $ts\n"
                send "bar\n"
                send ".\n"
                expect "250 2.0.0 Ok"
        }                                                                                                                                    
        "5**" {
                puts "Rcpt addr rejected!\n"
                return -1
        }
}
send "quit"

EDIT: modified the script to also receive from command line the recipient address

Os comentários estão fechados.