Hello, Guys welcome back to learn How to Download Gmail Messages as EML Files in Google Drive. This Google Script can assist you in downloading your Gmail email messages in EML format to your Google Drive.
What is the EML Format
Because it conforms to the RFC 822 standard and can thus be viewed natively within Apple Mail and Microsoft Outlook, the.eml file format is popular for moving emails from one email software to another. You may also open EML files in Google Chrome by dragging them from your desktop to a new browser tab.
The email content (email body, header, and encoded graphics and attachments) is stored in EML files as plain text in MIME format.
Download Gmail message as EML Files
Open any email thread in Gmail, go to the 3-dot menu, and select “Download Message” from the menu. It will store your current email message as an eml file to your desktop.
Apps Script, on the other hand, can help you automate the procedure and download many emails as eml files to your Google Drive.
const downloadEmails = () => {
const sender = '[email protected]';
const threads = GmailApp.search(`from:${sender}`).slice(0, 10);
threads.forEach((thread) => {
const subject = thread.getFirstMessageSubject();
const [message] = thread.getMessages();
const rawContent = message.getRawContent();
const blob = Utilities.newBlob(rawContent, null, `${subject}.eml`);
const file = DriveApp.createFile(blob);
Logger.log(subject, file.getUrl());
});
};
The script searches for emails from the specified sender gets the first email message and downloads it your Google Drive.
JavaScript Objects Quick Reference
Forward Gmail as EML Attachment
If you plan to forward an email message as an attachment, the.eml format is suggested since it saves all of the formatting and attachments from the original email thread in a single file that can be attached to the email.
const forwardEmail = () => {
const messageId = '123';
const message = GmailApp.getMessageById(messageId);
const rawContent = message.getRawContent();
const blob = Utilities.newBlob(rawContent, null, `email.eml`);
GmailApp.sendEmail('[email protected]', 'This email contains an eml file', '', {
attachments: [blob],
});
};
Create JSON Web Token (JWT) with Google Apps Script