Im trying to setup a form that will email me peoples responses.
This is the current script:
function nl2br_(input_string){
return input_string.replace(/(\r\n|\r|\n)/g,'<br />');
}
function contactUsMailer(e) {
// This script e-mails the contents of a form to a given recipient
// The form must have three fields in the order of: name; e-mail address; and message
// You must change the recipient variable below to your e-mail address
try {
var recipient = 'myemail#hotmail.com';
var timestamp = e.values[0];
var username = e.values[1];
var option = e.values[2];
var details = e.values[3];
var body = username+' sent the following message: '+option ;
var bodyHTML = '\
<p>'+username+' '+option+' below are the details </p>\
<blockquote>'+nl2br_(details)+'</blockquote>\
<p>Sent by the Steegle.com Contact Us Form Google Apps Script</p>';
var advancedArgs = {htmlBody:bodyHTML , replyTo:username};
MailApp.sendEmail(recipient, "Contact Us Form", body, advancedArgs);
} catch(e){
MailApp.sendEmail(recipient, "Error - Contact Us Form", e.message);
}
}
This is how it looks when I get an email:
var timestamp - not important ignore it
var username - their username
var option - this is the "issue"
var details - detail of problem
I would really like it to look something like this:
Username: "username here"
Issue: "issue here"
Details:
"wall of text for details here"
I've been at this for over an hour and a half and I can't get it to look anything remotely like what what is shown above :/
I had a similar situation and this is how I solved it.
I created a HTML Output with a table inside and then Sent this as My Email Body.
var log = HtmlService.createHtmlOutput()
log.append('<body><table border="1">');
log.append('<tr></td> Username: </td><td>' + usernameVAR +'</td></tr>');
log.append('<tr></td> ISSUE </td><td>' + issueVAR +'</td></tr>');
log.append('<tr></td> DETAILS </td><td>' + detailsVAR +'</td></tr>');
log.append('</table></body>');
var htmlContent = log.getContent();
MailApp.sendEmail(YOUR EMAIL GOES HERE ,"TITLE OF EMAIL", "SUBJECT LINE",{htmlBody:htmlContent} ) //Text as HTML
Good Luck!
Related
I am using this code to send emails (composing email content getting text from the sheet named ranges:
//compose issue emails to student and admin
function composeIssueEmail() {
//student's name, last name and email
var email = ss.getRangeByName("CourseProgressEmail").getValue()
var name = ss.getRangeByName("CourseProgressName").getValue()
var lastName = ss.getRangeByName("CourseProgressStudentLastName").getValue()
var subj = ss.getRangeByName("SetUpIssueTitle").getValue()
var subject = subj.replace("*imya*", name)
var bodyText = ss.getRangeByName("SetUpIssueBody").getValue()
var body = bodyText.replace("*imya*", name)
var link = getChecksheetURL()
var text = body.replace("*link*", link)
//send email to student
var studentEmail = sendEmail(email, subject, text)
var adminEmail = "AGcourseSup#gmail.com"
var adminSubj = ss.getRangeByName("SetUpAdminIssueTitle").getValue()
var adminSubject = adminSubj.replace("*imya*", name)
var adminSubjectFinal = adminSubject.replace("*familia*", lastName)
var adminText = ss.getRangeByName("SetUpAdminIssueBody").getValue()
var adminTextReplace = adminText.replace("*imia*", name)
var adminBody = adminTextReplace.replace("*familia*", lastName)
var adminText = adminBody.replace("*link*", link)
//send email to admin
sendEmail(adminEmail, adminSubjectFinal, adminText)
}
//gets current checksheet URL
function getChecksheetURL() {
var Url = ss.getUrl()
var linkMiddle = "#gid="
var sheetID = sheet.getSheetId()
var shecksheetURL = Url + linkMiddle + sheetID
return shecksheetURL
}
//sends emails
function sendEmail(email, subject, body) {
GmailApp.sendEmail(email, subject, body)
}
Execution transcript:
[19-06-12 16:39:43:396 EEST] Execution succeeded [2.399 seconds total runtime]
It sends stably to the gmail account that is the same as spreadsheet's one.
But to another gmail account it sends about every other time.
Details:
This code is executed (I log the line after this code)
The emails are visible in my outbound box but not arriving to any of the boxes of the recepient gmail.
Not in spam etc.
I don't get any messages, error or bounce notifications.
I tried MailApp instead - it's even worse and sometimes doesn't send even to my own email.
I tried to change things in settings config, but didn't find anything to work.
I set up a filter "never send to spam" and "always star it" - didn't work.
I deleted a link from it so it has no link - didn't work.
What can be a solution?
I handled this issue. The issue is about anti-spam filters not about the code.
I gained more trust to the email account by adding "Reply To" option within GmailApp.sendEmail method. It magically solved the problem so each email reaches target now.
I've created a script to send emails to a list of e.mail addresses with a PDF attachment. Now I want to "pimp it", adding a picture taken from Drive at the beginning of the email.
I found this method on Google Developers - sendEmail(recipient, subject, body, options) - but it's not crystal clear to me on how it works.
This is the code I wrote so far, but it's not working. I keep reading only the text, without any picture. It should take the first 50 rows of a spreadsheet, send an email to the address in column 9 and update column 11 once done.
var sheet = SpreadsheetApp.setActiveSheet(source.getSheets()[2]);
var row = 3
var subject = "Subject";
var imageId = DriveApp.getFileById("0B-OVYDHfkqhXOTF6aWVSSUtSbUE");
var htmlText = "<img src = "cid:imageId" /> Dear Friend, <BR> <BR> Text";
for (var i = 0; i <= 50; i++) {
var emailAddress = sheet.getRange(row + i, 9).getValue()
var message = "Hi,\n \n" + "text";
MailApp.sendEmail(emailAddress, subject, message, {
name: "Alternative Name",
htmlbody: htmlText,
attachments: [budgetPDF],
inLineImages: imageId
})
sheet.getRange(row + i, 11).setValue("Sent");
Could you please tell me what I'm doing wrong?
Thanks for your help!
You don't get the blob of the file.
var imageId = DriveApp.getFileById("0B-OVYDHfkqhXOTF6aWVSSUtSbUE");
var imageIdBlob = imageId.getBlob();
var htmlText = "<img src = 'cid:imageIdBlob' /> Dear Friend, <BR> <BR> Text";
.....
.....
MailApp.sendEmail(emailAddress, subject, message, {
name: "Alternative Name",
htmlbody: htmlText,
attachments: [budgetPDF],
inLineImages: imageIdBlob
})
As I don't know the format of the file you can also use getAs().
Stéphane
I am trying to make a simple paragraph where I can enter a few lines of text, but am not picking it up very easily. Can I switch it to html in the script or is there an easier way to write a template paragraph?
My current script looks like this where I am making an automated email response whenever someone fills out a form (conference registration). Currently, everything in my message section comes out as one block.
I would like to be able to have a few paragraphs in the email such as "Don't forget to mail payment to (Address)" and then the "registration" answer to list as its own line as well with text before it.
Also, I want to be able to display the Tuesday- Thursday answers in a bulleted list.
function myFunction(e){
var userName = e.values[2];
var userEmail = e.values[11];
var Registration = e.values[1];
var Tuesday = e.values[16];
var Wednesday = e.values[17];
var Thursday = e.values[18];
var Friday = e.values[19];
var subject = "Conference Registration";
var Message = "Thank you for Registering for the Conference," + userName + "You have following registration type: " + Registration;
var message = "You have registered for the following sessions: " + Tuesday + Wednesday + Thursday + Friday;
MailApp.sendEmail(userEmail, subject, message);
}
As mentioned in the other answer, you can create html content in an email message, below is a simple example of how to do that based on your question.
btw I added a script to simulate a form sending so it will be much easier to test (without needing to send hundreds of forms while you play with html tags)
About HTML tags, a Google search on these words will show you far more tips and tricks that I could ever do in this answer... enjoy !
note : in the example below I changed the e.values indexes to simplify my test code... don't forget to re-adapt !
Code :
function testMyFunction(){
var e = {};
e.values = ['test user Name','test user Email','test Registration',' event on Tuesday','event on Wednesday','party on Thursday','nothing on Friday'];
myFunction(e);
}
function myFunction(e){
var userName = e.values[0];
var userEmail = e.values[1];
var Registration = e.values[2];
var Tuesday = e.values[3];
var Wednesday = e.values[4];
var Thursday = e.values[5];
var Friday = e.values[6];
var subject = "Conference Registration";
var htmlMessage = "Thank you for Registering for the Conference," + userName + "<br>You have following registration type: <li>" + Registration;
htmlMessage+=Tuesday+'</li><li>'+Wednesday+'</li><li>'+ Thursday+'</li><li>'+Friday+'</li>';
Logger.log(htmlMessage);
MailApp.sendEmail(Session.getActiveUser().getEmail(), subject,'html content',{htmlBody : htmlMessage});
}
Result screen capture :
If you want to use html formatting in your body, when you are calling sendEmail do it like this:
MailApp.sendEmail({
to: userEmail,
subject: subject,
htmlBody: message
});
This will tell Gmail to interpret the message variable as HTML and not just raw text.
It is my first time writing google app script and I am desperately need help.
The purpose is to set up a workshop sign up form. Based on how many people already signed up, an email is sent out to inform if sign up was successful, or was put in the wait list.
I copied code from a tutorial. But need help to get the count of form responses. Here is how it looks like now:
function onFormSubmit(e) {
var timestamp = e.values[0];
var yourName = e.values[1];
var toAddress = e.values[2];
var subject = "Workshop Confirmation";
var emailBody = "Thank you for your submitted on " + timestamp
var num_response = COUNT_NUMBER_OF_RESPONSE // <-- need help here
var LIMIT = 15
if (num_response <= LIMIT) {
emailBody = emailBody + "\n\nYou are enrolled in the workshop";
}
else {
var wait = num_response - LIMIT
emailBody = emailBody + "\n\nThe workshop is full. You are #" + wait + " in the waiting list"
}
emailBody = emailBody + "\n\nThe details you entered were as follows: " +
"\nYour Name: " + yourName +
"\nYour Email: " + toAddress ;
MailApp.sendEmail(toAddress, subject,
emailBody, optAdvancedArgs);
}
I have no clue how to find right answer in the google app document. Any help would be greatly appreciated!
How about the composite function
FormApp.getActiveForm().getResponses().length
no need to go around looking for the spreadsheet (since in my experience, the spreadsheet is not always up to date, whereas the form is)
From what I see in the tutorial, this script is embedded in a spreadsheet so the easiest would be to count the number of rows and substract 1 because of the headers...
There is a method for that : getLastRow(), the doc refered in this link should give you enough information to write the few lines of code you need...
test :
function xx(){
var lr = SpreadsheetApp.getActiveSheet().getLastRow()-1;
Logger.log(lr);
}
Script on form (not spreadsheet)
function onFormSubmit(e) {
var num_response = FormApp.getActiveForm().getResponses().length
var LIMIT = 20 //or whatever
if (num_response < LIMIT) {
}
else {
var form = FormApp.getActiveForm();
form.setAcceptingResponses(false);
}
}
I have a google form with the following columns:
Name | email | have_kids
where Name is just text input, email is just an email, and have_kids is a yes/no radio button
ex. dmkumar | myemail#gmail.com | Yes
when the user submits the form, the data is sent to a google spreadsheet. I would like to have it set up so that if the user selected "Yes" to have_kids, then an email is sent to that user. Here is the code I have right now. I have a trigger event to run the function, from the spreadsheet, on the form submit.
Here is the code I have now (that is not working):
function emailConfirmation(e) {
var userEmail = e.values[11]; //column k
var check = e.values[14]; //column n
/**
* Un-comment this to use it for debugging
*/
//for (var i in e.values) {
// Logger.log("" + i + ":" + e.values[i]);
//}
var subject = "Sending email from a spreadsheet";
var message = "Hello World";
if(check == "Yes")
{
MailApp.sendEmail(userEmail, subject, message);
}
}
I am new to google scripting, so simple explanations would be much appreciated! Thanks!
This is the answer I came up, because I'm sure people will stumble upon this page sarcasm
But it works great, none the less.
function emailConfirmation(e) {
var userEmail = e.namedValues["Email Address"].toString(); //Where "Email Address" is the name of a column
var kids = e.namedValues["Some of my guests are children who will attend the kids' camp."].toString();
var subject = "Sending email from a spreadsheet";
if(kids == "Yes")
{
MailApp.sendEmail(userEmail, subject, kids);
}
}