Danesh
Danesh

Reputation: 145

Add labels to Gmail message while using /users.messages/send

While sending an email using the /users.messages/send API, is it possible to assign the message with a label in the same API call?

I'm aware you can use modify to apply a label to the message but I cannot include the gmail.modify scope for this application so using that endpoint isn't an option for me. The documentation says that the send endpoint takes an instance of Message, which allows for a labelIds key.

I've tried making the API request with the JSON body below. It successfully sends the email but it does not assign the label. Since the email is sent, I'm confident the raw message is not the issue. I suspect this endpoint is just ignoring the labelIds part of the body instead.

Does anyone know if it's possible to set the label while using the send endpoint?

{
  "raw": "<RFC 2822 formatted and base64url encoded string>",
  "labelIds": [
    "Label_1"
  ]
}

I obtained the ID, Label_1, from the labels endpoint directly so the ID is correct.

These are the scopes that my client + user are approved to use. That being said, I also included extra scopes to test but the same issue occurs, the labels aren't set even if you have gmail.modify and gmail.compose scopes approved.

userinfo.email
userinfo.profile
https://www.googleapis.com/auth/gmail.labels
https://www.googleapis.com/auth/gmail.send
https://www.googleapis.com/auth/contacts.readonly

Edit: In case this is a bug, I filed a ticket with Google as well.

Upvotes: 2

Views: 211

Answers (1)

Iamblichus
Iamblichus

Reputation: 19319

This is intended behaviour.

This was reported before in Issue Tracker, and the response was:

This behaviour is working as intended, labels can only added for inserted/imported messages. For sent messages, you can use users.messages.modify to update the labels after the message is sent.

Nevertheless, since setting labels when sending a message is possible via UI, I'd say this could be a valid feature request. I'd suggest you to file one.

Reference:

Upvotes: 1

Related Questions