1 ! Copyright (C) 2007, 2009 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 io.encodings.string
5 io.encodings.utf8 io.encodings.iana io.encodings.binary
6 io.encodings.ascii io.timeouts io.sockets io.sockets.secure io.crlf
7 kernel logging sequences combinators splitting assocs strings
8 math.order math.parser random system calendar summary calendar.format
9 accessors sets hashtables base64 debugger classes prettyprint 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
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 : send-body ( email -- )
83 [ body>> ] [ encoding>> ] bi encode >base64-lines write
84 ascii encode-output crlf
90 LOG: smtp-response DEBUG
92 : multiline? ( response -- ? )
93 3 swap ?nth CHAR: - = ;
95 : (receive-response) ( -- )
99 [ multiline? [ (receive-response) ] when ]
102 TUPLE: response code messages ;
104 : <response> ( lines -- response )
105 [ first 3 head string>number ] keep response boa ;
107 : receive-response ( -- response )
108 [ (receive-response) ] { } make <response> ;
110 ERROR: smtp-error response ;
113 "SMTP error (" write dup class-of pprint ")" print
114 response>> messages>> [ print ] each ;
116 ERROR: smtp-server-busy < smtp-error ;
117 ERROR: smtp-syntax-error < smtp-error ;
118 ERROR: smtp-command-not-implemented < smtp-error ;
119 ERROR: smtp-bad-authentication < smtp-error ;
120 ERROR: smtp-mailbox-unavailable < smtp-error ;
121 ERROR: smtp-user-not-local < smtp-error ;
122 ERROR: smtp-exceeded-storage-allocation < smtp-error ;
123 ERROR: smtp-bad-mailbox-name < smtp-error ;
124 ERROR: smtp-transaction-failed < smtp-error ;
126 : check-response ( response -- )
128 { [ dup { 220 235 250 221 354 } member? ] [ 2drop ] }
129 { [ dup 400 499 between? ] [ drop smtp-server-busy ] }
130 { [ dup 500 = ] [ drop smtp-syntax-error ] }
131 { [ dup 501 = ] [ drop smtp-command-not-implemented ] }
132 { [ dup 500 509 between? ] [ drop smtp-syntax-error ] }
133 { [ dup 530 539 between? ] [ drop smtp-bad-authentication ] }
134 { [ dup 550 = ] [ drop smtp-mailbox-unavailable ] }
135 { [ dup 551 = ] [ drop smtp-user-not-local ] }
136 { [ dup 552 = ] [ drop smtp-exceeded-storage-allocation ] }
137 { [ dup 553 = ] [ drop smtp-bad-mailbox-name ] }
138 { [ dup 554 = ] [ drop smtp-transaction-failed ] }
142 : get-ok ( -- ) receive-response check-response ;
144 GENERIC: send-auth ( auth -- )
146 M: no-auth send-auth drop ;
148 : plain-auth-string ( username password -- string )
149 [ "\0" prepend ] bi@ append utf8 encode >base64 ;
151 M: plain-auth send-auth
152 [ username>> ] [ password>> ] bi plain-auth-string
153 "AUTH PLAIN " prepend command get-ok ;
155 : auth ( -- ) smtp-auth get send-auth ;
157 : encode-header ( string -- string' )
160 "=?utf-8?B?" "?=" surround
163 ERROR: invalid-header-string string ;
165 : validate-header ( string -- string' )
166 dup "\r\n" intersects?
167 [ invalid-header-string ] when ;
169 : write-header ( key value -- )
170 [ validate-header write ]
171 [ ": " write validate-header encode-header write ] bi* crlf ;
173 : write-headers ( assoc -- )
174 [ write-header ] assoc-each ;
176 : message-id ( -- string )
181 gmt timestamp>micros #
183 smtp-domain get [ host-name ] unless* %
187 : extract-email ( recepient -- email )
188 ! This could be much smarter.
189 " " split1-last swap or "<" ?head drop ">" ?tail drop ;
191 : email-content-type ( email -- content-type )
192 [ content-type>> ] [ encoding>> encoding>name ] bi "; charset=" glue ;
194 : email>headers ( email -- assoc )
196 now timestamp>rfc822 "Date" ,,
197 message-id "Message-Id" ,,
198 "1.0" "MIME-Version" ,,
199 "base64" "Content-Transfer-Encoding" ,,
202 [ to>> ", " join "To" ,, ]
203 [ cc>> ", " join [ "Cc" ,, ] unless-empty ]
204 [ subject>> "Subject" ,, ]
205 [ email-content-type "Content-Type" ,, ]
209 : (send-email) ( headers email -- )
213 smtp-tls? get [ start-tls get-ok send-secure-handshake ] when
215 dup from>> extract-email mail-from get-ok
216 dup to>> [ extract-email rcpt-to get-ok ] each
217 dup cc>> [ extract-email rcpt-to get-ok ] each
218 dup bcc>> [ extract-email rcpt-to get-ok ] each
224 ] with-smtp-connection ;
228 : send-email ( email -- )
229 [ email>headers ] keep (send-email) ;