Daniel Marín Daniel Marín - 1 year ago 194
Javascript Question

gmail api send email using content in html

I am using the gmail api to send emails. The following is my code

function sendEmail(auth, from, to, subject, content) {

var encodedEmail = new Buffer(
'From: ' + from + '\r\n' +
'To: ' + to + '\r\n' +
'Subject: ' + subject + '\r\n\r\n' +

).toString('base64').replace(/\+/g, '-').replace(/\//g, '_');

var gmail = google.gmail('v1');
var request = gmail.users.messages.send({
auth: auth,
userId: 'me',
resource: {
raw: encodedEmail

But the content in this case should be plain/text. The problem is that I want to pass the 'content' in HTML format. Any suggestion on how I can solve this?

Answer Source

You have to provide a content type in your message, or it will default to plain/text as you mentioned. Just add a Content-Type-header with the value text/html:

From: sender@gmail.com
To: receiver@gmail.com
Subject: Foo bar
Content-Type: text/html; charset=UTF-8

<b> This is the content of the email </b>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download