0

I try to use gmail API to send emails form node application. Here is my code:

function sendEmail(auth) {
    var gmail = google.gmail('v1');
    gmail.users.messages.send({
        auth,
        userId: 'me',
        resource: {
            payload: {
                mimeType: 'message/rfc822',
                headers: [{name: 'To', value: 'ayeritsian@gmail.com'},
                    {name: 'Subject', value: 'test'},
                    {name: 'From', value: 'ayeressian2@gmail.com'}]
            },
            raw: new Buffer('test123').toString('base64')
        },
        internalDate: Date.now()
    }, function (err, bla, IncommingMessage) {
        console.log(arguments);
        console.log('end');
    });
}

When I run the application I keep getting bounce messages "An error occurred. Your message was not sent.". The error message is not descriptive enough and there is almost no documentation for google-api node package. I will appreciate it if someone can help me out.

Ara Yeressian
  • 3,746
  • 3
  • 26
  • 36
  • Eric answers your question below. [Look at this answer](http://stackoverflow.com/questions/29504289/send-email-using-google-api-with-only-access-token/29515412#29515412) if you just want to do it with a regular http-request. – Tholle Dec 19 '15 at 14:56

2 Answers2

0

You need to put the entire email in the "raw" field on the message, the parsed-out message doesn't work with send. c.f. https://developers.google.com/gmail/api/guides/sending

this is something akin to python, i'm sure you can translate to node.js :) email = "From: myemail@gmail.com\r\nTo: someguy@gmail.com\r\nSubject: hey check this out\r\n\r\nhi someguy, this is my email body here. it's plain text." message.raw = base64_websafe(email)

Eric D
  • 6,901
  • 1
  • 15
  • 26
  • Can you elaborate more? My email message is test123 and I've put in the raw field. I also tried without converting to base64 and I still get the same error. – Ara Yeressian Dec 18 '15 at 07:35
  • Need to put the entire email (headers and body (in RFC2822 format)) in the raw field, base64 url/web encoded. Updated my answer to have a quick example. – Eric D Dec 18 '15 at 17:58
0

For what it's worth I had a similar issue to yours. I kept getting a vague error message in the inbox of my app/user.

From: nobody@gmail.com
An error occurred. Your message was not sent.

What resolved the issue for me was adding the Message-ID property to the Buffer I created. Basically, my message wasn't composed to the rfc822 spec that they mention in the docs here.

Here is the document I referenced and below is my snippet.

module.exports = function (policyMeta) {
  log.info('BUILDING EMAIL');
  const message = 'From: xxx@xxx.com\r\n' +
                  'To: xxx@xxx.com\r\n' +
                  `Date: ${new Date()}\r\n` +
                  'Subject: Howdy Mundo\r\n' +
                  `Message-ID: ${uuid()}\r\n` +
                  'look mom i send a message';
  const params = {
    auth: Auth.getGoogleAuthClient(),
    userId: 'me',
    media: { mimeType: 'message/rfc822' },
    resource: {
      raw: new Buffer(message).toString('base64')
    }
  };
  return sendEmail(params).then((res) => console.log(res, 'success'));
};
Mike Fleming
  • 2,593
  • 4
  • 14
  • 24