1

I have followed gmail api for sending email. I am getting error as:

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

Here is the piece of code I have written for sending mail using gmail api with node.js. Help me out to resolve the issue.

router.post('/composeMail', async (req, res, next) => {
    function makeBody(to, from, subject, message) {
        let str = ["Content-Type: text/plain; charset=\"UTF-8\"\n",
            "Content-length: 5000\n",
            "Content-Transfer-Encoding: message/rfc822\n",
            "to: ", to,"\n",
            "from: ", from,"\n",
            "subject: ", subject,"\n\n",
            message
        ].join('');
        console.log("String: ", str);
        // let encodedMail = new Buffer(str).toString("base64").replace(/\+/g, '-').replace(/\//g, '_');
        let encodedMail = btoa(str).replace(/\+/g, '-').replace(/\//g, '_');
        return encodedMail;
    }
    let raw = makeBody("[email protected]", "[email protected]", "Test mail", "Everything is fine");
    let obj = {};
    obj.raw = raw;
    let body = JSON.stringify(obj);
    let option = {
        url: "https://www.googleapis.com/gmail/v1/users/userId/messages/send",
        method: 'POST',
        headers: {
            'Authorization': `Bearer ${req.query.access_token}`
        },
        qs: {
            userId: 'me'
        },
        body: body
    };

    await request(option).then(body => {
        return res.apiOk(body);
    }).catch(err => {
        return res.apiError(err);
    })
});
2
  • Did my answer show you the result what you want? Would you please tell me about it? That is also useful for me to study. If this works, other people who have the same issue with you can also base your question as a question which can be solved. If you have issues for my answer yet, feel free to tell me. I would like to study to solve your issues. Commented Nov 13, 2018 at 22:02
  • Yes your answer worked for me @Tanaike Commented Nov 14, 2018 at 5:40

1 Answer 1

2
  • You want to send an email using Gmail API by the request module.

If my understanding is correct, how about this modification? I think that there are several answers. So please think of this as one of them.

Modification points:

  • Please use https://www.googleapis.com/upload/gmail/v1/users/userId/messages/send as the endpoint.
  • Use the value as a string.
  • Add 'Content-Type': 'message/rfc822' to the headers.

Modified script:

Please modify makeBody() as follows.

function makeBody(to, from, subject, message) {
    let str = [
        "to: ", to, "\n",
        "from: ", from, "\n",
        "subject: ", subject, "\n\n",
        message,
    ].join('');
    return str;
}

Please modify option as follows.

let raw = makeBody("[email protected]", "[email protected]", "Test mail", "Everything is fine");
const userId = 'me'; // Please modify this for your situation.
let option = {
    url: "https://www.googleapis.com/upload/gmail/v1/users/" + userId + "/messages/send",
    method: 'POST',
    headers: {
        'Authorization': `Bearer ${req.query.access_token}`,
        'Content-Type': 'message/rfc822',
    },
    body: raw,
};

Note:

  • This modified script supposes that Gmail API is enabled at API console and the required scope for sending emails is included in the scopes of access token.

Reference:

In my environment, I could confirm that this modified script worked fine. But if this was not what you want, I'm sorry.

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

8 Comments

@Dinesh Sathrasala I'm sorry. I cannot understand about your comment. Can you post it as new question including more information? When you post it, it will help users including me think of your issue.
Tanaike please check the link of my post stackoverflow.com/questions/53761566/…
@Dinesh Sathrasala Thank you for replying. I would like to check it.
I have found solution for it. Thanks for replying.
@Dinesh Sathrasala Thank you for letting me know. I'm glad your issue was resolved. Thank you, too.
|

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.