I’m working with the Gmail API using Ruby and running into problems with recipient setup. The draft creation works fine but I can’t get the To field to populate correctly.
I’ve looked at existing drafts through the API explorer and tried copying their structure. Here’s my current approach but the recipients never show up:
You’re mixing raw and structured message formats in your API call. When you use the raw parameter, Gmail ignores the payload structure - including your headers array. I ran into this exact issue last year building something similar. You need to build the entire email as a properly formatted MIME string before base64 encoding it. Put the headers directly in the message body:
Then just pass the raw parameter without the payload structure. Gmail will parse the MIME headers from your raw string and populate the recipient fields correctly. Don’t forget proper CRLF line endings (\r\n) - MIME standard requires them.
davidw’s right - you can’t mix raw with payload headers. also, double-check your mime formatting or gmail will ignore everything. I test with a basic string first like “To: [email protected]\nSubject: test\n\nHello” before base64 encoding to make sure it actually works.
Been wrestling with Gmail API for months and this got me too. Yeah, it’s the raw/payload conflict everyone’s talking about, but here’s what really screwed me over - Gmail silently drops recipients if your MIME structure isn’t perfect. I manually build the headers string with proper spacing first, then validate it works before encoding. This format never fails me: “To: #{recipient}\nFrom: #{sender}\nSubject: #{subject}\n\n#{body}”. Oh, and if you’ve got multiple recipients or CC/BCC fields, add them as separate header lines in your raw message string. Don’t try using the payload structure for that.