How can i send confirmation mail send different from address? - google-apps-script

I'm sending confirmation mail with following code in google docs scripts.
But It is sending from my personal gmail address. I need define different mail address. Cause I will use this for my company and mail need show like that sometest#companytsite.com
How can i do that?
My Code:
function Initialize() {
var triggers = ScriptApp.getScriptTriggers();
for (var i in triggers) {
ScriptApp.deleteTrigger(triggers[i]);
}
ScriptApp.newTrigger("SendConfirmationMail")
.forSpreadsheet(SpreadsheetApp.getActiveSpreadsheet())
.onFormSubmit()
.create();
}
function SendConfirmationMail(e) {
try {
var ss, cc, sendername, subject, columns;
var message, value, textbody, sender;
// This is your email address and you will be in the CC
cc = Session.getActiveUser().getEmail();
// This will show up as the sender's name
sendername = "Your Name Goes Here";
// Optional but change the following variable
// to have a custom subject for Google Docs emails
subject = "Google Form Successfully Submitted";
// This is the body of the auto-reply
message = "We have received your details.<br>Thanks!<br><br>";
ss = SpreadsheetApp.getActiveSheet();
columns = ss.getRange(1, 1, 1, ss.getLastColumn()).getValues()[0];
// This is the submitter's email address
sender = e.namedValues["Email Address"].toString();
// Only include form values that are not blank
for ( var keys in columns ) {
var key = columns[keys];
if ( e.namedValues[key] ) {
message += key + ' :: '+ e.namedValues[key] + "<br />";
}
}
textbody = message.replace("<br>", "\n");
GmailApp.sendEmail(sender, subject, textbody,
{cc: cc, name: sendername, htmlBody: message});
} catch (e) {
Logger.log(e.toString());
}
}

According to the documentation: https://developers.google.com/apps-script/reference/gmail/gmail-app#sendEmail(String,String,String,Object)
You should be able to pass in a from in the options.
GmailApp.sendEmail(sender, subject, textbody, {
from: "sometest#companytsite.com",
cc: cc,
name: sendername,
htmlBody: message
});
Nb. to avoid confusion sender should really be to
Now it also says that the email address you want to send it from must be an alias, to check you can print out getAliases() to see if it exists. If not you should be able to follow these instructions:
https://support.google.com/mail/answer/22370?hl=en

Related

Convert MailApp.sendEmail code to GmailApp.sendEmail with HTML Body

All, I am looking for help from someone with more experience with this. I have a cobbled together email script that works great as is. I want to specify the from: address using an alias and it's my understanding that I need to use GmailApp vs MailApp to accomplish this. The trouble is, I can't figure out how to make my htmlBody work in the GmailApp version.
Here is my working MailApp code:
function SendPreReleaseAlertEmail(row) {
var sheet = SpreadsheetApp.getActive().getSheetByName('EmailSheet');
var subject = sheet.getRange("M3").getValues(); // Change the subject as needed.
var recipients = sheet.getRange("M2").getValue(); // Change the recipient as needed.
var message = "<HTML><BODY>" + "<P>"
for (var x=6;x<14;x++) { //Loop from * to * with a +1 increment
message = message + sheet.getRange("M" + x).getValues() + "<BR>" //Add row I(x) to the message
}
message = message + "</HTML></BODY>";
MailApp.sendEmail(recipients, subject, "", {htmlBody: message});
}
And this is my FAILED attempt at converting it to GmailApp with a From address:
function SendPreReleaseAlertEmail(row) {
var sheet = SpreadsheetApp.getActive().getSheetByName('EmailSheet');
var subject = sheet.getRange("M3").getValues(); // Change the subject as needed.
var recipients = sheet.getRange("M2").getValue(); // Change the recipient as needed.
var message = "<HTML><BODY>" + "<P>"
for (var x=6;x<14;x++) { //Loop from * to * with a +1 increment
message = message + sheet.getRange("M" + x).getValues() + "<BR>" //Add row I(x) to the message
}
message = message + "</HTML></BODY>";
GmailApp.sendEmail(recipients, subject, {htmlBody: message}, {from: "moo#cow.com"});
}
The above code does in fact email from the alias. But the htmlBody just has the words "[object Object]" in the body of the email.
It's Alive! Here is the final code that solved my problem:
GmailApp.sendEmail(recipients, subject, '', {htmlBody: message, from: "moo#cow.com"});
I think you need to fix two things.
1.The syntax is
sendEmail(recipient, subject, body, options)
So you may need to include a blank placeholder for body in your code.
2.You may also need to have both the htmlBody and from in the options JavaScript object like so:
GmailApp.sendEmail(recipients, subject, '' , {
htmlBody: message,
from: "moo#cow.com"});
You can still use MailApp like that:
var email = "exampleRec#email.com";
var Subject_to_Send = "This is an automated email";
var check_body =
"Good morning team, <br/><br/>"
+"I hope this email finds you well. <br/><br/>";
MailApp.sendEmail( {to:email, subject:Subject_to_Send, body:check_body,htmlBody:check_body, from: "exampleSen#email.com"});
If you also want to send a noReply message you can adjust the last line as follows:
MailApp.sendEmail( {to:email, subject:Subject_to_Send, body:check_body,htmlBody:check_body, noReply: true});

Modifying the Google Spreadsheet script for sending values by email

This is an offshoot of a similar post here. A good answer was provided by Rivero but his code sends the values on the Google Spreadsheet's response sheet. I was wondering if instead the script would return values of the same row and column range specified by the code BUT from other sheet in the same spreadsheet. I'm really new to this and can't seem to find the right combination of codes to do so. Here is Rivero's code, I hope someone can help me accomplish this. Thanks in advance!
/* Send Confirmation Email with Google Forms */
function Initialize() {
var triggers = ScriptApp.getProjectTriggers();
for (var i in triggers) {
ScriptApp.deleteTrigger(triggers[i]);
}
ScriptApp.newTrigger("SendConfirmationMail")
.forSpreadsheet(SpreadsheetApp.getActiveSpreadsheet())
.onFormSubmit()
.create();
}
function SendConfirmationMail(e) {
try {
var ss, cc, sendername, subject, columns;
var header, message, value, textbody, sender, itemID, url;
// This is your email address and you will be in the CC
cc = "name#email.com";
// This will show up as the sender's name
sendername = "name to be displayed as sender";
// Optional but change the following variable
// to have a custom subject for Google Docs emails
subject = "Choose an approppiate subject";
// This is the body of the auto-reply
message = "";
ss = SpreadsheetApp.getActiveSheet();
columns = ss.getRange(1, 1, 1, ss.getLastColumn()).getValues()[0];
// This is the submitter's email address
sender = e.namedValues["Username"].toString();
// Only include form values that are not blank
for ( var keys in columns ) {
var key = columns[keys];
//Use this to look for a particular named key
if ( e.namedValues[key] ) {
if ( key == "Username" ) {
header = "The user " + e.namedValues[key] + " has submitted the form, please review the following information.<br />";
} else {
message += key + ' ::<br /> '+ e.namedValues[key] + "<br />";
}
}
}
}
textbody = header + message;
textbody = textbody.replace("<br>", "\n");
Logger.log("Sending email");
GmailApp.sendEmail(cc, subject, textbody,
{cc: cc, name: sendername, htmlBody: textbody});
} catch (e) {
Logger.log(e.toString());
}
}
It would better if you can name your sheet and use it instead of using active spreadsheet.
var sheet = SpreadsheetApp.openById('spreadsheetId').getSheetByName('name');
OR you can also use the sheet number to access it.
var sheet = SpreadsheetApp.openById('spreadsheetId').getSheets()[0];
After you have reference to the sheet, get the range for the column followed by data array:
var columnRange = sheet.getRange('a1Notation');
var dataArray = columnRange.getValues();
then iterating the data as per your convenience
for(var i=1; i<=dataArray.length; i++)
{
}

Row number of a variable in Spreadsheet (Apps Script)

