5

I make a simple request step-by-step in Gmail API.

According to all instructions I made this call:

var request = gapi.client.gmail.users.messages.send({
    'userId': 'me',
    "payload": {
    "headers": [
             {
              "name": "To",
              "value": "########@gmail.com"
             }
           ]},
     'raw': 'SEVMTE8gTVkgREVBUiBGUklFTkQ='
     });

request.then(()=>{console.log('yep')})

But receive an error:

{
   "error": {
   "errors": [
     {
       "domain": "global",
       "reason": "invalidArgument",
       "message": "Recipient address required"
     }
   ],
   "code": 400,
   "message": "Recipient address required"
  }
}
Brandon Minnick
  • 13,342
  • 15
  • 65
  • 123
Max S
  • 109
  • 1
  • 4

1 Answers1

9

The correct format in sending to emails in Gmail API is stated in Users.messages: send:

function sendMessage(userId, email, callback) {
  // Using the js-base64 library for encoding:
  // https://www.npmjs.com/package/js-base64
  var base64EncodedEmail = Base64.encodeURI(email);
  var request = gapi.client.gmail.users.messages.send({
    'userId': userId,
    'resource': {
      'raw': base64EncodedEmail
    }
  });
  request.execute(callback);
}

For a more vivid sample on how to use this method, check this SO post:

[...] the complete message needs to be passed in the raw parameter, see the example:

From: John Doe <jdoe@machine.example> 
To: Mary Smith <mary@example.net> 
Subject: Saying Hello 
Date: Fri, 21 Nov 1997 09:55:06 -0600 
Message-ID: <1234@local.machine.example>

This is a message just to say hello. So, "Hello". 

So after base64 encoding the complete message, passing it in the raw parameter without using any other parameter, it works fine.

ottomeister
  • 5,415
  • 2
  • 23
  • 27
ReyAnthonyRenacia
  • 17,219
  • 5
  • 37
  • 56
  • Encoding whole info into base64 hellped to solve sending issue. Thank you a lot. Can you help me either to send an html letter? – Max S Mar 07 '18 at 13:38
  • 1
    I've figured out how to send an html letter. To add a *Content-Type: text/html; charset=utf-8* became a resolution. Thank you once again/ – Max S Mar 07 '18 at 13:49