Get email size using google apps script - google-apps-script

I have written this to extract last 500+ message from gmail into spreadsheets.
function myFunction() {
var ss = SpreadsheetApp.getActiveSheet();
var threads = GmailApp.getInboxThreads();
for (var i=0; i<threads.length; i++)
{
var messages = threads[i].getMessages();
for (var j=0; j<messages.length; j++)
{
var frm = messages[j].getFrom();
var to = messages[j].getTo();
var dat = messages[j].getDate();
ss.appendRow([frm, to, dat])
}
}
}
Is it possible to get the size of each email (with attachment and without attachment)? When I look into documentation of Gmail message method, there is no any function I could use.

Thanks to #s1c0j1 and #TheMaster. I could get the size of an email by using this function.
var size = Utilities.newBlob(messages[j].getRawContent()).getBytes().length;

the user.messages.get returns a user.messages.response which contains a parameter called "sizeEstimate"
sizeEstimate integer Estimated size in bytes of the message.
You will need to remember that this is an estimate.

Related

Count all eMails from a Label

i have create this script:
function myTest() {
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var label = GmailApp.getUserLabelByName("Label1");
var threads = label.getThreads();
var countMail = 0;
//get first message in first thread
for (var i = 0; i < threads.length; i++) {
var messages=threads[i].getMessages();
countMail = countMail + messages.length;
}
ss.getRange(1,1).setValue(countMail);
}
it runs nearly perfect. Here I get all eMails back which are connected with this treads (marked with Label1 or not).
Does anyone can share a simply script how I can count all eMails which are "realy" marked with the Label.
Thanks
Try this:
function labelMsgs(){
var labels = Gmail.Users.Labels.list("me").labels;
var lblId;
for (var i = 0; i < labels.length; i ++){
if (labels[i].name == "Label1"){
lblId = labels[i].id;
break;
}
}
var optionalArgs = {
"labelIds" : lblId,
"maxResults" : 200
}
var messages = Gmail.Users.Messages.list("me", optionalArgs).messages;
}
This method uses the Gmail API directly, for this you'll need to enable Google Advanced Services, it will return a list of all messages tagged with the "Label1" label. You can play around with maxResults and with the pageToken to see more results, but this is the general approach.

Extract the exact subjects and update the google sheets using apps script

I have found a below mentioned apps script to get the subject name of the emails from the particular label and update the google sheets.
function getSubjects() {
var sheet = SpreadsheetApp.getActiveSheet();
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var label = GmailApp.getUserLabelByName("Test");
var threads = label.getThreads();
var row = 2;
for (var i = 0; i < threads.length; i++) {
var messages=threads[i].getMessages();
for (var m=0; m < messages.length; m++) {
sheet.getRange(row,1).setValue(messages[m].getSubject());
row++;
}
}
};
On daily basis, I will receive more than 100 emails with the same subject lines with small modifications. So, I need to get in the google sheets only the emails that have a subject line which includes the word "Pending".
I am hoping someone can help me make this example work.
Use the Gmail API for Apps Script
It features the method Users.messages.list which allows you to
triage the messages by both labels and query simultaneously.
Sample:
function myFunction() {
Logger.log(Gmail.Users.Messages.list('me', {'labelIds': 'Label_1003148783259556236','q': 'subject:Pending' }));
}
Make sure you enable Gmail API from the Apps Script UI Resources-> Advanced Google Services-> Gmail API
Never-mind. I have figured it out. Below is the updated code which is working as expected.
function getSubject() {
var sheet = SpreadsheetApp.getActiveSheet();
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var threads = GmailApp.search('subject: Successfully Completed is:Test/2019_08');
var row = 2;
for (var i = 0; i < threads.length; i++) {
var messages=threads[i].getMessages();
for (var m=0; m < messages.length; m++) {
sheet.getRange(row,1).setValue(messages[m].getSubject());
row++;
}
}
};

How do I pull the contents of a thread matching search criteria into a Google sheet?

