我要向很多人發送電子郵件,因此我使用 Google Apps 腳本自動執行了此操作。將附件作為 PDF 和 jpeg 檔案發送一切都很順利,但我不知道如何發送 docx 檔案。
我會給你留下下面的代碼,我希望它有助于解決這個問題:
// Excel fields columns counting from 1
var email_position = 7;
var firstname_position = 1;
var lastname_position = 2;
var yesno_position = 11;
var sheet_name="Sheet1";
// DOCS ID - go to drive -> select document -> get link -> get the link between /d/.../view? -> we need what's in the '...'
// PDF
var TERMS_AND_CONDITIONS_ID = "1e3JzyHceUpPx_TNLeq4FvyTvqyu88--E"; // ***THIS SHOULD BE DOCX***
// IMAGES
var IMAGE_1_ID = "1VZP8m0mpqoGTMNrDJjnKnwaAUtgNHXVA";
var IMAGE_2_ID = "18fxaA1-oylleZ4W8E9tF0yr2p-uYN_O4";
// Email
var EMAIL_SUBJECT = "SUBJECT";
// Please modify the body yourself in HTML code, LINE 170
// Main function
function sendEmail() {
// ACTIVATE THE "Sheet1" SHEET AND MAKE IT THE PRINCIPAL ONE
SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheet_name).activate();
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
// GET THE INDEX OF THE LAST ROW
var last_row = spreadsheet.getLastRow();
// Check if we can still send emails today
if(check_emails_status() == 1) {
return;
}
// Get attachments // File id from drive
var pdf1 = DriveApp.getFileById(TERMS_AND_CONDITIONS_ID);
var img1 = DriveApp.getFileById(IMAGE_1_ID);
var img2 = DriveApp.getFileById(IMAGE_2_ID);
// SEND AN EMAIL TO EVERY PARTICIPANT
for(var participant_row = 2; participant_row <= last_row; participant_row) {
// Check if we didn't already send an email to that person
if(check_already_sent(spreadsheet, participant_row) == 1) {
continue;
}
// Check duplicate
if(participant_row < last_row && get_email(spreadsheet,participant_row).localeCompare(get_email(spreadsheet,participant_row 1)) == 0)
continue;
// Check if we have emails left to send in the google's limits
if(MailApp.getRemainingDailyQuota() <= 0) {
log_errors();
break;
}
// Send the emails
send_email(spreadsheet, participant_row, img1, img2, pdf1);
// Mark the person as someone who has received the email to not receive another one
spreadsheet.getRange(participant_row, yesno_position).setValue("YES");
// Mark duplicates as sent as well
var nr_of_duplicates = count_duplicate(spreadsheet, participant_row, last_row);
for(var i=1; i<=nr_of_duplicates;i ) {
var get_value = spreadsheet.getRange(participant_row,yesno_position).getValue();
spreadsheet.getRange(participant_row - i, yesno_position).setValue(get_value);
}
} // end loop
}
// Check how many emails we can send today
// The limit for Google Scripts is 100 emails / 24h!
function check_emails_left() {
var EMAILS_ABLE_TO_SEND_TODAY_LEFT = MailApp.getRemainingDailyQuota();
Logger.log("Emails left for today: " EMAILS_ABLE_TO_SEND_TODAY_LEFT.toString());
return EMAILS_ABLE_TO_SEND_TODAY_LEFT;
}
// Check if we can still send emails today
function check_emails_status() {
var EMAILS_ABLE_TO_SEND_TODAY_LEFT = check_emails_left();
if(EMAILS_ABLE_TO_SEND_TODAY_LEFT == 0) {
Logger.log("Warning. No more emails to send today! Limit reached!");
Browser.msgBox("Warning. No more emails to send today! Limit reached!");
return 1;
}
else {
return 0;
}
}
// Get person's email
function get_email(spreadsheet, participant_row) {
// ROW , COLUMN
return spreadsheet.getRange(participant_row, email_position).getValue();
}
// Get person's name
function get_name(spreadsheet, participant_row) {
// FIRST NAME // LAST NAME
return spreadsheet.getRange(participant_row, firstname_position).getValue() " " spreadsheet.getRange(participant_row, lastname_position).getValue();
}
// Dont send the same email to the same person
function check_already_sent(spreadsheet, participant_row) {
var bool_alreadySentEmail = spreadsheet.getRange(participant_row, yesno_position).getValue();
if(bool_alreadySentEmail.toString().toUpperCase() == "YES") {
Logger.log("Skipped " spreadsheet.getRange(participant_row, email_position).getValue());
return 1.
}
else {
Logger.log("Sending to " spreadsheet.getRange(participant_row, email_position).getValue());
return 0;
}
}
// Create email subject
function get_subject() {
return EMAIL_SUBJECT;
}
// Display error if we can no longer send emails today
function log_errors() {
Logger.log("Warning. No more emails to send today! Limit reached! The person who is yet to receive the email is " NAME "!");
Browser.msgBox("Warning. No more emails to send today! Limit reached! The person who is yet to receive the email is " NAME "!");
}
// Send the email to each person
function send_email(spreadsheet, participant_row, img1, img2, pdf1) {
MailApp.sendEmail({
to: get_email(spreadsheet, participant_row),
subject: get_subject(),
body: "",
htmlBody: get_body(get_name(spreadsheet, participant_row), spreadsheet, participant_row),
inlineImages: {sampleImage: img1.getBlob(), sampleImage2: img2.getBlob()}//,
//attachments:[pdf1.getAs(MimeType.PDF)] ***THIS SHOULD BE DOCX***
});
}
// Check for duplicate entry in the excel sheet
function count_duplicate(spreadsheet, participant_row, last_row) {
var duplicates = 0;
var counter = 1;
for(var i = participant_row; i >=0 ; i--)
if(get_email(spreadsheet, participant_row).localeCompare(get_email(spreadsheet, participant_row - counter)) == 0) {
duplicates ;
counter ;
}
else
break;
return duplicates;
}
// Create email body
function get_body(NAME, spreadsheet, participant_row) {
return "HTML TEXT <img src=\"cid:sampleImage\" width=200 height=200><img src=\"cid:sampleImage2\" width=200 height=200>"
}
我想要的是用 docx 檔案替換該 pdf 檔案。在那里提供另一個鏈接很簡單,因為它只是一個var,但是在send_email函式中,我不知道要附加它。
uj5u.com熱心網友回復:
你需要另一個 MimeType
我不使用 PDF mymetype,而是使用檔案中提到的MICROSOFT_WORD列舉。通過進行此更改,您的函式將如下所示:send_email
MailApp.sendEmail({
to: get_email(spreadsheet, participant_row),
subject: get_subject(),
body: "",
htmlBody: get_body(get_name(spreadsheet, participant_row), spreadsheet, participant_row),
inlineImages: {sampleImage: img1.getBlob(), sampleImage2: img2.getBlob()},
attachments:[blob.getAs(MimeType.MICROSOFT_WORD)]
});
其中pdf1是blob。確保您的pdf1變數是一個 blob,否則此代碼將引發錯誤。否則,您必須事先轉換它。
建議更改
為了避免被某些 Gmail 帳戶拒絕,我強烈建議您GmailApp改用。
GmailApp.sendEmail(get_email(spreadsheet, participant_row), get_subject(), 'body', {
inlineImages: {sampleImage: img1.getBlob(), sampleImage2: img2.getBlob()},
attachments: [blob.getAs(MimeType.MICROSOFT_WORD)],
name: 'sender name'
});
參考
- 列舉 MimeType
- getAs(contentType)
- sendEmail(收件人,主題,正文,選項)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/312937.html
標籤:javascript 电子邮件 谷歌应用程序脚本 谷歌表格 自动化
上一篇:決議器創建的郵件訊息的問題
