3

I have an email sender linked to a google sheets file as follows:

function sendEmail(form) {
const sSheet = SpreadsheetApp.getActiveSpreadsheet(); 
const file = DriveApp.getFileById(sSheet.getId());
const documentUrl = file.getUrl();

var toEmail = form.toAddress;  
var ccEmail = form.ccAddress;  
var subject = form.subject;
var message = form.message;


const folderId = "";
const ssId = sSheet.getId();
const returnFlag= 'blob';

if (form.process == "sendHotelRegistration"){
var fileName = "Hotel Registration Form";
var sheetId = sSheet.getSheetByName("Hotel Registration form").getSheetId();
var sheet= sSheet.getSheetByName("Hotel Registration form");
sheet.showSheet();
} else if (form.process == "updateRegistry"){
var fileName = "Hotel Contract Document";
var sheetId = "All";
}

var pdfFile = createPDF(folderId, ssId, fileName, sheetId, returnFlag);

MailApp.sendEmail({
    to:toEmail,
    cc:ccEmail,
    subject:subject,
    body:message,
    attachments:[pdfFile]
    });
}

I have 2 issues:

1- I want to add a signature including a logo to the email. So I need the company name in bold and the logo. I cannot work out how to insert an image or to have formatted text. I have read through a lot of posts and the following google documentation: https://developers.google.com/apps-script/reference/mail/mail-app

And I have tried to implement this in my code but I cannot get it to work.

Secondly, I have a variable set in my email body which retrieves a value from my google sheet document as follows:

html.contractorName = data.contractorName.toLowerCase();

However, I don't actually want lower case, I want title / proper case. I cannot though work out how to implement this, particularly bearing in mind that the contractor name may be two, three, four names long.

Thanks

Umair Mohammad
  • 4,489
  • 2
  • 20
  • 34
DevB1
  • 1,235
  • 3
  • 17
  • 43

4 Answers4

4

I have a similar sheetsEmail document. The logo problem took me a long time to solve, I recently found a semi-convenient solution from the following source.
The relevant code can take your personal signature as a string, and with htmlBody it can include images, all the desired formatting and what not.

var signature = Gmail.Users.Settings.SendAs.list("me").sendAs.filter(function(account){if(account.isDefault){return true}})[0].signature;

From your comment on another reply, you are correct that it causes some problems with your regular body. My solution was to include html tags around it as well, and concatenate the two strings. So, for example, my original body is stored in the var bod:

var body1 = "<p>"+bod+"</p>"+signature;

You could fool around with different tags to get the desired format. To solve your spaces and new lines issue, you could search your original body string and replace line break \n with the html br> tag, but I can't help you on the specifics of that!

Dylan Gimpelj
  • 306
  • 3
  • 11
1

You can attach your logo and name to the message and send email in HTML format.

Something like this

message += "<BR><BR>" + name + "<BR><BR> <inline logo image>";

You can use htmlBody parameter(to add name logo to email body) and inlineImages options (to attach logo). Check this for full reference.

https://developers.google.com/apps-script/reference/mail/mail-app#sendemailmessage

Umair Mohammad
  • 4,489
  • 2
  • 20
  • 34
  • 2
    This is almost perfect. The only problem is that the htmlBody paramter is messing with the actual message body which is being pulled from a text input. For example, with the simple body parameter, it picks up spaces and new lines that are written into the text input, however with htmlBody, it gets rid of these – DevB1 Jan 30 '18 at 12:25
  • Can you try to convert spaces and new lines to &nbsp and
    programmatically ? So that message text is converted into html. Maybe you'll find these link helpful https://stackoverflow.com/questions/8579093/converting-new-lines-to-paragraph-br-html-tags-can-this-be-a-single-regex https://stackoverflow.com/questions/4535888/jquery-text-and-newlines
    – Umair Mohammad Jan 30 '18 at 14:34
  • You can up vote/accept the answer so that other people can get benefit from it. – Umair Mohammad Jan 30 '18 at 14:35
1

Please read MailApp.sendEmail() documentation here :

 // This code fetches the Google and YouTube logos, inlines them in an email
 // and sends the email
 function inlineImage() {
   var googleLogoUrl = "http://www.google.com/intl/en_com/images/srpr/logo3w.png";
   var youtubeLogoUrl =
         "https://developers.google.com/youtube/images/YouTube_logo_standard_white.png";
   var googleLogoBlob = UrlFetchApp
                          .fetch(googleLogoUrl)
                          .getBlob()
                          .setName("googleLogoBlob");
   var youtubeLogoBlob = UrlFetchApp
                           .fetch(youtubeLogoUrl)
                           .getBlob()
                           .setName("youtubeLogoBlob");
   MailApp.sendEmail({
     to: "recipient@example.com",
     subject: "Logos",
     htmlBody: "inline Google Logo<img src='cid:googleLogo'> images! <br>" +
               "inline YouTube Logo <img src='cid:youtubeLogo'>",
     inlineImages:
       {
         googleLogo: googleLogoBlob,
         youtubeLogo: youtubeLogoBlob
       }
   });
 }

Get the Blob of your logo and assign it to inlineImages property. Then you can include it in your HTML body. Use HTML to format the body according to your needs and assign it to htmlBody property. About the contractor name, try with this prototype function:

html.contractorName = data.contractorName.toProperCase();

String.prototype.toProperCase = function () {
      return this.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
};

It will capitalize each word in the string so john dOe becomes John Doe.

k4k4sh1
  • 784
  • 6
  • 12
0

You're seeing the Reference error because you don't have the Gmail API enabled. You can enable the API in the developer console. You will then have to enable it for the individual script you're working on. Clicking Resources > Advanced Google services then flip the on-switch next to Gmail API

Donny
  • 1
  • 1