New features in SMTP and SSL library allow sending e-mail through Gmail
Sunday, November 30, 2008
Although I added SSL
support
to Factor’s I/O library recently, it was missing a feature needed by
SMTP-TLS: upgrading insecure connections to secure ones. Unlike HTTPS,
where the client connects to a special port number initiates a secure
handshake as soon as the connection is established, SMTP uses a
STARTTLS
command, which is sent in plain-text, and handshaking only
begins after this command is sent.
To accomodate this use-case, I added two new words to the
io.sockets.secure
vocabulary: send-secure-handshake
and
accept-secure-handshake
. They are meant to be used in a client and
server respectively, wishing to upgrade the current connection to a
secure connection.
I also updated the smtp
vocabulary to support the STARTTLS
and
AUTH PLAIN
features of the SMTP protocol. The end result is that the
SMTP vocabulary can now send e-mail through Gmail’s SMTP servers. An
example is shown below. The key thing to notice here is that we set the
smtp-auth
and smtp-tls?
variables before sending the e-mail. As you
can see, the SMTP library is easy to use and the code for sending an
e-mail is succinct; we create an e-mail object, fill in some slots, and
send it off:
"smtp.gmail.com" 587 <inet> smtp-server set
smtp-tls? on
"YourUserName@gmail.com" "YourPassword" <plain-auth> smtp-auth set
<email>
"yourname@example.com" >>from
{ "theirname@example.com" } >>to
"We should probably beer" >>subject
"For teh win" >>body
send-email
The SMTP library is still missing some functionality, like support for character encodings, attachements and setting custom headers; I’ll be adding those over time.