5

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' +

   content 
 ).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?

1 Answer 1

9

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: [email protected]
To: [email protected]
Subject: Foo bar
Content-Type: text/html; charset=UTF-8

<b> This is the content of the email </b>
Sign up to request clarification or add additional context in comments.

2 Comments

Tholle... Content-Type: text/html; charset=UTF-8 Is encoding the content, but I noticed that the subject is not being encoded. If the subject has a word like 'português' it shows 'português' Do you know how do I tell the gmail api to encode the subject to UTF-8 ?
This blog post explains it. var encodedSubject = '=?utf-8?B?' + btoa(subject) + '?=';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.