1

Apologies if this has already been answered - I am quite new to this and probably need some guidance; if this is the wrong forum for that I would happily take some advice :)

I am in the process of exploring solutions for a semi-automated email drafting script using a Google Sheet as the source data. I have used a script I found on the internet (probably a terrible idea, but again I am just starting out) here.

The source document is in rich HTML text but the resulting Gmail draft is in plain text. Is there a way to ensure it generates in rich HTML?

Thanks - Hugo

1 Answer 1

1

EDIT UPDATED ANSWER:

I looked at your script more carefully. It's using a different function called createDraft(). It's not actually sending any emails, it's just created Drafts that you can send later. No problem. Here's the documentation for createDraft. createDraft can ALSO use "htmlBody" as a replacement parameter for the message body. That's what we'll do...

In your script code, change this:

// Create the email draft
      GmailApp.createDraft(
        config[emailField],   // Recipient
        emailSubjectUpdated,  // Subject
        emailBody             // Body
      );

to this:

// Create the email draft
      GmailApp.createDraft({
        to: config[emailField],   // Recipient
        subject: emailSubjectUpdated,  // Subject
        htmlBody : emailBody  // Body
      });

As long as your "emailBody" variable content is in html, your email draft should be saved as html tool.

But there's the rub. It's not trivial to convert a Google Document into a pure HTML file without additional external libraries. One solution is simply to forgo a Google Document, and create a real html file inside your script, and use that as your emailBody variable. HERE'S AN EXAMPLE implementation of this solution.

ORIGINAL ANSWER:

The script you linked to didn't appear to include the actual mail sending function. I wrote a script a while ago (for a spreadsheet) that automatically creates an email based on rows of a spreadsheet, and the email is composed with "rich" html.

Here's the mail sending function:

function sendEmail(to,sub,mes) {
  var t = "";
  var s = "";
  var m = "";
  sub == null || sub == undefined || sub == "" ? s = "Subject Failed" : s = sub;
  mes == null || mes == undefined || mes == "" ? m = "Message Failed!" : m = mes;
  to == null || to == undefined || to == "" ? t = "[email protected]" : t = to;
  MailApp.sendEmail({to: t, subject: s, htmlBody: m});
}

In this function, I pass in a few variables (the recipient "to", the subject "sub", and the message body "mes") and I make sure the function still works even if any of the variables are for some reason blank. The "mes" is my message body, and it's a string that's written in html. This is all just how I draft the email. You can simplify this greatly if you want (I'll include a simplified version below).

The part that matters is the final line MailApp.sendEmail({to:t,subject:s,htmlBody: m}); PAY PARTICULAR ATTENTION to the "htmlBody" part. If the sendEmail function doesn't have "htmlBody" as the property name for your message body, then it will just send it in plain text. You'll likely just need to find the function that has the "MailApp.sendEmail()" function in it, and adjust it to include the "htmlBody" property name for your message body.

Here's that function simplified:

function sendEmail(recipient, sub, mes) {
  MailApp.sendEmail({to: recipient, subject: sub, htmlBody: mes});
}

To read about all the options for sending email using Google Apps Script, read this page.

Sign up to request clarification or add additional context in comments.

7 Comments

Hi Doomd, Appreciate the link to the Google Apps Script knowledge base, will use this in the future. In regards to your solution, I am getting the following error when running the script: Exception: The parameters (String) don't match the method signature for GmailApp.createDraft. (line 37, file "Code")
I've been looking into it and basically it's not trivial to convert a Google Document into pure html. Would you consider simply creating a pure html file and using that as your source for the message? You can easily do this in the Google Apps Script Editor: File > New > HTML File and then you can use that file as your email message body variable.
I created an example google doc that will show you how to use your own html file with your existing script, and I included this example solution in my answer above.
Running the script you provided shows that I am able to generate an email that is formatted in HTML, thanks. I am however still confused about how to make sure the email has the variables from the spreadsheet included. Is there a way for the email to be HTML, but still receive information from the source spreadsheet?
I'm sorry I don't have time to customize your script for you, but I did find an excellent tutorial that you'll be able to customize to achieve what you're after. Basically, you want your message body to be a combination of variables that you set up in your spreadsheet. You can make the fixed template parts of your message fixed variables on your top rows (ex: body1[1], body2[1], body3[1]) and the parts you want unique for each row dependent on the row number (name[i], subject[i], etc). Remember to use the htmlBody property.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.