2

I want to create an email/message throw Google API. user messages send

I managed to create a simple email (only text) using this endPoint

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

and inserting in the body only the "raw" field with the Base64 Encoded string that contains this info eg:

content of the base64 "raw" string

Postman Call


Now I'm trying to send an Email with some attachments and i can't get it to work. I found only example with Java/Javascript libraries but I want to made it throw standard Rest Api Call (Now i'm using Postman to test this endpoints).

First of all , Have I to use https://developers.google.com/gmail/api/reference/rest/v1/users.messages/send OR POST https://gmail.googleapis.com/upload/gmail/v1/users/{userId}/messages/send ?

Can you leave an example of an email with a body text and two attachments (for example two pdf) ? Thank you

Morfinismo
  • 4,985
  • 4
  • 19
  • 36
Filp99
  • 23
  • 5
  • try checking this https://developers.google.com/gmail/api/guides/sending – Linda Lawton - DaImTo May 11 '22 at 16:41
  • The guidelines are about Java or Python libraries. I need the HTTP plain call. – Filp99 May 12 '22 at 07:15
  • Oh here's some more documentation https://developers.google.com/gmail/api/guides/uploads This shows some of your raw http. BTW Im curious, How do you plan on getting postman to load a file as a stream? I didnt know it could do that. – Linda Lawton - DaImTo May 12 '22 at 07:44
  • i would upload a file (eg: pdf) using base64 encoding , is it possible ? Now i'm using Postman to test these APIs but then i will use Delphi to interact with them. – Filp99 May 12 '22 at 10:17
  • In the simple upload example of developers.google.com/gmail/api/guides/uploads , what is the format of Email Message data ? Do I have to load it directly in bytes? – Filp99 May 12 '22 at 10:19

1 Answers1

2

If you are using postman, I suggest you to follow these steps to make it work:

1.) The endpoint to use is: https://gmail.googleapis.com/upload/gmail/v1/users/me/messages/send?uploadType=multipart

2.) The content type header of the request should be: Content-Type: message/rfc822

3.) The body of the request should be: raw -> text

4.) The content of the body should have the following format:

Content-Type: multipart/mixed; boundary=foo_bar_baz
MIME-Version: 1.0
to: recipient@email.com
from: sender@email.com
subject: POSTMAN Rest API Execution

--foo_bar_baz
Content-Type: text/html; charset="UTF-8"
MIME-Version: 1.0

<h1>What is Lorem Ipsum?</h1>
<p style="color: darkred">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum<p>

--foo_bar_baz
Content-Type: application/pdf
MIME-Version: 1.0
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="Attachment_file.pdf"

JVBERi0xLjQKJdPr6eEKMSAwIG9iago8PC9UaXRsZSAoUHJvamVjdCBwcm9wb3NhbCkKL1Byb2R1Y2VyIChTa2lhL1BERiBtMTAzIEdvb2dsZSBEb2NzIFJlbmRlcmVyKT4+CmVuZG9iagozIDAgb2JqCjw8L2NhIDEKL0JNIC9Ob3JtYWw+PgplbmRvYmoKNyAwIG9iago8PC9UeXBlIC9YT2JqZWN0Ci9TdWJ0eXBlIC9JbWFnZQovV2lkdGggMTIwMAovSGVpZ2h0IDEyCi9Db2xvclNwYWNlIC9EZXZpY2VSR0IKL0JpdHNQZXJDb21wb25lbnQ==

--foo_bar_baz--

As a side note, I also followed the REST API documentation but I was getting all kind of error messages because I was confused. However, I found an old question that was able to help me to formulate the correct way of using the api... you might want to check it --> Mail attachment wrong media type Gmail API

If you have time, you might also want to read this --> https://www.rfc-editor.org/rfc/rfc2046. Reading it helped me overcome the confussion I was going through and gave me clarity as to why the above steps worked.

Morfinismo
  • 4,985
  • 4
  • 19
  • 36
  • 1
    Hi Morfinismo, Thank you so much. I tried this with a small pdf file and it work fine. In few days i will try it more thoroughly (eg: with several attachments and with bigger size). – Filp99 May 12 '22 at 16:05
  • @Thommy99 just keep in mind that there is an attachment limit of 25MB, as per the documentation https://support.google.com/mail/answer/6584 – Morfinismo May 12 '22 at 18:01
  • I tried to send multiple attachments in one email message and it works fine, but I have another question. How can I send a Reply Email (with attachments) to a specific thread-id with this format ? – Filp99 May 13 '22 at 08:50
  • @Thommy99 That, my friend, is another question... and it requires some explanation and probably some testing on my end too. I suggest you to make another question for that and if I have time, I might be able to help you. – Morfinismo May 13 '22 at 13:41
  • Ok, I will create a new question. In the meanwhile I tried the "request 3" of the confirmed answer of this link https://stackoverflow.com/questions/33171769/gmail-api-in-reply-to-not-workinggoogle-not-handling-on-one-side but i get the error 400 recipient address required. – Filp99 May 13 '22 at 15:31
  • Hi @Morfinismo, this is the link of my new question: https://stackoverflow.com/questions/72249932/google-api-send-reply-email-with-attachment-rest-api-http-call – Filp99 May 15 '22 at 16:00