Google Apps Script - Email trigger not working outside domain - google-apps-script

I have created a google form, went to response sheet - script editor - written script and activated a trigger to send email upon form submit, 'To' is as per the value entered in google form, 'cc' is by default 'raajesh#master-brains.com'. This is not working.
But, when I modify the script as 'To' and 'Cc' to raajesh#master-brains.com, I am receiving the trigger email. Why is it so? Is it working only within my domain of master-brains.com? What should I do to enable it for gmail / other domain accounts?
function myFunction(onboard) {
var conNo = onboard.values[3];
var conEmail = onboard.values[4];
var subject = 'Welcome Onboard as Master-Brains Connector | '+conName;
var htmlBody = 'Greetings from Master-Brains!!';
MailApp.sendEmail('raajesh#master-brains.com', subject, htmlBody, {'htmlBody':htmlBody, cc: 'raajesh#master-brains.com'})
}

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

Auto send Email from Sheets Using Google Scrip

I am trying to automate an email system where whenever someone submits a form, therefore editing the spreadsheet, google-scripts will send me an email containing the message. The code works perfectly fine and does what I want. However, I need to manually run the script every time which defeats the purpose. I have tried to do the function onEdit(e) however it never worked. I have also tried the triggers feature of AppsScript with no luck.
/**
* Sends emails with data from the current spreadsheet.
*/
function sendEmails() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = sheet.getLastRow();
var numRows = sheet.getLastRow();
// Fetch the range of cells
var dataRange = sheet.getRange(startRow, 1, numRows, sheet.getLastColumn());
// Fetch values for each row in the Range.
var data = dataRange.getValues();
var emailAddress = 'email#email.com'; // First column
var message = data[0][3]; // Fourth column;
var subject = 'A Subject';//this will always be the same
Logger.log(emailAddress, subject, message); //This is to test because of the limit of how many emails you can send a day.
MailApp.sendEmail(emailAddress, subject, message);
}
Here is a link to a google sheet that you can test and edit the script on: https://docs.google.com/spreadsheets/d/1bBNDc33fBx2JPcRByt-8TA2vrLqa51H72TIM-SeARsc/edit?usp=sharing
The sheets is not using google forms, it is getting it's data from the results of an HTML webpage I built that is used as a form.
This is possible by using Installable Triggers.
Follow these steps once you created the form inside your sheets:
For New Editor
Open your Apps Script project.
At the left, click Triggers alarm.
At the bottom right, click Add Trigger.
Select and configure the type of trigger you want to create.
Click Save.
For Legacy Editor
From the script editor, choose Edit > Current project's triggers.
Click the link that says: No triggers set up. Click here to add one now.
Under Run, select the name of function you want to trigger.
Under Events, select either Time-driven or the Google App that the script is bound to (for example, From spreadsheet).
Select and configure the type of trigger you want to create (for example, an > > - Hour timer that runs Every hour or an On open trigger).
Optionally, click Notifications to configure how and when you are contacted by email if your triggered function fails.
Click Save.
For this scenario, Select event source: From spreadsheet and Select event type: On form submit.
Once the setup for Installable trigger is done, you can now add an event object e to the function parameter to access the form values submitted by the user.
Try this in your code:
function onSubmitForm(e){
var message = e.namedValues["Message"]; //get the message value using question name
var emailAddress = 'xxxxxsampleemailxxxxxx';
var subject = 'A Subject';
MailApp.sendEmail(emailAddress, subject, message); //send email
}
Example:
Output:
References:
Installable Triggers
Event Object

Issue making a response from a google form showing error ""TypeError: Cannot read property "values" from undefined. (line 2, file "Code")""

In Google Forms, I am attempting to create an email response that includes a case number that is generated in the form responses (column titled code) but unfortunately, I have come across the error:
typeError: Cannot read property "values" from undefined. (line 2, file "Code")
I am a complete novice regarding this but I have set the trigger function and am given this error in the email that is sent in a reply.
Find a copy of the responses docs below:
https://docs.google.com/spreadsheets/d/1w8cUkErljf6JzQm9q5aFwa-m9jX4o5qrAg5K7ib9Keg/edit?usp=sharing
This is the code I'm using:
function myFunction(e){
var userName = e.namedValues.Name;
var userEmail = e.namedValues.Tag;
var date = e.namedValues.Timestamp;
var subject = e.namedValues.Code;
var message = "Thanks, " + userName + " for submiting your data remember to take note of your Case number "+ subject + "You'll need this to for you or the customer to find the case again" +date;
MailApp.sendEmail (userEmail, subject, message);}
I expect it to generate an email, use the code as a subject and include it the text in the body.
Different Google Applications can have triggers with the same name, but different event objects.
To access the latest form response you need the trigger onFormsubmit(), which can be both a Google Sheets and Google Form trigger.
e.namedValues is a Google Sheets onFormsubmit() event object, while a Google
Form onFormsubmit() event object would be e.response.
You have two options:
Bind your Apps Script to the Spreadsheet where your form responses are being saved and access the responses with e.namedValues as intended
Bind your Apps Script to the Form itself and access the responses with e.response as following:
var userName = e.response.getItemResponses()[0].getResponse();
var userEmail= e.response.getItemResponses()[1].getResponse();
...
Keep in mind that onFormsubmit() is an installable Trigger.

How to stop MailApp.sendEmail from sending myself a copy of the email

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.

Google Apps Script — Automatically Email and Share

I am attempting to write a small Google Apps script that will send a confirmation email and automatically share a folder with a logged-in user after they complete a form. Here is the script:
function formSubmitReply(e) {
MailApp.sendEmail({
to: Session.getActiveUser().getEmail(),
subject: "Keuka College Brand Download Center",
htmlBody:
"<p>Thank you for requesting access to the Keuka College Brand Download Center. Your access has been approved.</p>" +
"<p>You may access the download center by <a href='https://drive.google.com/a/keuka.edu/folderview?id=0B856ZeGoaGT_MG5BMEtEVGwzYkk&usp=sharing'>using this link,</a> " +
"visiting <a href='http://brand.keuka.edu'>Keuka College Brand Central,</a> or through your Google Drive.</p><p>Please contact the Office of Marketing and Communications " +
"with any questions you may have.</p>",
name: "Keuka College",
replyTo: "marketing#keuka.edu"
});
var folder = DocsList.getFolder('Brand Download Center');
folder.addViewer(Session.getActiveUser());
}​
This seems to be working, except it keeps emailing it to me -- not the user who is completing the form. I am not sure if it is sharing correctly.
Could someone provide some insight? This is my first time working with Google Apps script.
Session.getActiveUser() is the user that uses the form editor and the user that created the trigger that calls this function.
What you want us the user that filled the form.
Look at the documentation here to see how you can retrieve that value. Note that it will only be available if you are in a domain.
EDIT (sorry for the delay)
Here is a sample code to show how to get the form respondent in formApp (works only in GAFE or GA Business)
It will send you a mail with the name of the last form submitter (don't forget to create an installable onEdit trigger to test this).
function onSubmit(){
var form = FormApp.getActiveForm();
var formResponses = form.getResponses();
var lastResponse = formResponses[formResponses.length-1];
var user = lastResponse.getRespondentEmail()
var userString = 'user = '+user;
MailApp.sendEmail(Session.getActiveUser().getEmail(),'form submission report',userString);
}