public
Last active

Mass emailer with auto reply for forms

  • Download Gist
emailer.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
/*
* Mass Emailer Google Spreadsheet and Forms Script.
*
* Changelog:
* 19. June 2013
* - also copy CC and BCC from draft email
* 27. May 2013
* - bugfix, kudos to @stefanhoth: the col is called "Email"
* - show the quota left for the day (thanks to @stefanhoth)
* 30. Apr. 2013
* - add support for Form submit auto-replies
* 27. Apr. 2013
* - trial checkbox allows easier debugging
* 16. Feb. 2012
* - released the first version to public
* 15. Feb. 2012
* - add possibility to change the name
* - add full-blown UI
* 10. Feb. 2012
* - add Support to load Mail from Draft
*/
function start_mailer() {
var myapp = UiApp.createApplication().setTitle('Mass Emailer').setHeight(200).setWidth(300);
var top_panel = myapp.createFlowPanel();
top_panel.add(myapp.createHTML("<i>Note: You have a maximum of <strong>" + MailApp.getRemainingDailyQuota() + "</strong> email messages left today!</i><br/><br/>"));
top_panel.add(myapp.createLabel("Please select the draft you'd like to be sent"));
var lb = myapp.createListBox(false).setId('msg').setWidth(300).setName('message').setVisibleItemCount(1);
var threads = GmailApp.search('is:draft', 0, 10);
if (threads.length === 0) {
Browser.msgBox("Please save your template as a draft in your gmail account!");
return;
}
for (var i = 0; i < threads.length; i++) {
lb.addItem("" + threads[i].getFirstMessageSubject(), threads[i].getMessages()[0].getId());
}
top_panel.add(lb);
top_panel.add(myapp.createLabel("Name to send from (optional)"));
var name_box = myapp.createTextBox().setName("name").setWidth(300);
top_panel.add(name_box);
var check_box = myapp.createCheckBox().setName("trial").setText("Trail Run").setWidth(300);
top_panel.add(check_box);
var ok_btn = myapp.createButton('Send mails now');
top_panel.add(ok_btn);
myapp.add(top_panel);
var handler = myapp.createServerClickHandler('callback').addCallbackElement(lb).addCallbackElement(name_box).addCallbackElement(check_box);
ok_btn.addClickHandler(handler);
SpreadsheetApp.getActiveSpreadsheet().show(myapp);
}
function callback(e) {
var mail = GmailApp.getMessageById(e.parameter.message);
_send_mails(mail, e.parameter.name, e.parameter.trial);
var app = UiApp.getActiveApplication();
app.close();
return app;
}
function _send_mails(mail, name, trial) {
var ws = SpreadsheetApp.getActiveSpreadsheet().getActiveSelection(),
data = ws.getValues(),
attrs = data.shift(),
count = 0,
mail, bodyCopy, attachments, subjectCopy, idx, line_idx, mail_idx, line;
mail_idx = attrs.indexOf('Email');
if (mail_idx === -1) {
Browser.msgBox("Canceled: At least one row must be called 'Email'");
return;
}
attachments = mail.getAttachments();
for (line_idx in data) {
line = data[line_idx];
bodyCopy = mail.getBody();
subjectCopy = mail.getSubject();
for (idx in attrs) {
bodyCopy = bodyCopy.replace("{{" + attrs[idx] + "}}", line[idx]);
subjectCopy = subjectCopy.replace("{{" + attrs[idx] + "}}", line[idx]);
}
count += 1;
if(trial == "true") {
Browser.msgBox("to:" + line[mail_idx] + "\nSubject:" + subjectCopy + "\nBody:" + bodyCopy);
return;
} else {
GmailApp.sendEmail(line[mail_idx], subjectCopy, bodyCopy, {
htmlBody: bodyCopy,
name: name,
bcc: mail.getBcc(),
cc: mail.getCc(),
attachments: attachments
});
}
}
Browser.msgBox(count + " Mails send");
}
function onOpen() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var menuEntries = [ {name: "Send Mass Email", functionName: "start_mailer"}];
ss.addMenu("Mass-Mailer", menuEntries);
}
 
// don't forget to connect this to the On-Form-Submit event
function ReplyToUser(e) {
var name = e.namedValues["Name"],
userEmail = e.namedValues["Email"];
MailApp.sendEmail(userEmail, "Thanks for registering with AwesomeScript",
"Hi " + name + "\n\n" +
"thanks for registering. Please follow these instructions to prepare for the trip:\n" +
" [ ] become awesome\n [ ] stay awesome \n [ ] come by\n\n "+
"Thanks\n Ben",
{name:"OpenTechSchool e.V. Board"});
}‚Äč

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.