title: python & smtplib: Is sending mail via gmail using oauth2 possible?
body: <p>So I can login to and send mail through gmail using smtplib (using the script below), but I was just wondering if using oauth2 was an option like with imaplib?  I didn't see anything on the smtplib documentation page about oauth and I haven't found anything googling. Thanks.</p>
  
  <pre><code>#! /usr/bin/python
  
  import smtplib
  
  to = 'myemailaddress'
  gmail_user = 'myemailaddress'
  gmail_pwd = 'passwd'
  smtpserver = smtplib.SMTP("smtp.gmail.com",587)
  smtpserver.ehlo()
  smtpserver.starttls()
  smtpserver.ehlo
  smtpserver.login(gmail_user, gmail_pwd)
  header = 'To:' + to + '\n' + 'From: ' + gmail_user + '\n' + 'Subject:testing \n'
  print header
  msg = header + '\n this is test msg from me \n\n'
  smtpserver.sendmail(gmail_user, to, msg)
  print 'done!'
  smtpserver.close();
  </code></pre>
  
  <p><strong>Edit:</strong></p>
  
  <p>Thanks to samy.vilar for the very detailed explaination provided in his answer.  However, I'm having a little trouble.  Here is my script:</p>
  
  <pre><code>#! /usr/bin/python
  
  import oauth2 as oauth
  import oauth2.clients.smtp as smtplib
  
  consumer = oauth.Consumer('anonymous', 'anonymous')
  token = oauth.Token('1/MI6B2DqJP4FEkDRLUKrD5l46sQ0758-2ucEKBY-DeB0', 'NysqNqVTulFsdHpSRrPP56sF')
  
  url = "https://mail.google.com/mail/b/testing.oauth.1@gmail.com/smtp/"
  
  conn = smtplib.SMTP('smtp.googlemail.com', 587)
  conn.set_debuglevel(True)
  conn.ehlo('test')
  conn.starttls()
  
  conn.authenticate(url, consumer, token)
  
  header = 'To:testing.oauth.1@gmail.com\n' + 'From: testing.oauth.1@gmail.com\n' + 'Subject:testing \n'
  msg = header + '\n this is test msg from me \n\n'
  conn.sendmail('testing.oauth.1@gmail.com', 'testing.oauth.1@gmail.com', msg)
  </code></pre>
  
  <p>What is puzzling me, is that it appears to authenticate ok:</p>
  
  <pre><code>send: 'ehlo test\r\n'
  reply: '250-mx.google.com at your service, [75.173.8.127]\r\n'
  reply: '250-SIZE 35882577\r\n'
  reply: '250-8BITMIME\r\n'
  reply: '250-STARTTLS\r\n'
  reply: '250 ENHANCEDSTATUSCODES\r\n'
  reply: retcode (250); Msg: mx.google.com at your service, [75.173.8.127]
  SIZE 35882577
  8BITMIME
  STARTTLS
  ENHANCEDSTATUSCODES
  send: 'STARTTLS\r\n'
  reply: '220 2.0.0 Ready to start TLS\r\n'
  reply: retcode (220); Msg: 2.0.0 Ready to start TLS
  send: 'AUTH XOAUTH R0VUIGh0dHBzOi8vbWFpbC5nb29nbGUuY29tL21haWwvYi90ZXN0aW5nLm9hdXRoLjFAZ21haWwuY29tL3NtdHAvIG9hdXRoX2JvZHlfaGFzaD0iMmptajdsNXJTdzB5VmIlMkZ2bFdBWWtLJTJGWUJ3ayUzRCIsb2F1dGhfY29uc3VtZXJfa2V5PSJhbm9ueW1vdXMiLG9hdXRoX25vbmNlPSI3Nzc0ODMyIixvYXV0aF9zaWduYXR1cmU9IkxuckZHODdxdHRxZUhsUlQ1emRndmtEZ1UzTSUzRCIsb2F1dGhfc2lnbmF0dXJlX21ldGhvZD0iSE1BQy1TSEExIixvYXV0aF90aW1lc3RhbXA9IjEzNDIxNDI3NzIiLG9hdXRoX3Rva2VuPSIxJTJGTUk2QjJEcUpQNEZFa0RSTFVLckQ1bDQ2c1EwNzU4LTJ1Y0VLQlktRGVCMCIsb2F1dGhfdmVyc2lvbj0iMS4wIg==\r\n'
  reply: '235 2.7.0 Accepted\r\n'
  reply: retcode (235); Msg: 2.7.0 Accepted
  </code></pre>
  
  <p>But then when it comes to sending the email, it seems to forget that <code>conn</code> was already authenticated:</p>
  
  <pre><code>send: 'ehlo [192.168.2.4]\r\n'
  reply: '250-mx.google.com at your service, [75.173.8.127]\r\n'
  reply: '250-SIZE 35882577\r\n'
  reply: '250-8BITMIME\r\n'
  reply: '250-AUTH LOGIN PLAIN XOAUTH\r\n'
  reply: '250 ENHANCEDSTATUSCODES\r\n'
  reply: retcode (250); Msg: mx.google.com at your service, [75.173.8.127]
  SIZE 35882577
  8BITMIME
  AUTH LOGIN PLAIN XOAUTH
  ENHANCEDSTATUSCODES
  send: 'mail FROM:&lt;testing.oauth.1@gmail.com&gt; size=107\r\n'
  reply: '530-5.5.1 Authentication Required. Learn more at\r\n'
  reply: '530 5.5.1 http://support.google.com/mail/bin/answer.py?answer=14257 tu7sm3163839pbc.55\r\n'
  reply: retcode (530); Msg: 5.5.1 Authentication Required. Learn more at
  5.5.1 http://support.google.com/mail/bin/answer.py?answer=14257 tu7sm3163839pbc.55
  send: 'rset\r\n'
  reply: '250 2.1.5 Flushed tu7sm3163839pbc.55\r\n'
  reply: retcode (250); Msg: 2.1.5 Flushed tu7sm3163839pbc.55
  Traceback (most recent call last):
    File "./gmail_send3.py", line 47, in &lt;module&gt;
      conn.sendmail('testing.oauth.1@gmail.com', 'testing.oauth.1@gmail.com', msg)
    File "/usr/lib/python2.7/smtplib.py", line 713, in sendmail
      raise SMTPSenderRefused(code, resp, from_addr)
  smtplib.SMTPSenderRefused: (530, '5.5.1 Authentication Required. Learn more at\n5.5.1 http://support.google.com/mail/bin/answer.py?answer=14257 tu7sm3163839pbc.55', 'testing.oauth.1@gmail.com')
  </code></pre>