Lisa Vianti
Lisa Vianti

Reputation: 1

google script for sending emails

MailApp.sendEmail(emailAddress, subject, message);

to send out emails. We have this working on two domains, but on one it's not working. Emails bounce back, and the bounce back screen looks the same, regardless of the recipient, a traffic light with a red light on

Message blocked Your message to [email protected] has been blocked. See technical details below for more information. LEARN MORE

I suspect it's blocked by google. It may have to do with our account status. Manually sent emails go out fine. But emails sent via script do not, they do appear in the sent box, but then bounce back almost instantly.

Does anybody know how to fix this? We have already allowed less secure apps in the g suite security settings but this did not remedy the problem. The g suite access is part of a squarespace deal, so not directly bought from google. The account is around 2 months old now.

Thank you.

function sendEmails2() { 
  var EMAIL_SENT = "EMAIL_SENT"; 
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Press'); 
  var startRow = 2; 
  var numRows = 1; 
  var dataRange = sheet.getRange(startRow, 1, numRows, 100); 
  var data = dataRange.getValues(); 
  for (var i = 0; i < data.length; ++i) { 
    var row = data[i]; 
    var emailAddress = row[11]; 
    var have_email = row[12]; 
    var sendornot = row[16]; 
    var emailSent = row[17]; 
    if (emailSent != EMAIL_SENT && have_email == 1 && sendornot == 1) { 
      var subject = "Story Pitch"; 
      MailApp.sendEmail(emailAddress, subject, message, {'name':'Lisai'}); 
      sheet.getRange(startRow + i, 18).setValue(EMAIL_SENT); 
      SpreadsheetApp.flush(); 
    } 
  } 
}

Upvotes: 0

Views: 126

Answers (1)

Cooper
Cooper

Reputation: 64032

I just made a few minor changes but I think it looks okay to me.

function sendEmails2() { 
  var EMAIL_SENT = "EMAIL_SENT"; 
  var ss=SpreadsheetApp.getActive();
  var sheet=ss.getSheetByName('Press'); 
  var startRow=2; 
  var dataRange=sheet.getRange(startRow, 1, sh.getLastRow()-startrow + 1, sh.getLastColumn()); 
  var data=dataRange.getValues(); 
  for (var i=0;i<data.length;++i) { 
    var row=data[i]; 
    var emailAddress=row[11]; 
    var have_email=row[12]; 
    var sendornot=row[16]; 
    var emailSent=row[17]; 
    if (emailSent!=EMAIL_SENT && have_email==1 && sendornot==1) { 
      var subject= "Story Pitch"; 
      if(MailApp.getRemainingDailyQuota()<1) SpreadsheetApp.getUi().alert("Exceeded Daily Quota");
      MailApp.sendEmail(emailAddress, subject, message, {'name':'Lisai'}); 
      sheet.getRange(startRow + i, 18).setValue(EMAIL_SENT); 
      SpreadsheetApp.flush(); 
    } 
  } 
}

Upvotes: 1

Related Questions