+(define (get-host-from-email email-string)
+ (car (string-split (cadr (string-split email-string "@")) ">")))
+
+(define (deliver-message-remote msg config)
+ (let ((host (get-host-from-email (message-to msg))))
+ (print "Attempting delivery to host " host)
+ (let-values (((tcp-in tcp-out) (tcp-connect host 2525)))
+ (let ((smtp-session (make-outgoing-smtp-session tcp-in tcp-out)))
+ (let ((result (and
+ (smtp-session 'expect "220")
+ (smtp-session 'send "helo " (config-host config))
+ (smtp-session 'expect "250")
+ (smtp-session 'send "mail from:" (message-from msg))
+ (smtp-session 'expect "250")
+ (smtp-session 'send "rcpt to:" (message-to msg))
+ (smtp-session 'expect "250")
+ (smtp-session 'send "data")
+ (smtp-session 'expect "354")
+ (smtp-session 'send (message-text msg))
+ (smtp-session 'send ".")
+ (smtp-session 'expect "250")
+ (smtp-session 'send "quit"))))
+ (close-input-port tcp-in)
+ (close-output-port tcp-out)
+ result)))))
+
+(define ((make-outgoing-smtp-session tcp-in tcp-out) . command)
+ (match command
+ (('expect code)
+ (let ((result (read-line tcp-in)))
+ (print "Expecting " code " got " result)
+ (string-prefix? code result)))
+ (('send strings ...)
+ (print "Sending " (car strings) (if (> (length strings) 1) " ... (truncated)" ""))
+ (let ((processed-string
+ (string-translate* (conc (apply conc strings) "\n")
+ '(("\n" . "\r\n")))))
+ (write-string processed-string #f tcp-out)))))