I am trying to send email with attachment from Angular5 application. My application is working fine without attachments. I am making a call to gmail api (https://www.googleapis.com/gmail/v1/users/userId/messages/send) and passing to,from,subject,message (base64 encoded)in a FormGroup. Now, i would like to send a file along with the message. I am trying to use https://www.googleapis.com/upload/gmail/v1/users/userId/messages/send?uploadType=multipart but facing difficulty as of how to send data to the api call. Gmail api documentation says the input has to be in message/rfc822 format. I tried sending an .eml file to the api call and checked response in postman but i am getting error saying badcontetn, this may be because i just used a sample .eml file in which the content is not encoded. I am like struck here not knowing how to move further.. how do i make it work? How can i convert user inputted details and attachment into .eml file ? is there any other way to make this work?
I could not find the exact solution but i kind of got an workaround for it. Instead of using /upload/gmail/v1/users/userId/messages/send for the file attachment i used the /gmail/v1/users/userId/messages/send api url which is normally used for emails without attachments. The format in which i have sent the request body is
--foo_bar_baz Content-Type: message/rfc822 to: surimallabharat@gmail.com subject: Testing Subject
This is the testing text --foo_bar_baz Content-Type: img/png Content-Disposition: attachment filename="image.png" iVBORw0KGgoAAAANSUhEUgAAABAAAAALCAIAAAD5gJpuAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAA+SURBVHjaYvzPgAD/UNlYEUAAmuTYBgAYhIEgJJmboZgtHbaJKNK8TvozM8LIllwagEY4sIFV1UD/3swngMAaGBn/P3kCVApS7ebG8O/f/x07/n/6BFL36/f/37/+//oFZDPKygJtAAggkIb/YINBqqOjGUxNQeqUlf93dIDV/QLpAWtg+P0bqAEggJhA7gaqBtqoqMjg5PR/+vT/SUn/N2z4//Xr/+XL//Pwgu2BWgJUCxBATCAn/fgJEnVx/Q+05NgxkNzp0/9XrPgvJPR/zZr/ZmZQDX/+AE0HCCCQhv9//4D89OQxMMT+a2uDnKGm9v/SJZCrHj36v28fRAPESQABxALEjGBLGRYv/s/H97+oCOQYIIiM/P/ly/9Fi6CO+QMy9A8DA0AAgTQwg4MMaMD/rq7/vr7/WVlBrv/8GeROiAf+ADWAQgXoHIAAAmlg+v+fQVISbMxfhpMngToZhYUZ+PkZwAaDEDgMgQioGCCAGL+iRiSeOIYAgAADAO/XO1xGA79vAAAAAElFTkSuQmCC --foo_bar_baz--
I have encoded the above body into base64 and replaced + with - and / with _ as needed. It worked fine for me..Hope this at least solves the problem temporarily.