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 ;
10 FROM: namespaces => set ;
16 "localhost" 25 <inet> smtp-server set-global
20 SYMBOL: smtp-read-timeout
21 1 minutes smtp-read-timeout set-global
25 TUPLE: plain-auth username password ;
26 C: <plain-auth> plain-auth
29 no-auth smtp-auth set-global
31 LOG: log-smtp-connection NOTICE
33 : with-smtp-connection ( quot -- )
35 dup log-smtp-connection
37 smtp-domain [ host-name or ] change
38 smtp-read-timeout get timeouts
40 ] with-client ; inline
48 { content-type string initial: "text/plain" }
49 { encoding word initial: utf8 }
52 : <email> ( -- email ) email new ; inline
56 : command ( string -- ) write crlf flush ;
58 \ command DEBUG add-input-logging
60 : helo ( -- ) "EHLO " host-name append command ;
62 : start-tls ( -- ) "STARTTLS" command ;
64 ERROR: bad-email-address email ;
66 : validate-address ( string -- string' )
67 #! Make sure we send funky stuff to the server by accident.
68 dup "\r\n>" intersects?
69 [ bad-email-address ] when ;
71 : mail-from ( fromaddr -- )
73 "MAIL FROM:<" ">" surround command ;
77 "RCPT TO:<" ">" surround command ;
82 : send-body ( email -- )
84 [ body>> ] [ encoding>> ] bi encode >base64-lines write
85 ascii encode-output crlf
91 LOG: smtp-response DEBUG
93 : multiline? ( response -- ? )
94 3 swap ?nth CHAR: - = ;
96 : (receive-response) ( -- )
100 [ multiline? [ (receive-response) ] when ]
103 TUPLE: response code messages ;
105 : <response> ( lines -- response )
106 [ first 3 head string>number ] keep response boa ;
108 : receive-response ( -- response )
109 [ (receive-response) ] { } make <response> ;
111 ERROR: smtp-error response ;
114 "SMTP error (" write dup class pprint ")" print
115 response>> messages>> [ print ] each ;
117 ERROR: smtp-server-busy < smtp-error ;
118 ERROR: smtp-syntax-error < smtp-error ;
119 ERROR: smtp-command-not-implemented < smtp-error ;
120 ERROR: smtp-bad-authentication < smtp-error ;
121 ERROR: smtp-mailbox-unavailable < smtp-error ;
122 ERROR: smtp-user-not-local < smtp-error ;
123 ERROR: smtp-exceeded-storage-allocation < smtp-error ;
124 ERROR: smtp-bad-mailbox-name < smtp-error ;
125 ERROR: smtp-transaction-failed < smtp-error ;
127 : check-response ( response -- )
129 { [ dup { 220 235 250 221 354 } member? ] [ 2drop ] }
130 { [ dup 400 499 between? ] [ drop smtp-server-busy ] }
131 { [ dup 500 = ] [ drop smtp-syntax-error ] }
132 { [ dup 501 = ] [ drop smtp-command-not-implemented ] }
133 { [ dup 500 509 between? ] [ drop smtp-syntax-error ] }
134 { [ dup 530 539 between? ] [ drop smtp-bad-authentication ] }
135 { [ dup 550 = ] [ drop smtp-mailbox-unavailable ] }
136 { [ dup 551 = ] [ drop smtp-user-not-local ] }
137 { [ dup 552 = ] [ drop smtp-exceeded-storage-allocation ] }
138 { [ dup 553 = ] [ drop smtp-bad-mailbox-name ] }
139 { [ dup 554 = ] [ drop smtp-transaction-failed ] }
143 : get-ok ( -- ) receive-response check-response ;
145 GENERIC: send-auth ( auth -- )
147 M: no-auth send-auth drop ;
149 : plain-auth-string ( username password -- string )
150 [ "\0" prepend ] bi@ append utf8 encode >base64 ;
152 M: plain-auth send-auth
153 [ username>> ] [ password>> ] bi plain-auth-string
154 "AUTH PLAIN " prepend command get-ok ;
156 : auth ( -- ) smtp-auth get send-auth ;
158 : encode-header ( string -- string' )
161 "=?utf-8?B?" "?=" surround
164 ERROR: invalid-header-string string ;
166 : validate-header ( string -- string' )
167 dup "\r\n" intersects?
168 [ invalid-header-string ] when ;
170 : write-header ( key value -- )
171 [ validate-header write ]
172 [ ": " write validate-header encode-header write ] bi* crlf ;
174 : write-headers ( assoc -- )
175 [ write-header ] assoc-each ;
177 : message-id ( -- string )
182 gmt timestamp>micros #
184 smtp-domain get [ host-name ] unless* %
188 : extract-email ( recepient -- email )
189 ! This could be much smarter.
190 " " split1-last swap or "<" ?head drop ">" ?tail drop ;
192 : email-content-type ( email -- content-type )
193 [ content-type>> ] [ encoding>> encoding>name ] bi "; charset=" glue ;
195 : email>headers ( email -- assoc )
197 now timestamp>rfc822 "Date" set
198 message-id "Message-Id" set
199 "1.0" "MIME-Version" set
200 "base64" "Content-Transfer-Encoding" set
202 [ from>> "From" set ]
203 [ to>> ", " join "To" set ]
204 [ cc>> ", " join [ "Cc" set ] unless-empty ]
205 [ subject>> "Subject" set ]
206 [ email-content-type "Content-Type" set ]
210 : (send-email) ( headers email -- )
214 smtp-tls? get [ start-tls get-ok send-secure-handshake ] when
216 dup from>> extract-email mail-from get-ok
217 dup to>> [ extract-email rcpt-to get-ok ] each
218 dup cc>> [ extract-email rcpt-to get-ok ] each
219 dup bcc>> [ extract-email rcpt-to get-ok ] each
225 ] with-smtp-connection ;
229 : send-email ( email -- )
230 [ email>headers ] keep (send-email) ;