Search code examples
gmailgmail-api

Is it possible to send emails via GMAIL RESTful APIs without having a copy in Sent Folder?


Thank you all in advance.

Is it possible to send an email via the GMAIL RESTful API (https://www.googleapis.com/upload/gmail/v1/users/me/messages/send?uploadType=media) without saving a copy in the sent folder?

I have seen implementations in Outlook (Exchange/REST/Graph APIs) where emails can be sent without having copies in the sent folder. I was wondering if this is possible with the GMail APIs as well.

Again, thanks.


Solution

  • Workaround:

    Currently, sending an email using Gmail API will always create a copy in the SENT folder, but one workaround you can do is:

    1. Get the current timestamp timestamp before sending the message.
    2. Send the message using messages.send.
    3. Call users.messages.list with query parameter labelIds=SENT (note: this is case-sensitive) and ?q=in:sent after:<timestamp> to list the most recent sent message.
    4. Call users.messages.delete and use the ID fetched from the previous call as the parameter.

    References:

    Method: users.messages.list

    Method: users.messages.delete