0

I am using Apps Script and Gmail API to send a reply to the first message of a thread. The code I have works fine for sending the reply, but I'm facing an issue with the paragraph structure in the email body.

Here is my code snippet:

var originalMessage = messages[0];
var recipientEmail = originalMessage.getTo();

var messageId = originalMessage.getHeader("Message-ID");
var data = [
  "MIME-Version: 1.0\n",
  `In-Reply-To: ${messageId}\n`,
  `Subject: Re:${originalMessage.getSubject()}\n`,
  `From: ${firstName} ${lastName} <${senderEmail}>\n`,
  `To: ${recipientEmail}\n\n`,
  emailBody,
].join("");
var draftReplyRequestBody = {
  message: {
    threadId: existingThreadId,
    raw: Utilities.base64EncodeWebSafe(data),
  }
};
var draftReplyResponse = Gmail.Users.Drafts.create(draftReplyRequestBody, "me");
var draftReplyId = draftReplyResponse.id;
var sendReplyParams = { id: draftReplyId };

Gmail.Users.Drafts.send(sendReplyParams, "me");

The issue I'm facing is that when the recipient receives the reply email, the paragraph structure is destroyed. Long paragraphs are being shown as shortened lines, which affects the readability of the email.

For example, the original paragraph:

"I know you are extremely busy, and there is a possibility my last email got buried. I would be happy to provide you with more details regarding my CV . Your opinion will be very helpful in deciding whether or not I have a chance to participate in your company. Regards"

Turns into shortened lines in the recipient's email:

"I know you are extremely busy, and there is a possibility my last

email got buried. I would be happy to provide you with more details

regarding my CV . Your opinion will be very

helpful in deciding whether or not I have a chance to participate in

your company.

Regards"

enter image description here

I should also add that the script reads the email body from another sheet cell which contains the reply email boxy text. In addition, I checked it and it is not a screen or display dimension or device screen problem and it is related to paragraph formatting of the received email body. It has been changed.

I want to preserve the paragraph structure in the reply email and ensure that long paragraphs are displayed as intended. Is there anything I need to do to achieve this using the Gmail API in Apps Script?

Any insights or suggestions would be greatly appreciated. Thank you in advance!

Hamed
  • 23
  • 8
  • It seems you have a similar case in SO, have you seen these: https://stackoverflow.com/questions/61351800/customizing-cell-format-script-when-sending-emails-from-gsheets and https://stackoverflow.com/questions/36529890/emailing-google-sheet-range-with-or-without-formatting-as-a-html-table-in-a-gm . Hope these give you an idea or help. – Twilight Jul 08 '23 at 07:12
  • @Twilight Thank you for providing the links. I checked them but unfortunately, they are using MailApp. I have used Gmail API in my script for sending replies to the first message of the thread and seems Gmail API does not support htmlbody. – Hamed Jul 08 '23 at 08:32
  • May I know what error you are encountering while using the htmlbody in Gmail API? – Twilight Jul 12 '23 at 00:42
  • @Twilight It does not give an error. The issue is that (as I described in the question), it destroys the paragraph formatting of the email body and does not keep the original formatting. I showed the final result in the question for the email body. Its lines have been shortened. – Hamed Jul 12 '23 at 09:35

1 Answers1

-1

The issue you're experiencing with the paragraph structure being disrupted in the recipient's email could be related to how line breaks and formatting are handled in plain text emails.

By default, the Gmail API sends emails as plain text, which doesn't support advanced formatting such as line breaks and paragraph structures. As a result, long paragraphs can appear as shortened lines when viewed in certain email clients.

To overcome this issue, you can modify your code to send the email as an HTML-formatted message instead of plain text. Here's an updated version of your code that sends the reply as an HTML email:

var originalMessage = messages[0];
var recipientEmail = originalMessage.getTo();

var messageId = originalMessage.getHeader("Message-ID");
var subject = "Re: " + originalMessage.getSubject();
var emailBodyHtml = "<p>" + emailBody.replace(/\n/g, "</p><p>") + "</p>";
var data = [
  "MIME-Version: 1.0\n",
  `In-Reply-To: ${messageId}\n`,
  `Subject: ${subject}\n`,
  `From: ${firstName} ${lastName} <${senderEmail}>\n`,
  `To: ${recipientEmail}\n`,
  "Content-Type: text/html; charset=UTF-8\n\n",
  emailBodyHtml,
].join("");
var draftReplyRequestBody = {
  message: {
    threadId: existingThreadId,
    raw: Utilities.base64EncodeWebSafe(data),
  }
};
var draftReplyResponse = Gmail.Users.Drafts.create(draftReplyRequestBody, "me");
var draftReplyId = draftReplyResponse.id;
var sendReplyParams = { id: draftReplyId };

Gmail.Users.Drafts.send(sendReplyParams, "me");

In this updated code, the emailBody variable is assumed to contain the plain text content of the email. It is transformed into an HTML-formatted email by replacing line breaks (\n) with paragraph tags (

). This allows the recipient's email client to interpret the content as HTML and display paragraphs correctly.

Note that sending emails as HTML may require some additional considerations, such as ensuring the HTML is properly formatted, handling any potential HTML injection issues, and considering the recipient's email client compatibility. However, it should address the specific issue of disrupted paragraph structures.

Make sure to test the updated code and adjust the HTML formatting as needed to achieve the desired results.

Joe Hick
  • 7
  • 2
  • I tried your code but gmail api changes the html body to plain texts and shows html tags instead if interpreting the html or preserving the paragraph structure. **[This](https://www.gmass.co/blog/gmail-api-html-plain-text-messages/.)** is an article about this problem – Hamed Jul 07 '23 at 20:51
  • This answer looks like it was generated by an AI (like ChatGPT), not by an actual human being. You should be aware that [posting AI-generated output is officially **BANNED** on Stack Overflow](https://meta.stackoverflow.com/q/421831). If this answer was indeed generated by an AI, then I strongly suggest you delete it before you get yourself into even bigger trouble: **WE TAKE PLAGIARISM SERIOUSLY HERE.** Please read: [Why posting GPT and ChatGPT generated answers is not currently acceptable](https://stackoverflow.com/help/gpt-policy). – tchrist Jul 08 '23 at 02:04
  • 1
    @tchrist Yes, I think so. – Hamed Jul 08 '23 at 07:04