I would like to send bulk google mails with unique attachments using a spreadsheet. In this spreadsheet, i have put the email address, the content template, and the respective attachment urls. i have the below code with me which helped me sending emails with unique attachments successfully from MY inbox.
But,i want to change this code so that the emails are sent from an alias (example: alias@gmail.com) that I've already set up in my Gmail settings and also kindly let me know what all changes should i do to my gmail inbox settings in order to send the email from alias. I would really appreciate it if someone would be able show me how I can change the piece of code below to do this. Thanks in advance!
var EMAIL_SENT = "EMAIL_SENT";
function sendEmails() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2;
var numRows = 3;
var dataRange = sheet.getRange(startRow, 1, numRows, 5);
var data = dataRange.getValues();
for (var i = 0; i < data.length; ++i) {
var row = data[i];
var emailAddress = row[2];
var subject = "Attachments";
var message = row[4];
var fileId = row[3].split("/")[5];
var emailSent = row[0];
try {
var file = DriveApp.getFileById(fileId);
if (emailSent != EMAIL_SENT) {
GmailApp.sendEmail(emailAddress, subject, message, { attachments: [file.getBlob()] });
sheet.getRange(startRow + i, 1).setValue(EMAIL_SENT);
// SpreadsheetApp.flush(); // I think that this might not be required to be used.
}
} catch(e) {
console.log("File of " + row[3] + "cannot be retrieved. Error message is " + e.message);
}
}
}
The sendEmail
function:
getAliases
function:
https://developers.google.com/apps-script/reference/gmail/gmail-app#getaliases
You need to make sure you have aliases that GmailApp can read so try this snippet from the above link to see what aliases you have:
// Log the aliases for this Gmail account and send an email as the first one.
var me = Session.getActiveUser().getEmail();
var aliases = GmailApp.getAliases();
Logger.log(aliases);
if (aliases.length > 0) {
GmailApp.sendEmail(me, 'From an alias', 'A message from an alias!', {'from': aliases[0]});
} else {
GmailApp.sendEmail(me, 'No aliases found', 'You have no aliases.');
}
You need to choose one of these values exactly to be able to use the {'from': alias}
parameter
var EMAIL_SENT = "EMAIL_SENT";
function sendEmails() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2;
var numRows = 3;
var dataRange = sheet.getRange(startRow, 1, numRows, 5);
var data = dataRange.getValues();
for (var i = 0; i < data.length; ++i) {
var row = data[i];
var emailAddress = row[2];
var subject = "Attachments";
var message = row[4];
var fileId = row[3].split("/")[5];
var emailSent = row[0];
try {
var file = DriveApp.getFileById(fileId);
if (emailSent != EMAIL_SENT) {
GmailApp.sendEmail(emailAddress, subject, message, { attachments: [file.getBlob()] , 'from': 'alias@gmail.com'});
sheet.getRange(startRow + i, 1).setValue(EMAIL_SENT);
// SpreadsheetApp.flush(); // I think that this might not be required to be used.
}
} catch(e) {
console.log("File of " + row[3] + "cannot be retrieved. Error message is " + e.message);
}
}
}