MailApp.sendEmail通过电子邮件向不是收件人的用户发送电子邮件

时间:2020-02-19 16:14:09

标签: google-apps-script

(注意:为清晰起见,我编辑了标题)

我一直在试图找出导致此问题的原因,但无济于事。

我创建了一个Google脚本,允许请求者提交Google表格以供批准。提交后,它将通过电子邮件发送给请求者。它还会通过电子邮件向批准者发送批准或拒绝批准的选项。我的问题是:请求者也收到了相同的批准者邮件。

以下是代码的快照,供您查看和提供支持:

function emailSpreadsheetAsPDF(time, approverEmail, requesterEmail, financeEmail, approvalNeeded) {

var timestamp = time;
var approverEmail = approverEmail;
var requesterEmail = requesterEmail;
var financeEmail = financeEmail;
var approvalNeeded = approvalNeeded;
Logger.log("Approver: " + approverEmail);
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Form"); // Enter the name of the sheet here

//*******************************************
//* Enter the name of the TEMP folder ID here
//*******************************************

var TEMPfolderid = "xxxyyyzzz";     // TEMP folder to store the PDF export
var body4Requester = "\n\nAttached is a PDF copy of the Price Maintenance Form that you submitted. \n\nPlease note that you will be automatically notified via email when: \n(a) Your request is approved (or disapproved), and \n(b) When it is approved and already processed in SAP. \n\nAPAC Finance Team";
var body4Finance = "\n\nAttached is a PDF copy of the Price Maintenance Form submitted by " +  requesterEmail + ". It does not require an approval as per current policy.\n\nKindly process.";

var url = "https://docs.google.com/spreadsheets/d/SS_ID/export?".replace("SS_ID", ss.getId());

// Specify PDF export parameters
var url_ext = 'exportFormat=pdf&format=pdf' // export as pdf / csv / xls / xlsx
     + '&size=A4' // paper size legal / letter / A4
     + '&portrait=false' // orientation, false for landscape
     + '&fitw=true&source=labnol' // fit to page width, false for actual size
     + '&sheetnames=false&printtitle=false' // hide optional headers and footers
     + '&pagenumbers=false&gridlines=false' // hide page numbers and gridlines
     + '&fzr=false' // do not repeat row headers (frozen rows) on each page
     + '&gid='; // the sheet's Id

var token = ScriptApp.getOAuthToken();
var response = UrlFetchApp.fetch(url + url_ext + sheet.getSheetId(), {
        headers : {
            'Authorization' : 'Bearer ' + token
        },
        muteHttpExceptions: true
    });

var pdfFile = response.getBlob();

//Create the file...
var file = DriveApp.createFile(pdfFile).setName(timestamp + ".pdf");

//Store the file's ID for retrieval later during approval and post-SAP processing
var PDFfileID = file.getId();

//Save the file in TEMP folder 
DriveApp.getFolderById(TEMPfolderid).addFile(file);    

//Now, get the file and email as attachment
var blob = DriveApp.getFileById(file.getId());

// Email with Approve/Disapprove link
var url = ScriptApp.getService().getUrl();

var options = '?approval=%APPROVE%&timestamp=%TIMESTAMP%&reply=%EMAIL%&pdffileid=%FILEID%'
     .replace("%TIMESTAMP%",encodeURIComponent(timestamp))
     .replace("%EMAIL%",requesterEmail)    
     .replace("%FILEID%",PDFfileID);  
var approve = url+options.replace("%APPROVE%","Approved"); 
var disapprove = url+options.replace("%APPROVE%","Disapproved");

var html = "<body>"+
            "Attached is the Price Change request<br /><br />"+
            "<a href="+ approve +">Approve</a><br />"+
            "<a href="+ disapprove +">Disapprove</a><br />"+
         "</body>";

if (approvalNeeded == "Yes") {           //send email with Approve and Disapprove option to Approver
    MailApp.sendEmail(approverEmail, "TEST ONLY: Price Maintenance Form for your approval", body4Requester, {  
                htmlBody: html,
                attachments : [blob]
                });

    MailApp.sendEmail(requesterEmail, "TEST ONLY: Your Price Maintenance request has been submitted", body4Requester, {  
                attachments : [blob]
                });

} else {                                 //send straight to Finance
    MailApp.sendEmail(financeEmail, "TEST ONLY: Price Maintenance Form for processing. No approval neeed", body4Finance, {
                cc:requesterEmail,
                attachments : [blob]
                }); 
}}

不确定是否相关-我将其部署为网络应用程序,设置为“以用户身份访问网络应用程序执行应用程序”。

在此先感谢您的任何建议。

1 个答案:

答案 0 :(得分:0)

我尝试用GmailApp.sendEmail替换MailApp.sendEmail。 有效。