Skip to content

Instantly share code, notes, and snippets.

@labnol
Forked from gnunicorn/mass_emailer.js
Last active August 14, 2019 00:50
Show Gist options
  • Select an option

  • Save labnol/1907310 to your computer and use it in GitHub Desktop.

Select an option

Save labnol/1907310 to your computer and use it in GitHub Desktop.
Source Code for Mail Merge
function start_mailer() {
var myapp = UiApp.createApplication().setTitle('Mass Emailer').setHeight(120).setWidth(300);
var top_panel = myapp.createFlowPanel();
top_panel.add(myapp.createLabel("Please select the draft you'd like to be sent"));
var lb = myapp.createListBox(false).setId('msg').setWidth(250).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(250);
top_panel.add(name_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);
ok_btn.addClickHandler(handler);
SpreadsheetApp.getActiveSpreadsheet().show(myapp);
}
function callback(e) {
var mail = GmailApp.getMessageById(e.parameter.message);
_send_mails(mail, e.parameter.name);
var app = UiApp.getActiveApplication();
app.close();
return app;
}
function _send_mails(mail, name) {
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;
GmailApp.sendEmail(line[mail_idx], subjectCopy, bodyCopy, {
htmlBody: bodyCopy,
name: name,
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);
}
@haripetrov
Copy link

For some reason I receive "Error encountered: Cannot read property "1" from null." if I have inline picture (jpg, png, it doesn't matter) in the body of the message. Any suggestions how to fix that?

@Terminator-X
Copy link

Sorry, it is broken, since UIapp is unsupported anymore. Must be replaced with HTML service...

@relativegnosis
Copy link

Unfortunately this script is no longer working due to this error: UiApp has been deprecated. Please use HtmlService instead.
Details

Any chance this can be updated? This tool has been very handy for me ever since. It'd be so sad if it can't be fixed. Looking forward to some update

@labnol
Copy link
Author

labnol commented Aug 13, 2019

Please switch to the Gmail Mail Merge addon for Google Sheets. 🔥

@relativegnosis
Copy link

relativegnosis commented Aug 14, 2019 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment