How can I send HTML code in URL to doGet(e) function?
our team sends emails, but all emails go from one account on the owner's behalf. I collect some data from Google Sheets, make its HTML, paste this HTML code in a temporary range and call the doGet function which picks this code and sends the email. This doGet() function is published as web app by the account owner so emails are sent on his behalf.
var sent = UrlFetchApp.fetch(urlOfwebApp + "?recipient=" + recipients + "&urlOfGSheet=" + urlssEmail+"&shtName="+shtName);
Then doGet function picks the HTML from Google Sheet cell:
function doGet(e){
var htmlTable = mySheet.getRange('A1').getValue();//This has HTML code pasted in it
MailApp.sendEmail(e.parameter.recipient, subject,'', { htmlBody: htmlTable});
}
However, this is inefficient as multiple emails are to be sent and it's slow plus doesn't seem a good code. I want my HTML to transfer directly to doGet() function. I tried but it gives my a lot of errors cz of HTML syntax like ", < br> etc. So, how do I send it efficiently?
Later on, I also want to email Sheets/tabs as attachments but that would be easier this way as I'm currently doing I guess.
Related
We have requests / tickets coming across teams which are in different countries. And these requests are submitted through Google Forms. These requests / tickets are then picked at a single location and processed
There's a 5 hour non working window from 00:00 am to 5am. During this time as well we do get requests from the offices.
Is there a way if a form is submitted during this non workings hours an email notification goes to the requester letting them know that we are closed and the request submitted will be picked once we are open?
There is an option to run script. But i get an error.
If a script has to be run, how would that be possible based on the requirement?
Thanks
You can use the onFormSubmit trigger for spreadsheets. It provides all of the information for each submission via namedValues which is an object or values which is an array. As an installable trigger it is capable of performing operations that require permission like sending emails.
onFormSubmit event object
There are many examples of doing this on this site.
You will encounter such error when you tried to run your code manually using the Script Editor because your event object doesn't exist.
I tried to run your code using an on-form submit in Google Form itself and it was triggered successfully when I submit a form as an example. Google Forms events - Form submit
Using on-form submit installable trigger in Google Forms:
Sample Code:
function sendEmail(e) {
//response //getRespondentEmail()
var html = HtmlService.createTemplateFromFile("email.html");
var htmlText = html.evaluate().getContent();
//logger.log(htmlText);
var emailTo = e.response.getRespondentEmail();
var subject = "Thanks for filling the form";
var textBody = "This email requires HTML support make sure you opn";
var options ={ htmlBody: htmlText };
var today = new Date();
Logger.log(today.getHours())
if (emailTo !== undefined && today.getHours() < 5){
GmailApp.sendEmail(emailTo, subject, textBody, options);
}
}
Modifications done:
I just include an additional condition to check for the current time when the form was submitted to check if it is within 00:00 to 05:00. Refer to JavaScript getHours() Method
In this sample code, the email will only be sent when a form was submitted from 00:00 to 04:59. You can adjust the condition based on your preference if you want. You can also include checking the minutes using JavaScript getMinutes() Method
Note:
You can debug your installable triggers by adding logs in your code and by checking the Executions tab in the Apps Script Editor.
I am developing a add-on program in google app script which gets all the gmail sent mails with there subject, body, attachment(if any). I have did this for the Inbox mail using getInboxThreads() function. Is there a function which does same for sent mails?
The program that i am writing is for any gmail user or a group of users how wants to save their gmail emails on the google drive for monitoring or any other operations.
You can use the user.messages.list method to get a list of all the message ids for the user. You will then have to use user.messages.get To get the data about the message.
You can use the 'q': 'in:sent' parameter to get only the messages in the sent folder.
function myFunction() {
var myMessages=Gmail.Users.Messages.list("me",{'maxResults': 5 , 'q': 'in:sent' }).messages;
Logger.log(myMessages);
for(var i=0;i<myMessages.length;i++){
var id=myMessages[i].id;
Gmail.Users.Messages.get('me', id).payload.headers.forEach(function(e){
if(e.name=="Subject"||e.name=="From"){
Logger.log(e.name+": "+e.value)
}
}
);
}
}
Thank you #DalmTo
Your post help me lot.
I did some research got a similar but little bit different solution.
I found a method in GmailApp class called search and sent a query(in:sent) as you suggested. This returned me all the sent mails.
I did something like this
var _sentMail = GmailApp.search('in:sent');
This returned me array of GmailThread.
The one thing i did found that sent label in our gmail dashboard is not actually a label, its a method which takes a parameter "in:sent".
I'm trying to send an email to a number of users from a Google Sheet, triggered by a menu item by the user.
Unfortunately the emails won't send if I run the script from my work email.
I checked the oauthScopes under appsscript.json to be sure that the proper scopes are requested by the script, and they are.
I've also tested the code multiple times in a personal account, and it works. However when I run the code from my work account, the emails won't send. Everything about the code in the two accounts is the same, except for the domain sending the email.
function submit() {
//gather admin emails
var admins = ["someone#example.com"]
var thisUrl = SpreadsheetApp.getActiveSpreadsheet().getUrl();
var thisName = DriveApp.getFileById(SpreadsheetApp.getActiveSpreadsheet().getId()).getOwner().getName();
MailApp.sendEmail(admins.join(", "), thisName + " has submitted their template for upload", thisUrl)
}
As mentioned, the code works fine from a personal account. Any ideas why it isn't running from my work account?
Okay, so. I have written a simple script to send an email to anyone who completes my form on google docs. The problem is, i cannot get it to run, all i have is the guts to get the users email and send them an email. I have no means of launching the script. I have looked near and far trying to find a way to auto launch the script once the form is completed.
Tl;dr Trying to send a receipt confirming the form was completed.
here is the script:
function hallpassreceipt() {
// Get the email address of the active user - that's you.
var email = Session.getActiveUser().getEmail();
// Get the name of the document to use as an email subject line.
var subject = 'Hall Pass';
// Append a new string to the "url" variable to use as an email body.
var body = 'This is a hall pass .';
// Send yourself an email with a link to the document.
GmailApp.sendEmail(email, subject, body); }
Read Understanding Triggers.
You need to set up an Installable Form Response trigger. One example: Google Script to email form answers and questions to a user.
I would like to be able to send an automatic email message to those who complete a google form . There responses are fed to a google spreadsheet, and I'd like to be able to set it up so that anytime someone completes the form, they are sent the email. Any idea how to do this? I've tried the google tutorial about sending emails from a spreadsheet, but since this is not an automatic function, it does not suit my needs. Any suggestions?
Create an installable trigger in the Form to run for the on Form Submit event. Use a function like getFormDataFields to grab the data from the form submission event. Use the MailApp.sendEmail function to blast out an email. Provide it the necessary parameters.
function myFunction(e) {
var data = getFormDataFields(e); //data['Question Name']
MailApp.sendEmail();
}
function getFormDataFields(e){
var dataFields = [];
for (property in e.namedValues){
dataFields[property] = e.namedValues[property][0];
}
return dataFields;
}