Introduct link break into message body with MailApp.sendEmail

788 views
Skip to first unread message

Stefanie Duerr

unread,
Jan 1, 2023, 6:48:06 PM1/1/23
to Google Apps Script Community
Hi,
I am very new to Apps Scripts and hope someone could kindly help me to introduce line breaks into the email body that is automated via this code. 

function sendMailEdit(e){

 if (e.range.columnStart != 5 || e.value != "Shipped") return;

 const rData = e.source.getActiveSheet().getRange(e.range.rowStart,1,1,4).getValues();

 let itemsOrdered = rData[0][2];

 let email = rData[0][0];

 let name = rData[0][1];

 let trackingNo = rData[0][3];

 let msg = "Hello, " + name + ". Your order: " + itemsOrdered + " is for shipping with tracking number " + trackingNo + ". Thank you for your purchase."

 MailApp.sendEmail(email, "Shipped Order", msg)

}


Thank you so much for your help.

Stefanie

Scott Bennett

unread,
Jan 1, 2023, 8:25:40 PM1/1/23
to google-apps-sc...@googlegroups.com
You can use \n in the string to cause a line break.

You also might want to look into template literals, that will save you from having to concatenate so many Strings. 

Scott Bennett


Sent from my iPhone 

On Jan 1, 2023, at 5:48 PM, Stefanie Duerr <glue...@gmail.com> wrote:

Hi,
--
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/50033be4-7ea9-47a1-92ea-3bbc09f35e28n%40googlegroups.com.

Ed Sambuco

unread,
Jan 1, 2023, 8:55:49 PM1/1/23
to google-apps-sc...@googlegroups.com
You could format as an HTML message:

let htmlMsg = "Hello, " + name + ". <br><br>Your order: " + itemsOrdered + " is for shipping with tracking number " + trackingNo + ". <br><br>Thank you for your purchase."

GmailApp.sendEmail(email, "Shipped Order", "", {htmlBody:htmlMsg})

This uses HTML formatting as opposed  to plain text formatting.  I also prefer the GmailApp to the MailApp class -- for some reason MailApp doesn't always work for me, but GmailApp always does.


Stephanie

unread,
Jan 2, 2023, 4:01:28 AM1/2/23
to google-apps-sc...@googlegroups.com
Thank you immensely, Ed, this works like a charm. FYI GmailApp, but with MailApp it works perfectly.

You received this message because you are subscribed to a topic in the Google Groups "Google Apps Script Community" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-apps-script-community/udPwAMDptX8/unsubscribe.
To unsubscribe from this group and all its topics, 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/CAANZsGPjfTBHcdVxbBF5OQE2XA-LtnYG0EChCA9gqeUBfmBvaQ%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages