Skip to content

Instantly share code, notes, and snippets.

@trevorfoskett
Last active November 5, 2019 21:12
Show Gist options
  • Save trevorfoskett/807ffb8527078919d919315a77f4b7dd to your computer and use it in GitHub Desktop.
Save trevorfoskett/807ffb8527078919d919315a77f4b7dd to your computer and use it in GitHub Desktop.
Server-side function to generate and send an email with encrypted attachment content.
function sendEmail(cipherText, recipients, userMessage) {
// Get email address of file owner and assign attachment title.
var fileOwner = Session.getActiveUser().getEmail();
var fileName = DocumentApp.getActiveDocument().getName() + ".pdf.tdf3.html";
// Provide a basic email body for recipients who do not support HTML.
var emailBody = fileOwner + " has shared the encrypted file " + fileName +
" with you.\r\n\r\nIt\'s attached below; please download to open in" +
" Virtru\'s Secure Reader.";
// Assign values to variables in emailHTML.html template.
var htmlContent = HtmlService.createTemplateFromFile('emailHTML');
htmlContent.fileOwner = fileOwner;
htmlContent.fileName = fileName;
htmlContent.userMessage = userMessage;
// Create subject line based on filename and owner email address.
var subject = fileOwner + ' has shared a secure file: "' + fileName + '"';
// Convert ciphertext string to HTML blob.
var blob = Utilities.newBlob(cipherText, 'text/html', fileName);
// Send the email with the tdf.html blob as attachment.
MailApp.sendEmail(recipients, subject, emailBody, {
name: fileOwner,
attachments: [blob],
htmlBody: htmlContent.evaluate().getContent()
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment