2

I've created a report template which has the function of exporting the Google Sheet via PDF and then emails the created PDF using the GmailApp.sendEmail function. I've got this working perfectly, except I've noticed the emails sent out don't include the senders gmail signature.

Note I state the "Senders" email

I've read a fair few posts regarding signatures on Gmail API, default signatures, and even using a draft email but none help me as far as i can see. Here are my issues:

  1. As this is a template sheet, it can be used by anyone and therefore the email that is sent needs to use that particular users gmail signature. Most posts only provide a "default" signatures that are manually entered in the script.

  2. Other solutions I have seen which use "DRAFT" messages to pull this signature from my email, just flags up errors within my script or doesnt send an email at all.

So, to keep this question simple...

Is there a way I can include the "current users" Gmail signature when using the GmailApp.sendEmail function? And if possible, how do I add this into my script below?

The final section of script this applies to is as follows:

function sendSpreadsheetToPdf(sheetNumber, pdfName, email,subject, htmlbody) {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var spreadsheetId = spreadsheet.getId()
var sheetId = sheetNumber ? spreadsheet.getSheets()[sheetNumber].getSheetId() : null;
var url_base = spreadsheet.getUrl().replace(/edit$/,'');

var url_ext = 'export?exportFormat=pdf&format=pdf'   //export as pdf
+ (sheetId ? ('&gid=' + sheetId) : ('&id=' + spreadsheetId))
// following parameters are optional...
+ '&size=A4'      // paper size
+ '&portrait=true'    // orientation, false for landscape
+ '&fitw=true'        // fit to width, false for actual size
+ '&sheetnames=true&printtitle=false&pagenumbers=true'  //hide optional headers and footers
+ '&gridlines=false'  // hide gridlines
+ '&fzr=false';       // do not repeat row headers (frozen rows) on each page

var options = {
headers: {
'Authorization': 'Bearer ' +  ScriptApp.getOAuthToken(),
}
}

var response = UrlFetchApp.fetch(url_base + url_ext, options);
var blob = response.getBlob().setName(pdfName + '.pdf');
if (email) {
var mailOptions = {
attachments:blob, htmlBody:htmlbody
}

GmailApp.sendEmail(
email,
subject+" " + pdfName +"",
"html content only",
mailOptions);
}

This is tried and tested and is happily sending my active sheet via PDF, but no signature is present!

Any thoughts?

Rubén
  • 34,714
  • 9
  • 70
  • 166
R_Sy_K
  • 31
  • 5

1 Answers1

3

Adding a signature is a setting on the clientside of the Gmail Web UI, it is not available via API

You need to manually fetch your signature with the Users.settings.sendAs: get method of the Gmail API and manually append it to the end of you email body

Sample code snippet in Apps Script:

  var signature = Gmail.Users.Settings.SendAs.get("me", "your email or alias").signature; 
  htmlbody += "<br>" + signature;

UPDATE

Full code:

function sendSpreadsheetToPdf(sheetNumber, pdfName, email,subject, htmlbody) {
  var signature = Gmail.Users.Settings.SendAs.get("me", "your email or alias").signature; 
  htmlbody += "<br>" + signature;
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  var spreadsheetId = spreadsheet.getId()
  var sheetId = sheetNumber ? spreadsheet.getSheets()[sheetNumber].getSheetId() : null;
  var url_base = spreadsheet.getUrl().replace(/edit$/,'');
  
  var url_ext = 'export?exportFormat=pdf&format=pdf'   //export as pdf
  + (sheetId ? ('&gid=' + sheetId) : ('&id=' + spreadsheetId))
  // following parameters are optional...
  + '&size=A4'      // paper size
  + '&portrait=true'    // orientation, false for landscape
  + '&fitw=true'        // fit to width, false for actual size
  + '&sheetnames=true&printtitle=false&pagenumbers=true'  //hide optional headers and footers
  + '&gridlines=false'  // hide gridlines
  + '&fzr=false';       // do not repeat row headers (frozen rows) on each page
  
  var options = {
    headers: {
      'Authorization': 'Bearer ' +  ScriptApp.getOAuthToken(),
    }
  }
  
  var response = UrlFetchApp.fetch(url_base + url_ext, options);
  if (email) {
    var mailOptions = {
      attachments:blob, htmlBody:htmlbody
    }
    
    GmailApp.sendEmail(
      email,
      subject+" " + pdfName +"",
      "html content only",
      mailOptions);
  }
ziganotschka
  • 25,866
  • 2
  • 16
  • 33
  • Thanks ziganotschka, How can this be implemented within my script? Its flagging a "Gmail not defined" error. – R_Sy_K Jul 22 '20 at 10:48
  • `Gmail is not defined` usually means that you did not enable the Advanced Gmail service. If you follow the link in my answer about how to [enable](https://developers.google.com/apps-script/guides/services/advanced#enabling_advanced_services) you will see a detailed description. Basically you have to go on `Resources - >Advanced Google Services` and toggle the button right of `Gmail API` to `On`. – ziganotschka Jul 22 '20 at 12:02
  • I wasnt able to implement the Gmail API due to my user access but I have sorted this now. Could you highlight how I can implement this new script into my existing script? Im extremely new to script writing! – R_Sy_K Jul 22 '20 at 12:46
  • Sure - you can simply insert those lines at the beginning of your function - provided your funciton worked before and you pass the correct parameters and retrieve the right data from the sheet (which I cannot test without your data) it should work as intended. However, depending on what your `htmlbody` looks like now - you might need to make the hmtl tags a bit different. – ziganotschka Jul 22 '20 at 13:09
  • 1
    Took some modifying but it worked! Super stuff ziganotschka. Thank you very much for the help and patience. – R_Sy_K Jul 22 '20 at 15:02