4

I'm trying to get a hello world sending a mail via the JS Gmail API. I have authorised correctly (labels can be listed) according to this.

I'm using the following code, running in the browser:

const message =
"From: [email protected]\r\n" + 
"To: [email protected]\r\n" +
"Subject: As basic as it gets\r\n\r\n" +
"This is the plain text body of the message.  Note the blank line between the header information and the body of the message.";


// The body needs to be base64url encoded.
const encodedMessage = btoa(message)

const reallyEncodedMessage = encodedMessage.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '')

gapi.client.gmail.users.messages.send({
    userId: 'me',
    requestBody: {
        // same response with any of these
        raw: reallyEncodedMessage
        // raw: encodedMessage
        // raw: message
    }
}).then(function () { console.log("done!")});

This gives an HTTP 400 response:

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "invalidArgument",
    "message": "'raw' RFC822 payload message string or uploading message via /upload/* URL required"
   }
  ],
  "code": 400,
  "message": "'raw' RFC822 payload message string or uploading message via /upload/* URL required"
 }
}

This is using JS API available at https://apis.google.com/js/api.js. RFC822 example taken from MSDN and elsewhere. Web-safe base64 encoding the RFC822 message as far as I can tell is a the standard with this API. Same error in both Firefox and Chrome.

Where am I going wrong?

2
  • you could share your github project please? all code ? Commented Dec 25, 2023 at 0:17
  • @jonathasborges1 long since deleted sorry. Commented Jan 5, 2024 at 12:03

2 Answers 2

4

I think that the raw data is correct. So how about this modification?

From :

requestBody: {
    // same response with any of these
    raw: reallyEncodedMessage
    // raw: encodedMessage
    // raw: message
}

To :

resource: { // Modified
    // same response with any of these
    raw: reallyEncodedMessage
    // raw: encodedMessage
    // raw: message
}

If this didn't work, please tell me. I would like to think of other solution.

Sign up to request clarification or add additional context in comments.

3 Comments

@sennett I'm glad your issue was solved. Thank you, too.
please could share where you found documentation about lib "gapi" to answer this ask?
@jonathasborges1 About your question of please could share where you found documentation about lib "gapi" to answer this ask?, how about this? github.com/google/google-api-javascript-client
1

I dont know how to code work but code work by removing the body that are been added to you raw statement.

gapi.client.gmail.users.messages.send({
            userId: 'me',
                // resourse: {
                // same response with any of these
                raw: reallyEncodedMessage
                // raw: encodedMessage
                // raw: message
            // }

I hope you try this because answer by @Tanaike was not working for me

1 Comment

sorry , as it is not organised

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.