Reputation: 273
i must download a lot of eMails (ca. 1000 pieces) from my GMail-Account to our fileser and have only access per Browser.
Here is my starting-script:
function myeMailExportToEML() {
var threads = GmailApp.search('Label:TestExportEML');
GmailApp.markThreadsRead(threads);
for (var i=0; i< threads.length; i++) {
var myID = threads[i].getId();
var mySubject = GmailApp.getMessageById(myID).getSubject();
??????????????????
??????????????????
??????????????????
}
}
Does anyone has a tipp for me?
Thanks
Upvotes: 1
Views: 1995
Reputation: 273
I found a solve:
function myExportToGoogleDrive1() {
var threads = GmailApp.search("label:TestExportEML");
GmailApp.markThreadsRead(threads);
Logger.log(threads)
var messagesArray = []
var newFolder = DriveApp.createFolder("dddddddddddddddddddddd").getId();
for (var i=0; i< threads.length; i++) {
var messages = threads[i].getMessages();
for (var j=0; j< messages.length; j++) {
var myID = messages[j].getId();
var msg = GmailApp.getMessageById(myID);
var msgRaw = msg.getRawContent();
var msgBlob = Utilities.newBlob(msgRaw, 'message/rfc822', 'example.eml');
var ssss = DriveApp.getFolderById(newFolder).createFile(msgBlob);
var filename = GmailApp.getMessageById(myID).getSubject();
ssss.setName(filename);
}
}
}
Thanks for your help.
Greetings
Upvotes: 1
Reputation: 3340
To send your emails you first have to obtain each of them and convert them to a blob object with content-type “message/rfc822” (which is the mime type for .eml files) [1]:
In your code you’re obtaining the first message of each thread, because the threadID is the same as the messageID of the first message in that thread, but if a thread has more messages (replies for example) it won’t find them. You need to use the getMessages() function [2] for each thread in order to obtain all the messages.
After you obtain the Message object, you can use getRawContent function [3] to each message in order to obtain the string of data encoded. With this string we can create a Blob object [4], which will be the .eml file.
I’m unable to tell the kind of access that you have from the file server side, this is why I have developed these two different ways to tackle your problem to download/send .eml files depending on what is most convenient for you:
To test if the .eml blob file was correctly created i send it to myself as an attachment using the sendEmail function [5].
The other way is to store all the files in an array (messagesArray) and Fetch a Url of your server to send the array via POST. Using the UrlFetchApp.fetch() function in this case [6].
Here is the full code with both options:
function myeMailExportToEML() {
var threads = GmailApp.search('label:TestExportEML’);
GmailApp.markThreadsRead(threads);
Logger.log(threads)
var messagesArray = []
for (var i=0; i< threads.length; i++) {
var messages = threads[i].getMessages();
for (var j=0; j< messages.length; j++) {
var myID = messages[j].getId();
var msg = GmailApp.getMessageById(myID);
var msgRaw = msg.getRawContent();
var msgBlob = Utilities.newBlob(msgRaw, 'message/rfc822', 'example.eml');
//Sends email with the .eml file attached
GmailApp.sendEmail('RECIPIENT EMAIL', 'Attachment example', 'Please see the attached file.', {
attachments: msgBlob,
name: 'Automatic Emailer Script'
})
messagesArray.push(msgBlob);
}
}
//make a post request to the server sending the .eml file in the request body(I didn't test this part)
var formData = {
'mail': messagesArray
};
var options = {
'method' : 'post',
'payload' : formData
};
UrlFetchApp.fetch('https://example.com/post', options);
}
[1] https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types
[2] https://developers.google.com/apps-script/reference/gmail/gmail-thread#getMessages()
[3] https://developers.google.com/apps-script/reference/gmail/gmail-message#getRawContent()
[6] https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app
Upvotes: 0