How to stop MailApp.sendEmail from sending myself a copy of the email - google-apps-script

We are using Google Scripts to send emails after Google Forms are submitted. In the last day or two, we started receiving email delivery failures every time a form was submitted and the script ran. It is only unable to deliver messages to one account(sender#mydomain.com), which happens to be the account that the script is running as. However, this account should not be receiving a copy of this form anyways. All of the email addresses that are in the "To" field are getting the email with no issue, we are just trying to find out why we are getting this error message.
We are using the MailApp.sendEmail function and have been for years without any problem. We can't use the GmailApp functions because this account is not able to use Gmail and we've never needed to be able to in order to send emails.
In the script, when I add sender#mydomain.com to the To list, I receive the email and do not get any error messages. When I remove it from the To list, the rest of the recipients continue to receive the email but I get that error message again for sender#mydomain.com.
function formSubmitReply(e) {
var replyToAddr = "no_reply#mydomain.com";
var emailAddress = e.values[2]; // + ", sender#mydomain.com";
//Removed section that creates PDF, stores as the variable pdf
var subject = "Form Request from " + e.values[1];
var messageHtml = "<font size = 3><font face = arial>Form Request from: " + e.values[1];
var messagePlain = messageHtml.replace(/\<br\/\>/gi, '\n').replace(/(<([^>]+)>)/ig, "");
MailApp.sendEmail(emailAddress, subject, messagePlain, {htmlBody: messageHtml, name: "Sender", attachments: pdf, replyTo: replyToAddr});
}

It sounds weird to me that MailApp was working on an account that doesn't have Gmail enabled as MailApp affects the email sent Gmail quota but then I found No reports a 550 recipient rejected from MailApp. The only answer at this time looks to be wrong as MailApp sent emails are saved as sent emails on Gmail mailbox of the effective user that ran the script.

Related

How do I send an autoreply few minutes after the trigger in Google Apps Script?

I have a Google Apps Script code like this where I am sending an autoreply email upon submission of a Google Form. This will send an email immediately after the form submission but is there a way to send this autoreply 5 minutes after the form is submitted in Google Form?
function autoReply(e) {
var values = e.values;
...
var email = value3;
var title = "EMAIL TITLE COMES HERE";
var body = `
DEAR CUSTOMER,
EMAIL BODY COMES HERE
`;
GmailApp.sendEmail(email, title, "", {
htmlBody: body
});
You can use Utilities.sleep, its max allowed value is 5 minutes/300000 milliseconds
Use this method before the lines which take care of the sending part of operation.
Reference
Utilities.sleep

Sending email to a gmail contact list using a Google App Script

Picked up some code from this answer:
Email a group and not individual addresses with Google App Script
Seems to be working but I'm having this problem with the code. When I substitute emails for the send line... I get the following:
Message details
Invalid email: [L<?>;#11773e2
However, the Browser box msg for the email_list has the proper emails.
The code is as follows:
var email_list = [];
var contacts = ContactsApp.getContactGroup('TESTING').getContacts();
Browser.msgBox(contacts);
for(var i in contacts){
email_list.push(contacts[i].getPrimaryEmail());
}
Browser.msgBox(email_list);
MailApp.sendEmail({
to: email_list,
subject: "SUBJECT",
htmlBody: html2,
inlineImages:emailImages});
}
MailApp.sendEmail method expects to key's value to be a single string(composed of a list of emails separated by a ,), while yours is a array. That might be the problem. Try toString() or join():
email_list.join();
If that's not the case, You'd need to examine all emails in the list for any invalids.

Multiple BCC recipients in gmailapp (google apps script)

I would like to send an email to multiple bcc recipents using gmailapp in google apps script.
The following didn't work. I tested it. it seems that only the second recipient bcc2 will get the email. I couldn't find the answer in Google's documents (https://developers.google.com/apps-script/reference/mail/mail-app)
var bcc = "abc#example.com";
var bcc2 = "xyz#example.com";
GmailApp.sendEmail("",Email_Title, "", {'bcc':bcc, 'bcc':bcc2, htmlBody: Msg });
I can get around this by using a for loop
var bcc = ["abc#example.com", "xyz#example.com"];
for (var i = 0; i < bcc.length; i++){}
But this is not ideal to me, as I would find multiple sent email in the sent box, rather than one sent email, with multiple bcc recipients
If you look at the documentation in the link you provided, it states that it needs to be a string, comma-separated list of email addresses. Therefore your code should look like this:
var bcc1and2 = "xyz#example.com,abc#example.com";
GmailApp.sendEmail("",Email_Title, "", {'bcc':bcc1and2, htmlBody: Msg });

MailApp sending gmail account anonymous account

I have a script that sends an email to the person who submitted a form to me. However, I would like to send the email from an "anonymous" account so the confirmation email doesn't come from my account. Is there a way to send it from a different account so it doesn't look like I am manually generating all these emails. Thanks!
My script is something like the following:
if (user = email) {
GmailApp.sendEmail(user, subject, message);
}
Where 'user' is the google account of the person submitting and 'email' is the email address the person submitting wishes to have the confirmation sent to.
Again, is there a way to make the GmailApp.sendEmail(from:'Anonymous') ???
Well, I can't answer fully that it can be anonymous. You could author the app with a "dummy" account to obscure your email address, but if you're on a business or education account that won't really work. Also, you can put a little bit of smoke to distract the recipient by giving the email an "alias" name.
MailApp.sendEmail(userEmail,
"Help Desk Ticket",
"Thanks for submitting your issue. \n\nWe'll start " +
"working on it as soon as possible. \n\nHelp Desk",
{name:"Help Desk"});
}​
The most important part of that is the element in the last line {name:"Help Desk}. This makes your email "name" Help Desk, but you can still see the sending email address if you mouse over it (which is why I recommended a "dummy" email address in the beginning).
You can see the context of the script in this tutorial: https://developers.google.com/apps-script/articles/helpdesk_tutorial
Here is an example of how I used the same thing in a script:
function mailCheatSheet(copyId,userEmail,mrNumber){
var copyId = copyId;
var userEmail = userEmail;
var mrNum = mrNumber;
// Convert temporary document to PDF by using the getAs blob conversion
var pdf = DocsList.getFileById(copyId).getAs("application/pdf");
// Attach PDF and send the email
var subject = "SA Funding request by: "+userEmail;
var body = userEmail +" has submitted a SA Funding Inquiry for " +mrNumber +", which is attached to this email. \n"
+"Please ensure there are no errors before printing. \n"
+"If there are errors, please notify: xyz#abc.com. \n\n";
MailApp.sendEmail(userEmail, subject, body, {name: 'SA Worksheet Helperbot', htmlBody: body, attachments: pdf});
// Delete temp file
DocsList.getFileById(copyId).setTrashed(true);
}
If you just want to make it look like you're not sitting around all day typing out email responses to things (I'd assume so your boss thinks you're doing something useful), then you could add the alias name to your GmailApp method AND add a disclaimer line at the bottom of the email body like this:
var body = userEmail +" has submitted a SA Funding Inquiry for " +mrNumber +", which is attached to this email. \n"
+"Please ensure there are no errors before printing."
+"\n\nThis email was automatically authored by Form Helperbot. If there is an error, please report it to: dummyEmail#xyz.com"
MailApp.sendEmail(userEmail, subject, body,{name: 'Form Helperbot',htmlBody: body, attachments: pdf});
If you want to send from an anonymous account only because you don't want to receive the replies, it might be a better idea to specify a dummy reply-to address in your email notifications.
MailApp.sendEmail(userEmail, subject, body, {name: 'SA Worksheet Helperbot', htmlBody: body, attachments: pdf, replyTo: donotreply#example.com});

Google Apps Script: Multiple recipients in GmailApp?

Can I email multiple recipients through GmailApp.sendEmail()? I've tried storing the recipient addresses as an array, but it doesn't seem to send any of them.
Thanks!
Yes you can.
If you direct yourself towards the Google Apps Spreadsheet services, you can see under the Gmail method that there are advanced parameters.
GmailApp Services
It can look something like this.
GmailApp.sendEmail(recipient, subject, message, {cc: "email1#email.com,email2#email.com"});
I never liked that way, and if I was sending to a varying number of people, and wanted to send them each an email instead of it being one big CC, the work around I found was this:
var emails = ["email1#email.com","email2#email.com","email3#email.com"];
for (var i = 0; i < emails.length; i++) {
GmailApp.sendEmail(emails[i], subject, message);
}
This way you can just simply edit the emails array by adding/subtracting an email and never having to change the actual code that sends the email. Only downside is it sends out X number of emails based on how many addresses you have in the array (if you're worried about hitting the daily cap), but it works none the less.
The recipient argument takes a string, so you can simply write your multiple recipients as a string with a comma in between:
GmailApp.sendEmail('first.last#example.com, your.friend#example.com', ...);
Should work. Note that they will all see each other as recipients.
Edit: I tried it and it works.
When I have multiple addresses, I just reference the list via a range. The below script sample would send to 5 email addresses pulled from C2:C6 on the sheet/tab named Email.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var emailRange = ss.getSheetByName("Email").getRange(2,3,5,1);
var emailAddress = emailRange.getValues();
Logger.log(emailAddress);
var message = 'Message you wish to enter';
var subject = 'Verbiage for Subject Line';
GmailApp.sendEmail(emailAddress, subject, message)