title: Auth error sending email with smtp and Gmail in Google App Engine
body: <p>I'm trying to send an email with my Google Apps email via SMTP in App Engine, but I get an 'Authorization required' during the <code>sendmail</code> call. It seems the auth call itself succeeds.</p>
  
  <p>Here's the message I'm trying to send. It's a draft retrieved via IMAP, so I know the access token is good. It's generated by Python's <a href="https://docs.python.org/2/library/email.message.html#email.message.Message.as_string" rel="nofollow"><code>email.Message.as_string</code></a> method.</p>
  
  <pre><code>MIME-Version: 1.0
  Received: by 10.76.124.8 with HTTP; Thu, 3 Apr 2014 00:12:26 -0700 (PDT)
  To: Ela Honoridez II &lt;ela@mydomain.com&gt;
  Date: Thu, 3 Apr 2014 15:12:26 +0800
  Message-ID: &lt;CAN8SGD-joz9Adq9QpKowME3N0xmgwegYGYwRVQh_9kbGfTa75A@mail.gmail.com&gt;
  Subject: =?UTF-8?B?RGVsYXllZCBlbWFpbCB0ZXN0ICMyIMOlw5/iiJo=?=
  From: John Del Rosario &lt;john@mydomain.com&gt;
  Content-Type: multipart/alternative; boundary=14dae94edfeda1125304f61e1fec
  
  --14dae94edfeda1125304f61e1fec
  Content-Type: text/plain; charset=UTF-8
  
  foo bar
  
  --14dae94edfeda1125304f61e1fec
  Content-Type: text/html; charset=UTF-8
  
  &lt;div dir="ltr"&gt;foo bar&lt;/div&gt;
  
  --14dae94edfeda1125304f61e1fec--
  </code></pre>
  
  <p>Here's my code:
  </p>
  
  <pre><code>auth_string = 'user=%s^Aauth=Bearer %s^A^A' % ('john@mydomain.com', access_token)
  smtp = smtplib.SMTP('smtp.gmail.com', 587)
  smtp.set_debuglevel(True)
  smtp.ehlo()
  smtp.starttls()
  smtp.ehlo()
  smtp.docmd('AUTH', 'XOAUTH2 ' + base64.b64encode(auth_string))
  smtp.sendmail('john@mydomain.com', ['ela@mydomain.com'], rfc822_msg_str)
  </code></pre>
  
  <p>And here's the debug messages from smtp.</p>
  
  <pre><code>send: 'ehlo dev.myapp.appspot.com\r\n'
  reply: '250-mx.google.com at your service, [74.125.182.85]\r\n'
  reply: '250-SIZE 35882577\r\n'
  reply: '250-8BITMIME\r\n'
  reply: '250-STARTTLS\r\n'
  reply: '250-ENHANCEDSTATUSCODES\r\n'
  reply: '250 CHUNKING\r\n'
  reply: retcode (250); Msg: mx.google.com at your service, [74.125.182.85]
  SIZE 35882577
  8BITMIME
  STARTTLS
  ENHANCEDSTATUSCODES
  CHUNKING
  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: 'ehlo dev.myapp.appspot.com\r\n'
  reply: '250-mx.google.com at your service, [74.125.182.85]\r\n'
  reply: '250-SIZE 35882577\r\n'
  reply: '250-8BITMIME\r\n'
  reply: '250-AUTH LOGIN PLAIN XOAUTH XOAUTH2 PLAIN-CLIENTTOKEN\r\n'
  reply: '250-ENHANCEDSTATUSCODES\r\n'
  reply: '250 CHUNKING\r\n'
  reply: retcode (250); Msg: mx.google.com at your service, [74.125.182.85]
  SIZE 35882577
  8BITMIME
  AUTH LOGIN PLAIN XOAUTH XOAUTH2 PLAIN-CLIENTTOKEN
  ENHANCEDSTATUSCODES
  CHUNKING
  send: 'AUTH XOAUTH2 (snipped base64 encoded auth string)\r\n'
  reply: '501 5.5.2 Cannot Decode response s9sm12053561igw.16 - gsmtp\r\n'
  reply: retcode (501); Msg: 5.5.2 Cannot Decode response s9sm12053561igw.16 - gsmtp
  send: u'mail FROM:&lt;john@mydomain.com&gt; size=651\r\n'
  reply: '530-5.5.1 Authentication Required. Learn more at\r\n'
  SMTPSenderRefused: (530, '5.5.1 Authentication Required. Learn more at\n5.5.1 http://support.google.com/mail/bin/answer.py?answer=14257 s9sm12053561igw.16 - gsmtp', u'john@mydomain.com')
  </code></pre>