Issues in sending gmail

Hi
I am trying to send mail using Gmail API. I have seen few solved cases in this forum but I am not able to succeed. I am getting below error
"code": 400,
"message": "Recipient address required"

Hence sharing my issue details.
To start with, I have created a resource using gmail API using OAUTH2 authentication.
Then I have created a transformer ('emailtransformer") as below


const subject = 'Reminder for Pending bill'
const body = 'Please pay your bill as the due date is over'
const from = 'from_mail@gmail.com'
const to = 'to_mail@gmail.com'

const messageParts = [
From: ${from},
To: ${to},
'Content-Type: text/html; charset=utf-8',
'MIME-Version: 1.0',
Subject: ${subject},
'',
body,
];
const message = messageParts.join('\n');
//return messageParts.join('\r\n');
const encodedMessage = btoa(message)
.replace(/+/g, '-')
.replace(///g, '_')
.replace(/=+$/, '');

return encodedMessage


I am using above transformer in a query (below screenshot)

Please suggest how this can be resolved and what changes I need to make this work.
Thanks in advance

I used to work with Gmail API a bit. Here are a few considerations.

As per building the raw string, you may try to build it this way:

  // here, the 'From' field is not necessary because your API URL contains 'me' user ID,
  // see https://developers.google.com/gmail/api/reference/rest/v1/users.messages/send
  const message = `To: ${to}\nSubject: ${subject}\n\n${body}`;
  const raw = Buffer.from(message).toString("base64");

Then, in your query5 http request, I would try to either omit Content-Type header at all or set it to application/json. I believe there also should be Authorization http header with the access token, it's value should be like 'Bearer ' + access_token.

I'm using the code from a project of mine. If I remember correctly I should work.

Hope this helps

Hi @preshetin
Unfortunately this did not solve my problem and issue persists.
As Buffer is not working in retool, I used btoa()
While using Content -Type 'application/json' I am getting error, media type 'application/json' not allowed, hence above usage of rfc822
Still wondering how to resolve this

Hey @srichint - I noticed on your screenshot pic that you use Upload URI, not Metadata URI (see Gmail docs). Did you use it intentionally? If not, then I would suggest instead of this API URL:

POST https://gmail.googleapis.com/upload/gmail/v1/users/{userId}/messages/send

use this one which is a bit different:

POST https://gmail.googleapis.com/gmail/v1/users/{userId}/messages/send

After making this change, I would suggest to still try to use Content -Type 'application/json' or omit it.

Hope this helps

Hi @preshetin
Thanks a ton for your suggestion worked and I could send mail.

Thanks

1 Like