1 ! Copyright (C) 2007, 2008 Elie CHAFTARI, Dirk Vleugels,
2 ! Slava Pestov, Doug Coleman, Daniel Ehrenberg.
3 ! See http://factorcode.org/license.txt for BSD license.
4 USING: arrays namespaces make io io.encodings.string io.encodings.utf8
5 io.encodings.iana io.timeouts io.sockets io.sockets.secure
6 io.encodings.ascii kernel logging sequences combinators splitting
7 assocs strings math.order math.parser random system calendar summary
8 calendar.format accessors sets hashtables base64 debugger classes
9 prettyprint io.crlf words ;
15 "localhost" 25 <inet> smtp-server set-global
19 SYMBOL: smtp-read-timeout
20 1 minutes smtp-read-timeout set-global
24 TUPLE: plain-auth username password ;
25 C: <plain-auth> plain-auth
28 no-auth smtp-auth set-global
30 LOG: log-smtp-connection NOTICE ( addrspec -- )
32 : with-smtp-connection ( quot -- )
34 dup log-smtp-connection
36 smtp-domain [ host-name or ] change
37 smtp-read-timeout get timeouts
39 ] with-client ; inline
47 { content-type string initial: "text/plain" }
48 { encoding word initial: utf8 }
51 : <email> ( -- email ) email new ; inline
55 : command ( string -- ) write crlf flush ;
57 \ command DEBUG add-input-logging
59 : helo ( -- ) "EHLO " host-name append command ;
61 : start-tls ( -- ) "STARTTLS" command ;
63 ERROR: bad-email-address email ;
65 : validate-address ( string -- string' )
66 #! Make sure we send funky stuff to the server by accident.
67 dup "\r\n>" intersects?
68 [ bad-email-address ] when ;
70 : mail-from ( fromaddr -- )
72 "MAIL FROM:<" ">" surround command ;
76 "RCPT TO:<" ">" surround command ;
81 ERROR: message-contains-dot message ;
83 M: message-contains-dot summary ( obj -- string )
84 drop "Message cannot contain . on a line by itself" ;
86 : validate-message ( msg -- msg' )
88 [ message-contains-dot ] when ;
90 : send-body ( email -- )
91 [ body>> ] [ encoding>> ] bi encode
92 >base64-lines write crlf
98 LOG: smtp-response DEBUG
100 : multiline? ( response -- ? )
101 3 swap ?nth CHAR: - = ;
103 : (receive-response) ( -- )
107 [ multiline? [ (receive-response) ] when ]
110 TUPLE: response code messages ;
112 : <response> ( lines -- response )
113 [ first 3 head string>number ] keep response boa ;
115 : receive-response ( -- response )
116 [ (receive-response) ] { } make <response> ;
118 ERROR: smtp-error response ;
121 "SMTP error (" write dup class pprint ")" print
122 response>> messages>> [ print ] each ;
124 ERROR: smtp-server-busy < smtp-error ;
125 ERROR: smtp-syntax-error < smtp-error ;
126 ERROR: smtp-command-not-implemented < smtp-error ;
127 ERROR: smtp-bad-authentication < smtp-error ;
128 ERROR: smtp-mailbox-unavailable < smtp-error ;
129 ERROR: smtp-user-not-local < smtp-error ;
130 ERROR: smtp-exceeded-storage-allocation < smtp-error ;
131 ERROR: smtp-bad-mailbox-name < smtp-error ;
132 ERROR: smtp-transaction-failed < smtp-error ;
134 : check-response ( response -- )
136 { [ dup { 220 235 250 221 354 } member? ] [ 2drop ] }
137 { [ dup 400 499 between? ] [ drop smtp-server-busy ] }
138 { [ dup 500 = ] [ drop smtp-syntax-error ] }
139 { [ dup 501 = ] [ drop smtp-command-not-implemented ] }
140 { [ dup 500 509 between? ] [ drop smtp-syntax-error ] }
141 { [ dup 530 539 between? ] [ drop smtp-bad-authentication ] }
142 { [ dup 550 = ] [ drop smtp-mailbox-unavailable ] }
143 { [ dup 551 = ] [ drop smtp-user-not-local ] }
144 { [ dup 552 = ] [ drop smtp-exceeded-storage-allocation ] }
145 { [ dup 553 = ] [ drop smtp-bad-mailbox-name ] }
146 { [ dup 554 = ] [ drop smtp-transaction-failed ] }
150 : get-ok ( -- ) receive-response check-response ;
152 GENERIC: send-auth ( auth -- )
154 M: no-auth send-auth drop ;
156 : plain-auth-string ( username password -- string )
157 [ "\0" prepend ] bi@ append utf8 encode >base64 ;
159 M: plain-auth send-auth
160 [ username>> ] [ password>> ] bi plain-auth-string
161 "AUTH PLAIN " prepend command get-ok ;
163 : auth ( -- ) smtp-auth get send-auth ;
165 : encode-header ( string -- string' )
168 "=?utf-8?B?" "?=" surround
171 ERROR: invalid-header-string string ;
173 : validate-header ( string -- string' )
174 dup "\r\n" intersects?
175 [ invalid-header-string ] when ;
177 : write-header ( key value -- )
178 [ validate-header write ]
179 [ ": " write validate-header encode-header write ] bi* crlf ;
181 : write-headers ( assoc -- )
182 [ write-header ] assoc-each ;
184 : message-id ( -- string )
191 smtp-domain get [ host-name ] unless* %
195 : extract-email ( recepient -- email )
196 ! This could be much smarter.
197 " " split1-last swap or "<" ?head drop ">" ?tail drop ;
199 : email-content-type ( email -- content-type )
200 [ content-type>> ] [ encoding>> encoding>name ] bi "; charset=" glue ;
202 : email>headers ( email -- assoc )
204 now timestamp>rfc822 "Date" set
205 message-id "Message-Id" set
206 "1.0" "MIME-Version" set
207 "base64" "Content-Transfer-Encoding" set
209 [ from>> "From" set ]
210 [ to>> ", " join "To" set ]
211 [ cc>> ", " join [ "Cc" set ] unless-empty ]
212 [ subject>> "Subject" set ]
213 [ email-content-type "Content-Type" set ]
217 : (send-email) ( headers email -- )
221 smtp-tls? get [ start-tls get-ok send-secure-handshake ] when
223 dup from>> extract-email mail-from get-ok
224 dup to>> [ extract-email rcpt-to get-ok ] each
225 dup cc>> [ extract-email rcpt-to get-ok ] each
226 dup bcc>> [ extract-email rcpt-to get-ok ] each
232 ] with-smtp-connection ;
236 : send-email ( email -- )
237 [ email>headers ] keep (send-email) ;