How to send email as EML attachment with SendGrid - node.js

I've got a webhook which is parsing emails sent by SendGrid's Inbound Parse, and if any error occurs with processing the email, I want to send the original email as an EML attachment to a notification email address.
I've been using Busboy to parse the multipart/form-data received by the webhook and I receive the full plain cleartext of the email contents itself. Everything is fine there.
When I go to send the email using #sendgrid/mail and set the original email as an EML attachment file, this is where I get issues.
I've tried a variety of content encodings and mimetypes, but haven't found one that works yet as a EML file. I've tried text/plain, message/rfc822 and application/octet-stream with plain text (which results in error from SendGrid API), and base64 encoded text.
In fact, it gets weirder. When I send the email attachment as text/plain with filename ending in .txt, everything seems to be good. When I download the attachment and rename to .eml then it views perfectly in my Mail app.
However, when I send the email attachment as text/plain, message/rfc822 or application/octet-stream with filename ending in .eml, this is where I get inconsistencies. In the email I sent I can see that the filename now ends with .eml.eml and opening the file in my Mail app shows me base64 encoded text (decoding the text gives me the original email contents that I had to encode in order to allow the endpoint to send as an attachment). Viewing the file contents in a text editor looks like this:
Content-Type: text/plain
MIME-Version: 1.0
VGhpcyBpcyB3aGVyZSB0aGUgYmFzZTY0IGVuY29kZWQgZW1haWwgdGV4dCBnb2Vz
My current conclusion is that the SendGrid /v3/mail/send API endpoint is treating attachments with .eml extension in the filename differently and encapsulating the base64-encoded contents within another .eml wrapper. It doesn't seem possible to set an attachment's content to a plain text string as I get a 400 Bad Request from the endpoint if its not base64 encoded.
I haven't been able to find any official documentation about this particular behaviour on this endpoint. The closest I got was this GitHub issue (dated 2018), but it doesn't seem to solve my issue.

Related

Sending a mail with attachment via Gmail API using Postman

What I am doing
I am strictly following the Gmail API guide - https://developers.google.com/gmail/api/guides/sending
https://developers.google.com/gmail/api/reference/rest/v1/users.messages/send
From the sample code given for both sending a simple mail and mail with an attachment. I learned from these codes that we need to send Base64 encoded value as "raw" as per RFC-2822.
Sending mail without attachment
I am able to encode the below RFC 2822 format in Base64 and send mail successfully.
From: automationreportbyamod#gmail.com
To: automationreportbyamod2#gmail.com
Subject: Saying Hello
This is a message just to say hello.
So, "Hello".
Sending mail with attachment
From the example code with the attachment, I see that we need to encode email content with the attachment in Base64. But if we want to do the same using Postman, how can I encode a simple format with a file to be uploaded?
Note - As explained here, I have tried the raw as text and passed the attachment file as Base64 and that is working as well. But I want to make it work as shown in the example code here using Postman.

SendGrid dynamicTemplateData - HTML content and plaintext content in one email?

I'm trying to figure out how to send a plain text version of my email, alongside the html content generated by dynamicTemplateData and my dynamic template.
Including text as a sibling to dynamicTemplateData results in a broken email.
It looks like SendGrid is including a text/plain email alongside the text/html content, but it's empty.
Does anybody know how to inject a text/plain version of my email?
Twilio SendGrid developer evangelist here.
When you preview your dynamic template, you can check it out in Desktop, Mobile and Plain Text versions. You can set SendGrid to automatically create a plain text version of the email from your HTML variant, or you can choose to write your own plain text version.
See the screenshot below for more detail:

Spam Assassin reports PP_MIME_FAKE_ASCII_TEXT MIME text/plain claims to be ASCII but isn't

When emailing via Sendgrid SMTP from Gmail using https://www.mail-tester.com/ it reports SpamAssassin says "PP_MIME_FAKE_ASCII_TEXT MIME text/plain claims to be ASCII but isn't".
Sending the same content from the Gmail address via Gmail's SMTP servers to mail tester does not give this error.
What is going on? How do I fix this error?
Update 2021-03-07 Sendgrid say they require the "SMTP payload" sent from Gmail to their SMTP server. Any suggestions how I can get this?
Thanks
An email entity that claims to be 7bit text contains 8bit characters.
Maybe Google is fixing it for you and sendgrid isn’t.
Check the source of the email you are sending and compare it with what you receive from Google.

Attached EML files in emails sent via GMAIL-APIs are not viewable in GMAIL

I am currently developing a gmail addon. The addon sends an email, with an attached eml file, to a particular recipient. Unfortunately, the recipient is not able to open the eml file attached with the email I sent via GMAIL API. If they click on the attached eml file they will receive the message "An error occurred while loading the attached message."
I found two issues, which might be related to this issue, when I analyzed the data I saw when I clicked "show original" on the email. One is that all emails I sent via GMAIL API have the "Received from xxxxxxxxxxx named unknown by gmailapi.google.com with HTTPREST" header. And that the entire base64-encoded eml file is missing in the content of the email. Please see image below :
Is this related to Emails sent via GMAIL API are flagged as Phishy? Or am I missing or doing something wrong? If I set the "attachment-type" to "text/plain" then the base64-encoded eml file data is present and viewable when I "show original" the email. Thank you all in advance.
#GovZ you can use MailApp.send rather than GmailApp.send, along with setting the content-type to "message/rfc822" and the email should preview correctly.
Details here: https://issuetracker.google.com/issues/155736170

PHPMailer doesn't load HTML email with attachment on iPhone

I'm using PHPMailer to send an HTML email when someone requests a brochure from a website.
My issue is that when I send the email without the attachment the email opens and displays the HTML email correctly. As soon as I add the attachment it doesn't load the HTML email and just shows text. NB: I haven't uploaded any code as the email itself sends with or without the attachment, so didn't think you guys would need it.
So:
1) Email sends OK with or without the attachment
2) With the attachment, it arrives (with the attachment) but doesn't show HTML email
3) Without the attachment the HTML is displayed correctly.
This specifically relates to an iPhone and it asks me to download the remainder of the email.
I know this should work OK as I know a competitors website that uses the exact same process and their email loads perfect everytime!
Any help would be useful.

Resources