Search code examples
google-apigmailgmail-api

Google API Send Email With Attachment - REST API Call Postman


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


Solution

  • 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: [email protected]
    from: [email protected]
    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.