Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
130
rated 0 times [  135] [ 5]  / answers: 1 / hits: 42769  / 9 Years ago, thu, october 1, 2015, 12:00:00

this is the first script i try to write from scratch. It's been no good up to now so i'm going to ask for some help.



Case: I recieve e-commerce confirmation emails from e-commerce sites no reply email address. In the email's body they send email address from buyers. I want to send an automated mail to the body's email address.



How i plan to do this (any suggetions to eliminate steps will be thanked).




  1. Use a rule to tag incoming emails with a unique tag.


  2. Use that tag to identify emails in gmail with a script, go one by one and extract the info i need. Use regex with the emails body content to extract the email address i need to send the automated emails. Plan is to get: subject, date, email from body.


  3. Write all that info to a spreadsheet.


  4. Get rid of unique tag info to prevent duplicate runs.


  5. Then use form mule addon to send emails from the spreadsheet.




So far, i've dealt with steps 1 (easy), and been stuggling with steps 2 and 3 (im not a coder, i can read, undestrand and hack. writing from scratch is a completely different thing). Ive dealt with 4 before i think this is the best way to deal with it.



With the script i extract info to the spreadsheet, with the addon i use the info from the spreadsheet to send emails.



This is the code ive written so far. I've left the regex part for later cause i cant even write anything into the spreadsheet yet. once i get that working, ill start working in the regex and remove the label aspects of the script.



function myFunction() {
function getemails() {
var label = GmailApp.getUserLabelByName(Main tag/subtag);
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 message=messages[j];
var subject=message.getSubject();
tosp(message);
}
}
}

function tosp(message){
var body=message.getBody()
var date=message.getDate();
var subject=message.getSubject();
var id= my spreasheet id;
var ss = SpreadsheetApp.openById(id);
var sheet = ss.getActiveSheet();
sheet.appendRow(subject,date,body);

}
}


Any help would be appreciated.



Thanks
Sebastian


More From » regex

 Answers
1

Following is the code I wrote and tested that performs the steps 2, 3 and 4 mentioned by you perfectly well.



function myFunction() {

var ss = SpreadsheetApp.getActiveSheet();

var label = GmailApp.getUserLabelByName(MyLabel);
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();

ss.appendRow([msg, sub, dat])
}
threads[i].removeLabel(label);
}
}


One of the faults in your code was that the appendRow function accepts an array of elements specified within [ ] brackets.



Depending on where you're attaching this script, your line of code:



var ss = SpreadsheetApp.openById(id);


is not necessary if the script is being written in the script editor of the Spreadsheet where you want these emails to be logged. However, if there are multiple sheets in that spreadsheet, you can replace my line



var ss = SpreadsheetApp.getActiveSheet();


by



var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(Sheet1);


Another suggestion is that the current code will give you messages in HTML format. Hence, if you want to get the message in plain text as you see it, use:



var msg = messages[i].getPlainBody();


Now you can write another function for regex and pass the message msg to that. Hope this helps!


[#64873] Tuesday, September 29, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
brennonr

Total Points: 124
Total Questions: 101
Total Answers: 101

Location: Estonia
Member since Wed, Jun 8, 2022
2 Years ago
;