1

Does anyone know how to customize the format of an email body in a script or within the cell formula when sending an email from google sheets?

The first attachment is the incorrect email body the script is sending .

And the second attachment is the correct email body format I need the script to send .

Code below:

// has been sent successfully.
var EMAIL_SENT = 'EMAIL_SENT';

/**
 * Sends non-duplicate emails with data from the current spreadsheet.
 */
function sendEmails2() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 2; // First row of data to process
  var numRows = 1000; // Number of rows to process
  // Fetch the range of cells A2:D1000
  var dataRange = sheet.getRange(startRow, 1, numRows, 1000);
  // Fetch values for each row in the Range.
  var data = dataRange.getValues();
  for (var i = 0; i < data.length; ++i) {
    var row = data[i];
    var emailAddress = row[0]; // First column
    var subject = row[3]; // Second column
    var message = row[3]; // Third column
    var emailSent = row[3]; // Fourth column
    if (emailSent !== EMAIL_SENT) { // Prevents sending duplicates
      MailApp.sendEmail(emailAddress, subject, message);
      sheet.getRange(startRow + i, 4).setValue(EMAIL_SENT);
      // Make sure the cell is updated right away in case the script is interrupted
      SpreadsheetApp.flush();
    }
  }
}
BigBen
  • 46,229
  • 7
  • 24
  • 40
  • @BigBen edited with correct photos. Sorry about that. – Matt Stapleton Apr 21 '20 at 19:56
  • I have to apologize for my poor English skill. Unfortunately, I cannot understand about `customize the format of an email body in a script or within the cell formula` and your 2 images. Can I ask you about the detail of your issue and your goal? – Tanaike Apr 21 '20 at 23:40
  • Not sure how it's supposed to be formatted. Do you mean the line breaks? Could you share the spreadsheet you're working on, so that the original data format can be seen? – Iamblichus Apr 22 '20 at 07:40
  • @lamblichus Here's the sheet:https://docs.google.com/spreadsheets/d/1dAHxhZ4U5gkfkCaPssf4AdhFzkmSQd2Fmz7R3ehUBsg/edit?usp=sharing – Matt Stapleton Apr 24 '20 at 22:24

1 Answers1

2

Issue:

When plain text is added to the email body, a line break is added after ~78 characters, as you can see, for example, here: Gmail API - plaintext word wrapping.

To avoid this, you should add an HTML body instead of plain text, using MailApp.sendEmail(recipient, subject, body, options).

Also, you need to add HTML line breaks (<br>) if you want to maintain the line breaks from the cell value (\n). You can use String.prototype.replace() for that.

Solution:

Replace this:

MailApp.sendEmail(emailAddress, subject, message);

For this:

MailApp.sendEmail(emailAddress, subject, "", {
  htmlBody: message.replace(/\n/g,'<br>')
});

Reference:

Iamblichus
  • 18,540
  • 2
  • 11
  • 27
  • @lamblichus any chance you know how to hyperlink text within the email body too? – Matt Stapleton Apr 29 '20 at 16:46
  • @MattStapleton In HTML, links are added with [anchor tags](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a), but how to replace the cell hyperlink with that depends on how the hyperlink is set in the cell. More information would be needed for that. I'd suggest you to post a new question if you have problems with that. – Iamblichus Apr 30 '20 at 08:30