Send an HTML email using the Gmail API

2019-05-24 13:43发布

问题:

For some reason the Gmail API won't send an html email. Sending plaintext works fine:

var message = 'From: Me <me@gmail.com>\r\n' +
    'To: Me <me@gmail.com>\r\n' +
    'Subject: Hello\r\n\r\n'
    'World'

var raw = btoa(message)

Then when I try html, it just shows up as an empty message:

var message = 'From: Me <me@gmail.com>\r\n' +
    'To: Me <me@gmail.com>\r\n' +
    'Subject: Hello\r\n'
    'Content-Type: text/html; charset=utf-8\r\n' +
    'Content-Transfer-Encoding: quoted-printable\r\n\r\n' +
    '<html><body>' +
    '<h1>World</h1>' +
    '</body></html>'

var raw = btoa(message)

Any ideas? Maybe because it's not RFC 2822 compliant?

回答1:

For starters you need to use base64url encoding, using the web/url safe alphabet not just the standard btoa() base64. If that doesn't fix it can you post your code and the exact error message you're getting? (Or does it work and not show up as html?)