Skip to content

Commit

Permalink
Maximum length for email subject added.
Browse files Browse the repository at this point in the history
Turns out the MailApp.sendEmail() API has an undocumented limit
of 250 characters on the length of the email subject. If the
subject is longer than the limit an error is thrown.

I've added a test to check if the limit is met: if it is not the
string gets truncated to 250 - 3 characaters and an ellipsis is
added at the end, so that the final string "ABCABCABC..." is 250
characters long.
  • Loading branch information
GioBonvi committed Oct 1, 2018
1 parent 6012f56 commit 68c5c41
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion code.gs
Original file line number Diff line number Diff line change
Expand Up @@ -2078,7 +2078,7 @@ function getEventsOnDate (year, month, day, calendarId) {
function generateEmailNotification (forceDate) {
var now, events, contactList, subjectPrefix, subjectBuilder, subject,
bodyPrefix, bodySuffixes, bodyBuilder, body, htmlBody, htmlBodyBuilder,
contactIter, runningOutdatedVersion;
contactIter, runningOutdatedVersion, maxSubjectLength, ellipsis;

log.add('generateEmailNotification() running.', Priority.INFO);
now = forceDate || new Date();
Expand Down Expand Up @@ -2237,6 +2237,12 @@ function generateEmailNotification (forceDate) {
log.add('Building the email notification.', Priority.INFO);
runningOutdatedVersion = isRunningOutdatedVersion();
subject = subjectPrefix + subjectBuilder.join(' - ');
// An error is thrown if the subject of the email is longer than 250 characters.
maxSubjectLength = 250;
ellipsis = '...';
if (subject.length > maxSubjectLength) {
subject = subject.substr(0, maxSubjectLength - ellipsis.length) + ellipsis;
}
body = [bodyPrefix, '\n']
.concat(bodyBuilder)
.concat(['\n\n ', bodySuffixes[0], '\n '])
Expand Down

0 comments on commit 68c5c41

Please sign in to comment.