In my Spreadsheet, I have taken the answers from the users and added a column to grade them. Now, I want to mail them the whole data they have entered and their respective grades. To do that, I have to know the Row number of the variable "key2" which will be changing for every response of the user. I am providing my code here. Some one please help me.
/* Send Confirmation Email with Google Forms */
function Initialize() {
var triggers = ScriptApp.getProjectTriggers();
for (var i in triggers) {
ScriptApp.deleteTrigger(triggers[i]);
}
ScriptApp.newTrigger("SendConfirmationMail")
.forSpreadsheet(SpreadsheetApp.getActiveSpreadsheet())
.onFormSubmit()
.create();
}
function SendConfirmationMail(e) {
var s1 = SpreadsheetApp.getActiveSpreadsheet();
var s2 = SpreadsheetApp.setActiveSheet(s1.getSheetByName("Sheet2"));
try {
var cc, sendername, subject, column2;
var message, value, textbody, sender;
// This is your email address and you will be in the CC
cc = Session.getActiveUser().getEmail();
// This will show up as the sender's name
sendername = "Quiz2win";
// Optional but change the following variable
// to have a custom subject for Google Docs emails
subject = "Google Form Successfully Submitted";
// This is the body of the auto-reply
message += "We have received your details.<br />Thanks!<br /><br />";
column2 = s2.getRange(1,1,1,10).getValues()[0];
// This is the submitter's email address
sender = e.namedValues["Email Address"].toString();
// Only include form values that are not blank
for ( var keys in column2 ) {
var key2 = column2[keys];
if ( e.namedValues[key2] ) {
message += key2 + ' :: '+ e.namedValues[key2] + "<br />";
} else {
var key1 = s2.getValues();
message += key2 + ' :: ' + key1 + "<br/>";
}
}
textbody = message.replace("<br>", "\n");
GmailApp.sendEmail(sender, subject, textbody,
{cc: cc, name: sendername, htmlBody: message});
} catch (e) {
Logger.log(e.toString());
}
The range you are mentioning here s2.getRange(1,1,1,10) is basically getting only first row of data from the sheet.
Instead get all the rows and column values into a range variable and loop through the range for each row and send an email in the loop itself.
Check this documentation for more details.

Want to send confirmation email from alias

I want the "from" address of my confirmation emails to be from an aliased account instead of my default Google account.
This is my code. I've tried replacing cc = Session.getActiveUser().getEmail(); with the alias email address, but that didn't work.
/* Send Confirmation Email with Google Forms */
function Initialize() {
var triggers = ScriptApp.getProjectTriggers();
for (var i in triggers) {
ScriptApp.deleteTrigger(triggers[i]);
}
ScriptApp.newTrigger("SendConfirmationMail")
.forSpreadsheet(SpreadsheetApp.getActiveSpreadsheet())
.onFormSubmit()
.create();
}
function SendConfirmationMail(e) {
try {
var ss, cc, sendername, subject, columns;
var message, value, textbody, sender;
// This is your email address and you will be in the CC
cc = Session.getActiveUser().getEmail();
// This will show up as the sender's name
sendername = "EarthFest Singapore";
// Optional but change the following variable
// to have a custom subject for Google Docs emails
subject = "EarthFest Tickets";
// This is the body of the auto-reply
message = "<center><img src='http://earthfestsingapore.com/email_logo.png' /></center><br><center>Tickets for Sept. 26, 2015. 10:30 am to 3:30 pm.</center><br>Thanks so much for joining EarthFest! Let's make a modern and sustainable future fun, delicious, and possible!<br><br>We can't wait to see you there! Feel free to invite your friends by sharing this link: <a href='http://goo.gl/mS5D2X'>http://goo.gl/mS5D2X</a> and we'd love to see you on:<br><br><center><a href='http://facebook.com/earthfestsingapore'><img src='http://earthfestsingapore.com/facebook_small.png' /></a> <a href='http://instagram.com/earthfestsing'><img src='http://earthfestsingapore.com/instagram_small.png' /></a> <a href='http://twitter.com/earthfestsing'><img src='http://earthfestsingapore.com/twitter_small.png' /></a> <a href='https://www.youtube.com/user/earthfestsing'><img src='http://earthfestsingapore.com/youtube_small.png' /></a></center><br><br>";
ss = SpreadsheetApp.getActiveSheet();
columns = ss.getRange(1, 1, 1, ss.getLastColumn()).getValues()[0];
// This is the submitter's email address
sender = e.namedValues["E-mail address can we send tickets"].toString();
// Only include form values that are not blank
for ( var keys in columns ) {
var key = columns[keys];
if ( e.namedValues[key] ) {
message += key + ' :: '+ e.namedValues[key] + "<br />";
}
}
textbody = message.replace("<br>", "\n");
GmailApp.sendEmail(sender, subject, textbody,
{cc: cc, name: sendername, htmlBody: message});
} catch (e) {
Logger.log(e.toString());
}
}
You can add the alias in the options parameter of the sendEmail() method.
GmailApp.sendEmail(sender, subject, textbody, {
cc: cc,
name: sendername,
htmlBody: message,
from: "alias#example.com"
});

Send email to specific user based on Google Form

Background: Google Script for Forms creating a phone message log that emails to a specific person as selected.
Need help with the email address portion of the code. I want to have a message sent to an email address based on the radio button with the person's name selected.
function Initialize() {
var triggers = ScriptApp.getScriptTriggers();
for (var i in triggers) {
ScriptApp.deleteTrigger(triggers[i]);
}
ScriptApp.newTrigger("SendGoogleForm")
.forSpreadsheet(SpreadsheetApp.getActiveSpreadsheet())
.onFormSubmit()
.create();
}
function SendGoogleForm(e) {
try {
var email = HELPHELPHELP;
// Optional but change the following variable
// to have a custom subject for Google Form email notifications
var subject = "Google Docs Form Submitted";
var s = SpreadsheetApp.getActiveSheet();
var columns = s.getRange(1, 1, 1, s.getLastColumn()).getValues()[0];
var message = "";
// Only include form fields that are not blank
for (var keys in columns) {
var key = columns[keys];
if (e.namedValues[key] && (e.namedValues[key] != "")) {
message += key + ' :: ' + e.namedValues[key] + "\n\n";
}
}
// This is the MailApp service of Google Apps Script
// that sends the email. You can also use GmailApp for HTML Mail.
MailApp.sendEmail(email, subject, message);
} catch (e) {
Logger.log(e.toString());
}
}
Presumably the intended recipient's name comes from a question / column with a title like "Message for".
In that case, try this:
...
var email = resolveEmail( e.namedValues["Message for"] );
...
function resolveEmail( userName ) {
// List of userNames & (secret) email addresses
var userEmails = {
"Mickey" : "bigears#example.com",
"Donald" : "duckman#example.com" // etc.
};
if (userName in userEmails) {
return userEmails[userName];
}
else {
// Unknown user - send email to script owner
return Session.getEffectiveUser().getEmail();
// Alternatively, could throw an error to get notified
// throw new Error( "Could not resolve email for user: " + userName );
}
}