-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend_mail
106 lines (102 loc) · 2.02 KB
/
send_mail
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/bin/expect
#
# sends a properly formatted file to smtp server
# usage: send_email
#
# blatantly copied from Peter Vibert’s expect script:
# http://www.petervibert.com/posts/01-11-09-expect-smtp.html
# source: http://pfautsch.com/?p=484
#
if {$argc<3} {
send_user "sends mime formatted message file to smtp server using telnet\n"
send_user "usage: send_email mailserver port message_file\n"
exit
}
set mailserver [lrange $argv 0 0]
set portno [lrange $argv 1 1]
set cfile [lrange $argv 2 2]
send "$cfile\n"
set fp [open "$cfile" r]
set content [read $fp]
set hostname [exec hostname]
# extract the from address from message file
# must be in one of two forms:
# From: "Recipient Name"
# or
# From: [email protected]
set from [exec grep "From:" "$cfile"]
set quoted [string match "?*<*@*.*>" "$from"]
if [expr $quoted > 0 ] {
set from [exec echo "$from" | cut -d< -f2 | tr -d '>‘]
} else {
set from [exec echo "$from" | cut -d: -f2 | tr -d \[:space:\]]
}
# extract the to address – same as from (see above)
set to [exec grep "To:" "$cfile"]
set quoted [string match "?*<*@*.*>" "$to"]
if [expr $quoted > 0 ] {
set to [exec echo "$to" | cut -d< -f2 | tr -d '>‘]
} else {
set to [exec echo "$to" | cut -d: -f2 | tr -d \[:space:\]]
}
spawn telnet $mailserver $portno
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 $portno timeout\n"
exit
}
send "HELO $hostname\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
}
log_user 0
send "$content"
set timeout 1
expect "$content"
close $fp
send ".\r"
expect ".\r"
expect "2?? *" {
} "5?? *" {
exit
} "4?? *" {
exit
}
send_user "$expect_out(buffer)"
send "QUIT\r"
exit