简体   繁体   中英

How to include the users Gmail signature when using GmailApp.sendEmail from Google Sheet

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?

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);
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM