pass emails instead of call function; parse no email from

This commit is contained in:
Niraj D 2017-12-27 11:19:26 -05:00
parent 9e66a083ea
commit 968543d3cc
2 changed files with 19 additions and 12 deletions

View File

@ -14,7 +14,6 @@
<tr> <tr>
<td><div style="height: 10px;"></div></td> <td><div style="height: 10px;"></div></td>
</tr> </tr>
<? var data = getEmails() ?>
<? for (var i = 0; i < data.length; i++) { ?> <? for (var i = 0; i < data.length; i++) { ?>
<tr> <tr>

View File

@ -48,19 +48,24 @@ function getEmails() {
if (threads[i].getLastMessageDate()>maxDate){ if (threads[i].getLastMessageDate()>maxDate){
var d = {} var d = {}
d.subject = threads[i].getFirstMessageSubject() d.subject = threads[i].getFirstMessageSubject()
d.from = threads[i].getMessages()[0].getFrom().replace(/\"|<.*>/g,'') var from = threads[i].getMessages()[0].getFrom()
d.from = from.replace(/\"|<.*>/g,'')
d.date = threads[i].getLastMessageDate() d.date = threads[i].getLastMessageDate()
d.email = threads[i].getMessages()[0].getFrom().match(/<(.*)>/)[1] if (from.match(/<(.*)>/)!==null){
d.email = from.match(/<(.*)>/)[1]
} else {
d.email = from
}
d.uns = null d.uns = null
var uns = threads[i].getMessages()[0].getRawContent().match(/^list\-unsubscribe:(.|\r\n\s)+<(https?:\/\/[^>]+)>/im); var uns = threads[i].getMessages()[0].getRawContent().match(/^list\-unsubscribe:(.|\r\n\s)+<(https?:\/\/[^>]+)>/im);
Logger.log(uns+"\n\n")
if(uns) { if(uns) {
d.uns = uns[uns.length-1] d.uns = uns[uns.length-1]
} else { } else {
var rex = /.*?<a[^>]*href=["'](https?:\/\/[^"']+)["'][^>]*>(.*?[Uu]nsubscribe.*?)<\/a>.*?/gi
var rex = /.*?<a[^>]*href=["'](https?:\/\/[^"']+)["'][^>]*>(.*?)<\/a>.*?/gi body_t = threads[i].getMessages()[0].getBody()
while(u = rex.exec(threads[i].getMessages()[0].getBody())){ while(u = rex.exec(body_t)){
Logger.log("regmatch" + u.length)
if(u[0].toLowerCase().indexOf('unsubscribe')!==-1){ if(u[0].toLowerCase().indexOf('unsubscribe')!==-1){
for(var j = u.length-1; j >=0; j--){ for(var j = u.length-1; j >=0; j--){
if(u[j].substring(0,4)=="http"){ if(u[j].substring(0,4)=="http"){
@ -71,6 +76,7 @@ function getEmails() {
if(d.uns){ if(d.uns){
break break
} }
} }
} }
} }
@ -85,12 +91,14 @@ function getEmails() {
//run every day to send summary email including emails from last 24 hours //run every day to send summary email including emails from last 24 hours
function noneroll() { function noneroll() {
if(getEmails().length > 0) { emails = getEmails()
if(emails.length > 0) {
var date = Utilities.formatDate(new Date(), Session.getScriptTimeZone(), 'yyyy-MM-dd'); var date = Utilities.formatDate(new Date(), Session.getScriptTimeZone(), 'yyyy-MM-dd');
var subject = "Bulk Emails: " + date; var subject = "Bulk Emails: " + date;
var hB = HtmlService var t = HtmlService
.createTemplateFromFile('email') .createTemplateFromFile('email');
.evaluate().getContent() t.data = emails;
var hB = t.evaluate().getContent();
MailApp.sendEmail({ MailApp.sendEmail({
to: Session.getActiveUser().getEmail(), to: Session.getActiveUser().getEmail(),
subject: subject, subject: subject,