Writing a small script that pulls messages with a label and outputs the subject to a Slack channel.
I've gotten it down ( customized to from someone ) but need further help on where to get the time from.
/* Credit: gist.github.com/andrewmwilson */
function sendEmailsToSlack() {
var label = GmailApp.getUserLabelByName('!urgent');
var messages = [];
var threads = label.getThreads();
for (var i = 0; i < threads.length; i++) {
messages = messages.concat(threads[i].getMessages())
}
for (var i = 0; i < messages.length; i++) {
var message = messages[i];
Logger.log(message);
var output = '*State Change Detected*';
output += '\n*subject:* ' + message.getSubject();
Logger.log(output);
You can use the getDate() method on a message:
var message = messages[0]; // Get first message
Logger.log(message.getDate()); // Log date and time of the message
Related
I am trying to create a script for an autoreply function. I am currently trying to grab each of the emails assigned to a specific label and grab specific details off of each email, however I am receiving an error stating TypeError: threads.getMessages is not a function, even though I have triple checked that this is, indeed, a function of the GMAIL scripting. Have I done something wrong here in this code that may be causing this error?
var labelName = "auto-reply-incoming";
var receivedCount = 0;
// extract emails from label in Gmail
function extractEmails() {
// get all email threads that match label
var receivedSearchQuery = "label:"+labelName+" -is:sent";
var threads = GmailApp.search(receivedSearchQuery, 0, 500);
for (var i=0; i<threads.length; i++) {
//count received emails in label
receivedCount = receivedCount + threads[i].getMessageCount();
//get body of each email
var message = threads.getMessages()[i].getBody();
//get recipient of each email
var recipient = threads.getMessages()[i].getTo();
}
Logger.log(receivedCount);
Logger.log(recipient);
}
The problem occurs because the code is using the threads iterator that might be bigger than the number of messages on certain threads, more specifically, the problem is in the following lines:
//get body of each email
var message = threads.getMessages()[i].getBody();
//get recipient of each email
var recipient = threads.getMessages()[i].getTo();
Here is a quick fix (replace the above, by the following)
var messages = threads[i].getMessages();
for( j = 0; j < messages.length; j++){
//get body of each email
var message = messages[j].getBody();
//get recipient of each email
var recipient = messages[j].getTo();
}
Note: Logger.log(recipient) will log the recipient of the last message of the last thread.
Try this:
function extractEmails() {
var labelName = "auto-reply-incoming";
var receivedCount = 0;
var receivedSearchQuery = "label:" + labelName + " -is:sent";
var ts = GmailApp.search(receivedSearchQuery, 0, 500);
var a = [];
ts.forEach((t, i) => {
let ms = t.getMessages();
ms.forEach((m, j) => {
let msg = m.getBody();
let rec = m.getTo();
a.push({ threadindex: j, messageindex: i, message: msg, recipient: rec })
});
});
Logger.log(JSON.stringify(a));
}
So i'm writing a script to automatically collect emails to a Google spreadsheet. I would like from this email to extract some details like the email address, the name, the address, the phone number. So far i managed to extract the date, the subject, the sender details and the body contents. Below i'll attach the email body and also the code i've written so far! If you have contact me directly at this emails.
This a code i've tried so far:
function onOpen(e) {
var ui = SpreadsheetApp.getUi();
ui.createMenu("KingBoss Menu").addItem("Get Emails", "getGmailEmails").addToUi();
}
function getGmailEmails() {
var label = GmailApp.getUserLabelByName("warranty");
var threads = label.getThreads();
for(var i = threads.length - 1; i >=0; i--) {
var messages = threads[i].getMessages();
for (var j = 0; j <messages.length; j++) {
var message = messages[j];
extractDetails(message);
GmailApp.markMessageRead(message);
}
}
}
function extractDetails(message) {
var dateTime = message.getDate();
var subjectText = message.getSubject();
var senderDetails = message.getFrom();
var bodyContents = message.getPlainBody();
var activeSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
activeSheet.appendRow([dateTime, senderDetails, subjectText, bodyContents]);
}
Have been working on an Apps Script to bring Gmail message information (sender, subject, link, attachment) into a Google Sheet, but not able to bring the title of the attachment over successfully (attachment name field in Google Sheet reads like [Ljava.lang.Object;#127afe9; it appears to be related to the attachments array, but I cannot figure it out. Have tried the solutions offered in What does "[Ljava.lang.Object;#" mean?, but no luck.
function emailToSheet() {
var label = GmailApp.getUserLabelByName('New TODO'); // Find messages with label
var changeLabel = GmailApp.getUserLabelByName('TODO'); // Change message label to
var ss = SpreadsheetApp.openById('xxxx'); // Append to spreadsheet
var sh = ss.getSheetByName("Email"); //On this sheet
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 sent = messages[j].getDate();
var from = messages[j].getFrom();
var subject = messages[j].getSubject();
// Get attachment name
var attachments = messages[j].getAttachments();
var attachmentName = [];
for (var k=0; k<attachments.length; k++) {
var attachmentName = attachments[k].getName();
}
var messageId = messages[j].getId();
var messageUrl = "https://mail.google.com/mail/u/0/#inbox/" + messageId;
ss.appendRow([sent, from, subject, attachmentName, messageUrl])
}
threads[i].removeLabel(label);
threads[i].addLabel(changeLabel);
}
}
I'm thinking that you want this:
var attachmentName = [];
for (var k=0; k<attachments.length; k++) {
var attachmentName = attachments[k].getName();
}
to be this:
var attachmentName = [];
for (var k=0; k<attachments.length; k++) {
attachmentName.push(attachments[k].getName());
}
and maybe this:
ss.appendRow([sent, from, subject, attachmentName, messageUrl])
to be this:
ss.appendRow([sent, from, subject, attachmentName.join(" "), messageUrl])
I am trying to create an Apps Script for Gmail, so that all messages labelled product-related and product-a are forwarded to the producta#gmail.com address, and all messages labelled "product-related" and product-b are forwarded to the productb#gmail.com address.
The script will be launched via a card, so there is no need for more automation.
Here's the code I did:
function testforward1() {
var label = "product-related";
//var interval = 2000;
//var date = new Date();
//var timeFrom = Math.floor(date.valueOf()/1000) - 60 * interval;
var threads = GmailApp.search('label:' + label);
for (var i = 0; i < threads.length; i++) {
if (label == "product-a" && "product-related") {
var recipient = 'producta#gmail.com';
var messages = threads[i].getMessages();
//var attachment = messages[i].getAttachments();
for (var j = 0; j < messages.length; j++) {
var body = messages[j].getBody();
messages[j].forward(recipient, {
htmlBody: body
});
}
}
if (label == "product-b" && "product-related") {
var recipient1 = 'productb#gmail.com';
var messages1 = threads[i].getMessages();
//var attachment1 = messages1[i].getAttachments();
for (var j = 0; j < messages1.length; j++) {
var body1 = messages1[j].getBody();
messages1[j].forward(recipient1, {
htmlBody: body1
});
}
}
}
}
I guess I did something wrong with the variables, but I'm a total beginner with Google Apps Scripts, and I already spent more than 10 hours on this, with no success.
I got no email transferred with this, but the execution gives no error. And I was wondering if the var label = "product-related"; should be replaced with something else?
I will be grateful if you could give me some help on this!
I made it work (I think)! :)
function testforward1() {
var threadsa = GmailApp.search('label: product-related label: product-a');
for (var i = 0; i < threadsa.length; i++) {
var recipient = 'producta#gmail.com';
var messages = threadsa[i].getMessages();
for (var j = 0; j < messages.length; j++) {
var body = messages[j].getBody();
messages[j].forward(recipient,{htmlBody: body});
}
}
var threadsb = GmailApp.search('label: product-related label: product-b');
for (var i = 0; i < threadsb.length; i++) {
var recipient = 'productb#gmail.com';
var messages = threadsb[i].getMessages();
for (var j = 0; j < messages.length; j++) {
var body = messages[j].getBody();
messages[j].forward(recipient,{htmlBody: body});
}
}
}
Now I still have a few more functions to implement, but the basic forwarding function works, and it's the most important.
I need a program for google script that tells me if I have an email address in my contacts or not.
I get many emails from vendors each day and many of them are completely new contacts. I need a program that makes a list of only the new contacts so I can decide which contact group to add them too.
Basic structure of what I'm looking for:
Will run this program every 5 minutes (I can set that up.)
-Checks emails with yellow or blue star for email address of recipient and sender. filters out my email address.
-Checks the email address against all my contacts. If the email address does not appear in my google contacts I need:
Email address, subject line, message text, additional thread message, additional thread message... in seperate columns in a spreadsheet.
I will then go through this information manually to decide which groups to put them in.
Thank you for your help!!!
UPDATE//////////////////////////////update.3
Here's where I'm at. This is working on and off. One time I run it it works, another time it doesn't. REally annoying. If anyone can see any glairing problems let me know. Especially hard to get the if sndr and if rcpnt to return false to run the rest of the program. I've tried about 20 ways!!!
//http://webapps.stackexchange.com/questions/9813/get-e-mail-addresses-from-gmail-messages-received
/////////////NESTED LOOP METHOD
function newEmailAddressList(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("NewEmails");
var range = sheet.getRange("A2:BA");
var addrA;
range.clearContent(); //May need to change this to my delete function to speed things up.
var contact = ContactsApp.getContacts();
for(var i = 0;i < contact.length;i++){
var addrA = [];
var start;
var addresses = contact[i].getEmails();
for(var j = 0;j < addresses.length;j++){
addrA.push(addresses[j].getAddress());
};
};
sheet.getRange('H1').setValue("List Created");
for (var i=0; i<50; i++){
var threads = GmailApp.getInboxThreads(0,50)[i];
var messages = threads.getMessages()[0];
var sndr = messages.getFrom().replace(/^.+<([^>]+)>$/, "$1"); //http://stackoverflow.com/questions/26242591/is-there-a-way-to-get-the-specific-email-address-from-a-gmail-message-object-in
var rcpnt = messages.getTo().replace(/^.+<([^>]+)>$/, "$1");
function contains(addrA, sndr) {
for (var i = 0; i < addrA.length; i++) {
if (addrA[i] === sndr) {
sheet.appendRow("Emails Match");
}else{
var dat = messages.getDate();
//var sndr = messages.getFrom();
//var rcpnt = messages.getTo();
var sub = messages.getSubject();
var msg = messages.getPlainBody();
var info = [dat,sndr,rcpnt,sub,msg];
sheet.appendRow(info); //appendRow only works with sheet class not range class
};
};
};
};
};
Well, I'll give you a start. And keep in my that I've never really worked with GmailApp in the past so all of this information is pretty much readily available to any who opens the code editor clicks on help and then clicks on API Reference. Everything you need to know about Google Scripts is right there organized about as well as it's ever been. They have really improved their documentation since the first time I've looked at it several years ago.
So I'm giving you some functions that will get all of your contact emails, your from emails from your inbox and and your emails.
function myContacts()
{
var s = '';
var br = '<br />';
var contact = ContactsApp.getContacts();
for(var i = 0;i < contact.length;i++)
{
var addrA = [];
var addresses = contact[i].getEmails();
for(var j = 0;j < addresses.length;j++)
{
s += addresses[j].getAddress() + br;
addrA.push(addresses[j].getAddress());
}
}
dispStatus('Contact Emails',s, 800, 400);
}
function MyFroms()
{
var threads = GmailApp.getInboxThreads();
var s = '';
for(var i = 0; i < threads.length; i++)
{
var msg = threads[i].getMessages();
for(var j = 0; j < msg.length;j++)
{
s += msg[j].getFrom() + '<br />';
}
}
dispStatus('My Messages', s , 800 , 400);
}
function MyMessages()
{
var threads = GmailApp.getInboxThreads();
var s = '';
for(var i = 0; i < threads.length; i++)
{
var msg = threads[i].getMessages();
for(var j = 0; j < msg.length;j++)
{
s += 'Message' + j+1 + '<br />';
s += msg[j].getFrom() + '<br />';
s += msg[j].getBody() + '<br />';
}
}
dispStatus('My Messages', s , 800 , 400);
}
function dispStatus(title,html,width,height)
{
var title = typeof(title) !== 'undefined' ? title : 'No Title Provided';
var width = typeof(width) !== 'undefined' ? width : 800;
var height = typeof(height) !== 'undefined' ? height : 400;
var html = typeof(html) !== 'undefined' ? html : '<p>No html provided.</p>';
var htmlOutput = HtmlService
.createHtmlOutput(html)
.setWidth(width)
.setHeight(height);
SpreadsheetApp.getUi().showModelessDialog(htmlOutput, title);
}
It's not a complete answer. But hopefully it will encourage you to jump in get your feet wet and exercise your mental muscles and bring us back at least a partially working skeleton of a program that we can help you get running.