function sendWelcomeEmail () { var ss = SpreadsheetApp.getActive().getSheetByName('Form Responses 1'); var lastRow = ss.getLastRow(); var studentName = ss.getRange(lastRow, 4).getValue().toString(); var studentEmail = ss.getRange(lastRow, 6).getValue().toString(); var accessCode = getCode();
// get the draft by ID and its raw content var id = Gmail.Users.Drafts.get('me', 'r-4118928725090248526').message.id; // correct Gmail object ID ----^ var message = GmailApp.getMessageById(id); var rawContent = message.getRawContent(); // replaces template email with student first name and proper email address var updatedRawContent = rawContent.replace(/studentName/, studentName); updatedRawContent = updatedRawContent.replace(/newstu...@eazlonboarding.com/, studentEmail); updatedRawContent = updatedRawContent.replace(/uniqueURL/, accessCode); // ^---- Logger.log shows that studentName and uniqueURL are *correctly* replaced // create the new message var message = Gmail.newMessage(); var encodedMsg = Utilities.base64EncodeWebSafe(updatedRawContent); message.raw = encodedMsg; // send and log it Gmail.Users.Messages.send(message, "me", Utilities.newBlob(rawContent, "message/rfc822"));
};.
--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/9382beea-1558-426e-870f-7c7c857ad048%40googlegroups.com.
function configureAndSendEmail() {
var templateDocId = “abc123”;
var subject = “Hello World”;
var rowObj = {
firstName: “Jane”,
lastName: “Doe”,
email: “te...@email.com”,
phone: “555-555-5555”,
}
if (!templateDocId || !subject || !rowObj.email) return 'Missing values';
var url = "https://docs.google.com/feeds/download/documents/export/Export?id=" + templateDocId + "&exportFormat=html";
var param = {
method: "get",
headers: {
"Authorization": "Bearer " + ScriptApp.getOAuthToken()
},
muteHttpExceptions: true,
};
try {
var html = UrlFetchApp.fetch(url, param).getContentText();
// Replace tags in template doc with object valu
for (var [key,val] in rowObj){
html = html.replace('{{'+key+'}}',val);
}
MailApp.sendEmail(rowObj.email, subject,
"We're sorry. Your email does not support html", {htmlBody: html});
return true;
} catch(e) {
Logger.log(e)
return;
}
}
function sendEmail () { var ss = SpreadsheetApp.getActive().getSheetByName('sheetName');
var lastRow = ss.getLastRow(); var studentName = ss.getRange(lastRow, 4).getValue().toString();
var studentLastName = ss.getRange(lastRow, 5).getValue().toString();
var studentEmail = ss.getRange(lastRow, 6).getValue().toString(); var accessCode = getCode();
// Fetch images as blobs, set names for attachments var welcomeBanner = UrlFetchApp .fetch('googleDriveImageURL') .getBlob() .setName("welcomeBannerURL"); // gets and modifies the template with the student's information var template = GmailApp.getDraft('draftID').getMessage().getBody(); template = template.replace(/studentName/, studentName); template = template.replace(/uniqueURL/, 'urlHTMLCode'); template = template.replace(/bannerHTMLcode/, '<img src="cid:welcomeBannerURL" alt="altText"></img>');
// replaces template email with student first name and proper email address
GmailApp.createDraft( studentEmail, 'subjectText', 'plainTextVersion', { htmlBody: template, inlineImages:{ welcomeBannerURL: welcomeBanner, }, name: 'customName' }).send(); };