Gmail API发送EML附件无法打开问题咨询
Hey there, let's work through why your EML attachments are failing to open for recipients, and address your questions one by one:
First: Is this related to phishing marking?
Short answer: Probably not. That auto-added Received from xxxxxxxxxxx named unknown by gmailapi.google.com with HTTPREST header is standard for emails sent via the Gmail API—Google adds this to track the source of the message, and it doesn’t trigger phishing flags on its own. If your email were flagged as phishing, recipients would likely see it in their spam folder or get a warning about the message itself, not just an error opening the attachment. Your core issue is tied to how you’re formatting the attachment, not phishing filtering.
Why does setting attachment-type to text/plain fix the issue?
EML files are essentially plain-text MIME messages under the hood. When you mark the attachment as text/plain, Gmail treats it like any other text file: it decodes the base64 data and displays it directly as text content. This is why you see the full base64 data and can view it—but this isn’t the correct way to send an EML attachment (recipients expect to open it as a standalone email, not a text blob).
The real issue: Incorrect attachment formatting for EML
The error "An error occurred while loading the attached message" almost always happens when Gmail can’t properly parse the EML attachment. Based on your observation that the base64 data is missing when using the "correct" attachment type, here are the key fixes you need to make:
Use the right MIME type for EML
EML attachments require themessage/rfc822MIME type—this is the standard that tells Gmail "this is a standalone email message file." If you were using any other type (likeapplication/octet-streamor a custom type), Gmail won’t recognize it as an EML file and will fail to load it.Ensure you’re using Base64url encoding (not standard Base64)
The Gmail API requires attachment data to be encoded in Base64url (a variant of Base64 that replaces+with-,/with_, and removes trailing=characters). If you’re using standard Base64, the API might truncate or corrupt the data, leading to missing content in the final email.Verify your attachment structure in the API request
Double-check that your attachment part is structured correctly in themessages.sendrequest body. It should look something like this:"payload": { "parts": [ { "filename": "your-attachment.eml", "mimeType": "message/rfc822", "body": { "data": "BASE64URL_ENCODED_EML_CONTENT_HERE" } } ] }
Next steps to test
- Re-encode your EML file using Base64url instead of standard Base64.
- Update the attachment’s
mimeTypetomessage/rfc822and keep the.emlfilename extension. - Send a test email and check the raw message content again—confirm the attachment’s
datafield contains the full, correct Base64url-encoded string.
Once you fix these formatting issues, recipients should be able to open the EML attachment normally.
内容的提问来源于stack exchange,提问作者GovZ

