Build an Authenticating SMTP Client The basic parts of SMTP can be implemented in surprising little code. I was able to build an example SMTP client that works with Gmail and Amazon SES in about 320 characters using PHP.

Your task is to implement an SMTP client that can send UTF-8 email from at least 2 major providers listed here:


Gmail
Yahoo!
Live/Hotmail
Postmark
Amazon SES


Keep in mind that most require SSL support. Some SMTP services (like Amazon) require different SMTP Auth and From addresses. Your function can take input for the actual data transmitted. After all, it woudn't make sense to hard-code anything into the transport layer.


SMTP User
SMTP Password
SMTP Host
SMTP Port
DATA (message)
From email
To email


Sample DATA (message)

Subject: =?UTF-8?B?VGVzdCBFbWFpbA==?=\r\n
To: &lt;him@gmail.com&gt;\r\n
From: &lt;you@gmail.com&gt;\r\n
MIME-Version: 1.0\r\n
Content-Type: text/html; charset=utf-8\r\n
Content-Transfer-Encoding: base64\r\n\r\n
PGgxPlRlc3QgRW1haWw8L2gxPjxwPkhlbGxvIFRoZXJlITwvcD4=\r\n.


In Summary


Build an SMTP client that can send email through at least two of the services above.
Accepts the seven inputs needed to form a valid SMTP Auth request (above). (Might only need six inputs if you combine the "From" and "SMTP User" inputs).
Use as few characters as posible.
Python 3 864 bytes

Extremely quick and dirty. Works with AWS SES and Yahoo!. Could not get it to work with Postmark (requires STARTTLS) or Gmail (requires further authentication outside of the SMTP connection).

import socket,ssl
from base64 import*
N=b'\x00'
U='utf-8'
E=lambda s:s.encode(U)
D=lambda b:b.decode(U)
B=lambda s:D(b64encode(E(s)))
A=lambda f,t,j,b:E('\r\n'.join(['DATA','Subject: =?UTF-8?B?{}?=','To: &lt;{}&gt;','From: &lt;{}&gt;','MIME-Version: 1.0','Content-Type: text/html; charset=utf-8','Content-Transfer-Encoding: base64',B(b),'','.','']).format(B(j),t,f,b))
X=lambda s,e:s.startswith(e)
def S(h,u,p,t,f,j,b):
    c,F,W=ssl.wrap_socket(socket.create_connection((h,465))),lambda m:E(m if'amazonaws'in h else'&lt;'+m+'&gt;'),lambda m:c.write(m+b'\r\n')
    def R(d,o=[]):
        x,y=c.read(),str(d)
        if not(X(x,E(y))or filter(lambda i:X(i,X(x,E(y))),o)):raise RuntimeError(y+' '+repr(x))
    R(220);W(b'EHLO x');R(250);W(b'AUTH PLAIN '+b64encode(N+E(u)+N+E(p)));R(235);W(b'MAIL FROM: '+F(f));R(250);W(b'RCPT TO: '+F(t));R(250);W(A(f,t,j,b));R(250,(354,));W(b'QUIT')


Invoke like S(hostname, username, password, to, from_, subject, body)

Expect bugs and changes (kind of got bored so it feels slightly half-baked).