3
votes

After taking a look at the provided tutorial for sending emails from a spreadsheet with Google Apps Script, I modified the given code with aims to be able to send the set of emails out with attachments as well.

It works well enough, even with a couple quirks from the limitations of Google Apps Script (the files have to be in the Google Drive, and all files in the Google Drive with whichever name is appropriate are taken from all folders in the drive).

function sendEmails() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 2;  // First row of data to process
  var numRows = 1;   // Number of rows to process
  // Fetch the range of cells A2:C3
  var dataRange = sheet.getRange(startRow, 1, numRows, 3)
  // Fetch values for each row in the Range.
  var data = dataRange.getValues();
  for (i in data) {
    var row = data[i];
    var emailAddress = row[0];  // First column
    var message = row[1];       // Second column
    var attachment = row[2];    // File name provided.
    var subject = "Mass Email";
    var files = DriveApp.getFilesByName(attachment); // Get all files with name.
    var blobs = []; // Array for attachment.
    // Move files into blobs
    while (files.hasNext()) {
      var file = files.next();
      blobs.push(file.getAs(MimeType.PLAIN_TEXT));
    }
    MailApp.sendEmail(emailAddress, subject, message, {
      attachments: blobs, // add attachments
      cc: "[email protected]" // CC to employer
    });
  }
}

After I first used it, however, I learned that I need to send the files not as attachments, but as the message body of the emails, among some other changes (this is for work). That is, I will only ever email one 'attachment' at a time to each email, and instead of that file being an attachment, its content should be copied over to the message of the email. The attachments are currently text files, and I'd like them to stay that way, but it isn't the most vital thing.

I cannot determine a way to do this with Google Apps Script. Is this possible, or will I have to have a different way of emailing these files? (Hopefully not by hand.)

Thanks in advance.

2
Those two limitations you mention dont exist in apps script, getting the blobs from a drive file is just one of thexwaysZig Mandel
Also, yes its possible to include the text in the body. Look at the api for mail, it allows you to build your own html body.Zig Mandel
I don't understand why you are having issues, you're so close to what you describe... instead of storing text in an array just send it as 'message' in the same while loop and you're done... you don't even need HTML or whatever.Serge insas
Well, I meant the limitations of this particular script, which wasn't clear. Still, however, I found no other way to automate the file sending to this degree. I certainly may be entirely wrong, but I haven't seen how in my hunting. And yes, I've seen that you can put text in as you send the email, that is not my sticking point. I can't figure out how to read text in from a .txt file, only move it around.Orion
@Sergeinsas Unless I've missed something, I'm not storing text in an array, but blobs with MimeType.PLAINTEXT as their content type. Blobs representing text files.Orion

2 Answers

4
votes

as mentioned in my last comment, converting your .txt files to Google documents makes it easy to achieve. see below (suggestion)

function sendEmails() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 2;  // First row of data to process
  var numRows = 1;   // Number of rows to process
  // Fetch the range of cells A2:C3
  var dataRange = sheet.getRange(startRow, 1, numRows, 3)
  // Fetch values for each row in the Range.
  var data = dataRange.getValues();
  for (i in data) {
    var row = data[i];
    Logger.log(row)
    var emailAddress = row[0];  // First column
    var message = row[1];       // Second column
    var attachment = row[2];    // File name provided.
    var subject = "Mass Email";
    var files = DriveApp.getFilesByName(attachment); // Get all files with name.
    while (files.hasNext()) {
      var file = files.next();
      var Id = file.getId();
      var content = DocumentApp.openById(Id).getBody().getText();
      Logger.log(content)
    MailApp.sendEmail(emailAddress, subject, message+'\n'+content)
    }
  }
}
7
votes

Try this. It's more faster and simpler.

var docContent = file.getBlob().getDataAsString();
Logger.log(docContent);