+(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 25)))
+ (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 '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)
+ (string-prefix? "220" (read-line tcp-in)))
+ (('send strings ...)
+ (write-string (string-translate (conc (apply conc strings) "\n") "\n" "\r\n")
+ out-port))))