I’m working on a Go application that needs to send emails with image attachments using the Gmail API. The email text sends fine, but the image attachment doesn’t work properly. I’ve been reading through the Gmail API docs but can’t figure out the right way to structure the message parts for attachments.
You’re mixing Raw field with structured Payload, which confuses the Gmail API. Pick one approach - either build the entire MIME message manually and use only Raw, or go with structured Payload without Raw. I’ve hit this same problem before. Creating the MIME message from scratch works way better. Make sure you encode the image data properly in base64 and set correct MIME boundaries. Your Content-Disposition header should be “attachment; filename=photo.jpg” - not just the file path. Ditch the .Media() call too - that’s for different attachment handling. Your attachment data goes directly in the message body. The multipart structure needs a proper boundary string, and each part needs its own headers (including Content-Type like image/jpeg for images). Honestly, just use a MIME library instead of building it manually. It handles all the encoding and boundary stuff automatically.
Gmail API attachment handling in Go is a nightmare. Been there, done that.
Your problem? You’re mixing Raw and Payload fields while your MIME structure is broken. Gmail API doesn’t know what to do with that mess.
Honestly? Ditch the manual MIME building. I wasted days on this exact same attachment nightmare with different email providers.
Now I just use Latenode for email automation. It handles all the MIME garbage, base64 encoding, and multipart boundaries automatically. Point it at your image and recipients - done.
Set up workflows from webhooks, database changes, whatever. No more Content-Disposition header hell or boundary string hunting. Emails deliver perfectly with attachments every time.
Saved me 20+ hours of debugging this year. Works with Gmail, Outlook, SendGrid - any email service.
Your message structure is the problem - you’re not creating a proper multipart/mixed container. Right now your MIME structure is malformed. I hit this same issue six months ago building an automated report sender. You need a root multipart section with separate child parts: one for your email text (text/plain or text/html) and another for the attachment (image/jpeg). Each child needs its own headers and body. Ditch the Raw field entirely since you’re using Payload. For the attachment, read your image file, encode it to base64, then stick it in the Data field of MessagePartBody. Set Content-Type to the right MIME type and Content-Disposition to ‘attachment; filename=“photo.jpg”’. Don’t forget a proper boundary string for the multipart container. Gmail API is picky about this structure - get it wrong and attachments just disappear silently.
your attachments are failing cuz ur missing Content-Type headers. each message part needs the right MIME type - like ‘image/jpeg’ for JPGs. u can’t mix Raw and Payload either - the Gmail API doesn’t handle that well. stick with Payload but fix ur multipart boundaries and content headers first.