I'm testing Gmail API here https://developers.google.com/gmail/api/reference/rest/v1/users.drafts/create but is it possible to create a full email with sender, subject, body and load a gmail template and send it through curl command?
This is what I tried but I am getting an empty message:
This is one approach you could take:
Using users.draft list
{
"drafts": [
{
"id": "r-1561xxx",
"message": {
"id": "179891xxx",
"threadId": "179891xxx"
}
}
],
"resultSizeEstimate": 1
}
Then users.draft get
with one of the id
in the previous request:
You can retrieve the current MIME message contained in the draft by calling drafts.get with the parameter format=raw.
(The data is sanitized)
{
"id": "r-1561xxx",
"message": {
"id": "179891xxx",
"threadId": "179891xxx",
"labelIds": [
"DRAFT"
],
"snippet": "Name: Reason: Company:",
"sizeEstimate": 635,
"raw": "TUlNRS1WZXJzaW9uOiAxLjAKRGF0ZTogRnJpLCAxMyBBdWcgMjAyMSAxNjoxMjowOSArMDIwMApNZXNzYWdlLUlEOiA8Q0FGZHVrYm5yZHhReHh4eEBtYWlsLmdtYWlsLmNvbT4KU3ViamVjdDogZHJhZnQxCkZyb206IFNlbmRlciA8c2FtcGxlQGdtYWlsLmNvbT4KVG86IFJlY2VpdmVyIDxzYW1wbGVAZ21haWwuY29tPgpDb250ZW50LVR5cGU6IG11bHRpcGFydC9hbHRlcm5hdGl2ZTsgYm91bmRhcnk9IjAwMDAwMDAwMDAwMGViNGI1ODA1Yzk3MTZmZDIiCgotLTAwMDAwMDAwMDAwMGViNGI1ODA1Yzk3MTZmZDIKQ29udGVudC1UeXBlOiB0ZXh0L3BsYWluOyBjaGFyc2V0PSJVVEYtOCIKCk5hbWU6CgpSZWFzb246CgpDb21wYW55OgoKLS0wMDAwMDAwMDAwMDBlYjRiNTgwNWM5NzE2ZmQyCkNvbnRlbnQtVHlwZTogdGV4dC9odG1sOyBjaGFyc2V0PSJVVEYtOCIKCjxkaXYgZGlyPSJsdHIiPk5hbWU6PGRpdj48YnI+PC9kaXY+PGRpdj5SZWFzb246PC9kaXY+PGRpdj48YnI+PC9kaXY+PGRpdj5Db21wYW55OjwvZGl2PjwvZGl2PgoKLS0wMDAwMDAwMDAwMDBlYjRiNTgwNWM5NzE2ZmQyLS0=",
"historyId": "67xxx",
"internalDate": "1628xxx"
}
}
The raw
attribute is where the message is. It's encoded in base64. Decoded it looks like this:
MIME-Version: 1.0
Date: Fri, 13 Aug 2021 16:12:09 +0200
Message-ID: <[email protected]>
Subject: draft1
From: Sender <[email protected]>
To: Receiver <[email protected]>
Content-Type: multipart/alternative; boundary="000000000000eb4b5805c9716fd2"
--000000000000eb4b5805c9716fd2
Content-Type: text/plain; charset="UTF-8"
Name:
Reason:
Company:
--000000000000eb4b5805c9716fd2
Content-Type: text/html; charset="UTF-8"
<div dir="ltr">Name:<div><br></div><div>Reason:</div><div><br></div><div>Company:</div></div>
--000000000000eb4b5805c9716fd2--
In the guide it says:
Create the email content in some convenient way and encode it as a base64url string. Create a new message resource and set its raw property to the base64url string you just created.
The text should conform to the rfc2822.
So make a plain text email like this (some field you can omit, like date and message ID):
MIME-Version: 1.0
Subject: draft1
From: Sender <[email protected]>
To: Receiver <[email protected]>
Content-Type: multipart/alternative; boundary="000000000000eb4b5805c9716fd2"
--000000000000eb4b5805c9716fd2
Content-Type: text/plain; charset="UTF-8"
Name: TooNetCreation
Reason: TooNetCreation
Company: TooNetCreation
--000000000000eb4b5805c9716fd2
Content-Type: text/html; charset="UTF-8"
<div dir="ltr">Name: TooNetCreation<div><br></div><div>Reason: TooNetCreation</div><div><br></div><div>Company: TooNetCreation</div></div>
--000000000000eb4b5805c9716fd2--
Which encodes to:
TUlNRS1WZXJzaW9uOiAxLjAKU3ViamVjdDogZHJhZnQxCkZyb206IFNlbmRlciA8c2FtcGxlQGdtYWlsLmNvbT4KVG86IFJlY2VpdmVyIDxzYW1wbGVAZ21haWwuY29tPgpDb250ZW50LVR5cGU6IG11bHRpcGFydC9hbHRlcm5hdGl2ZTsgYm91bmRhcnk9IjAwMDAwMDAwMDAwMGViNGI1ODA1Yzk3MTZmZDIiCgotLTAwMDAwMDAwMDAwMGViNGI1ODA1Yzk3MTZmZDIKQ29udGVudC1UeXBlOiB0ZXh0L3BsYWluOyBjaGFyc2V0PSJVVEYtOCIKCk5hbWU6IFRvb05ldENyZWF0aW9uCgpSZWFzb246IFRvb05ldENyZWF0aW9uCgpDb21wYW55OiBUb29OZXRDcmVhdGlvbgoKLS0wMDAwMDAwMDAwMDBlYjRiNTgwNWM5NzE2ZmQyCkNvbnRlbnQtVHlwZTogdGV4dC9odG1sOyBjaGFyc2V0PSJVVEYtOCIKCjxkaXYgZGlyPSJsdHIiPk5hbWU6IFRvb05ldENyZWF0aW9uPGRpdj48YnI+PC9kaXY+PGRpdj5SZWFzb246IFRvb05ldENyZWF0aW9uPC9kaXY+PGRpdj48YnI+PC9kaXY+PGRpdj5Db21wYW55OiBUb29OZXRDcmVhdGlvbjwvZGl2PjwvZGl2PgoKLS0wMDAwMDAwMDAwMDBlYjRiNTgwNWM5NzE2ZmQyLS0=
Which you can send with:
curl --request POST \
'https://gmail.googleapis.com/gmail/v1/users/me/messages/send?key=[YOUR_API_KEY]' \
--header 'Authorization: Bearer [YOUR_ACCESS_TOKEN]' \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--data '{"raw":"TUlNRS1WZXJzaW9uOiAxLjAKU3ViamVjdDogZHJhZnQxCkZyb206IFNlbmRlciA8c2FtcGxlQGdtYWlsLmNvbT4KVG86IFJlY2VpdmVyIDxzYW1wbGVAZ21haWwuY29tPgpDb250ZW50LVR5cGU6IG11bHRpcGFydC9hbHRlcm5hdGl2ZTsgYm91bmRhcnk9IjAwMDAwMDAwMDAwMGViNGI1ODA1Yzk3MTZmZDIiCgotLTAwMDAwMDAwMDAwMGViNGI1ODA1Yzk3MTZmZDIKQ29udGVudC1UeXBlOiB0ZXh0L3BsYWluOyBjaGFyc2V0PSJVVEYtOCIKCk5hbWU6IFRvb05ldENyZWF0aW9uCgpSZWFzb246IFRvb05ldENyZWF0aW9uCgpDb21wYW55OiBUb29OZXRDcmVhdGlvbgoKLS0wMDAwMDAwMDAwMDBlYjRiNTgwNWM5NzE2ZmQyCkNvbnRlbnQtVHlwZTogdGV4dC9odG1sOyBjaGFyc2V0PSJVVEYtOCIKCjxkaXYgZGlyPSJsdHIiPk5hbWU6IFRvb05ldENyZWF0aW9uPGRpdj48YnI+PC9kaXY+PGRpdj5SZWFzb246IFRvb05ldENyZWF0aW9uPC9kaXY+PGRpdj48YnI+PC9kaXY+PGRpdj5Db21wYW55OiBUb29OZXRDcmVhdGlvbjwvZGl2PjwvZGl2PgoKLS0wMDAwMDAwMDAwMDBlYjRiNTgwNWM5NzE2ZmQyLS0="}' \
--compressed
After which it should return an instance of the message and you will have send an email!