I currently have a Google Apps Script running a spreadsheet that pulls the correct Email I am querying for. However, I can only get the original email body contents, when I am actually trying to pull a thread reply in the actual email conversation.
Does anyone know if this is possible or how to go about doing so?
My current script only pulls the initial email from the search query, but this is not the intention.
function myFunction() {
// Use sheet
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
// Gmail query
var query = "subject:Attendance Shed";
// Search in Gmail, bind to array
var threads = GmailApp.search(query);
// Loop through query results
for (var i = 0; i < threads.length; i++)
{
// Get messages in thread, add to array
var messages = threads[i].getMessages();
// Used to find max index in array
var max = messages[0];
var maxIndex = 0;
// Loop through array to find maxIndexD = most recent mail
for (var j = 0; j < messages.length; j++) {
if (messages[j] > max) {
maxIndex = j;
max = messages[j];
}
}
// Find data
var mId = messages[maxIndex].getId() // ID used to create mail link
var from = messages[maxIndex].getFrom();
var cc = messages[maxIndex].getCc();
var time = threads[i].getLastMessageDate()
var sub = messages[maxIndex].getSubject();
// Write data to sheet
ss.appendRow([from, cc, time, sub, 'https://mail.google.com/mail/u/0/#inbox/'+mId])
}
}
I expect it to be able to retrieve a specific thread using the search query given and output it to a Spreadsheet. Currently it only pulls the initial email.
Try this code after the var messages = threads[i].getMessages(); line:
…
for (var j = 0; j < messages.length; j++) {
// Find data
var mId = messages[j].getId() // ID used to create mail link
var from = messages[j].getFrom();
var cc = messages[j].getCc();
var time = threads[i].getLastMessageDate()
var sub = messages[j].getSubject();
// Write data to sheet
ss.appendRow([from, cc, time, sub, 'https://mail.google.com/mail/u/0/#inbox/' + mId])
}
ss.appendRow();
This will get each message’s information, and then append an empty row to separate between each thread.

How to get gmail attachment name and paste it into sheets google-apps

I'm trying to get the filename of an attachment on Sheets using google apps scripts.
It gives me an error : "TypeError: Cannot find function getName in object GmailAttachment,GmailAttachment"
Any Ideas??
Here is my code :
function myFunction() {
var ss = SpreadsheetApp.getActiveSheet();
var label = GmailApp.getUserLabelByName("Label");
var threads = label.getThreads();
for (var i=0; i<threads.length; i++)
{
var messages = threads[i].getMessages();
for (var j=0; j<messages.length; j++)
{
var msg = messages[j].getBody();
var sub = messages[j].getSubject();
var dat = messages[j].getDate();
var attachment = messages[j].getAttachments().getName();
ss.appendRow([msg, sub, dat]);
ss.appendRow([attachment])
}
//threads[i].removeLabel(label);
}
}
Thanks!!
getAttachements() method return a array of GmailAttachement. You must go throught the array to use the GmailAttachement.getName() method.

GoogleScript Mail Parser - Only retrieve emails for past X days

Built a basic email parser so that I can get in gSheet my emails, however though, I have > 50k emails and I only need to retrieve them for the past 7 days each time I'll run the parser. Looked around but weren't able to find any similar issue
This is the script I have
var sheet = SpreadsheetApp.getActiveSheet();
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
function getEmails() {
var label = GmailApp.getUserLabelByName("HOUSE_PHONELEAD");
var inc = 100;
var start = 0;
var row = 2;
var threads = label.getThreads();
for (var i = 0; i < threads.length; i++) {
var messages=threads[i].getMessages();
for (var m=0; m < messages.length; m++) {
sheet.getRange(row,1).setValue(messages[m].getPlainBody());
row++;
Utilities.sleep(1000);
}
}
}
function onOpen() {
var menuEntries = [ {name: "Load Emails", functionName: "getEmails"} ];
spreadsheet.addMenu("Email", menuEntries);
}
Try to use
var threads = GmailApp.search('newer_than:7d');
instead of
var threads = label.getThreads();
search method allow to pass a string query :
https://developers.google.com/apps-script/reference/gmail/gmail-app#searchquery
the query string must be a gmail specific operator :
https://support.google.com/vault/answer/2474474?hl=en