2

我是 Google Apps 脚本的新手,我正在尝试将图像内嵌到自动回复电子邮件中。自动回复工作完美,电子邮件的主要文本格式为纯文本和 html。我面临的问题是图像没有出现。

我的代码:

// This constant is written in column Y for rows for which an email
// has been sent successfully.
var EMAIL_SENT = 'EMAIL_SENT';

/**
 * Sends non-duplicate emails with data from the current spreadsheet.
 */
function sendEmails2() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  SpreadsheetApp.setActiveSheet(sheet.getSheetByName('Data'))
  var startRow = 2; // First row of data to process


  // Fetch the range
  var dataRange = sheet.getRange("L2:L1000")
  var dataRange2 = sheet.getRange("K2:K1000")
  var dataRange3 = sheet.getRange("O2:O1000")
  var dataRange4 = sheet.getRange("Y2:Y1000")
  var dataRange5 = sheet.getRange("B2:B1000")
  // Fetch values for each row in the Range.
  var data = dataRange.getValues();
  var data2 = dataRange2.getValues();
  var data3 = dataRange3.getValues();
  var data4 = dataRange4.getValues(); 
  var data5 = dataRange5.getValues();

    for (var i = 0; i < data.length; ++i) {
        var yesno = data2[i]
        if(yesno == "Yes"){
        var TFlogoUrl = "https://drive.google.com/openid=1nzmvP_zzOms1HiBoFCsVLFjDM6ZzM287";
        var TFlogoBlob = UrlFetchApp
                            .fetch(TFlogoUrl)
                            .getBlob()
                            .setName("TFlogoBlob");
        var emailAddress = data[i];
        var ShipID = data3[i];
        var cmdrID = data5[i];
        var TFmsg = "Hi " + cmdrID + ",/n /nThank you for signing up to The  Fatherhoods Lost Souls Expedition./n /nYour unique Ship ID is: " + ShipID + "/n /nWe look forward to seeing you on the expedition CMDR!/n /nFly Safe,/nThe Lost Souls Expedition team.";
        var htmlTFmsg = "Hi " + cmdrID + ",<br> <br>Thank you for signing up to The Fatherhoods Lost Souls Expedition.<br> <br>Your unique Ship ID is: " + ShipID + "<br> <br>We look forward to seeing you on the expedition CMDR!<br> <br>Fly Safe,<br>The Lost Souls Expedition team.<br><img src='cid:TFlogo'>";             
        emailSent = data4[i]; // email sent (column Y)
        if (emailSent != EMAIL_SENT) { // Prevents sending duplicates
         var subject = "Lost Souls Expedition Sign up confirmation";
         MailApp.sendEmail(emailAddress,subject,TFmsg,{
           htmlBody: htmlTFmsg,
           inlineImage: 
           { 
             TFlogo:TFlogoBlob
           }
         });
         sheet.getRange("Y" + (startRow + i)).setValue(EMAIL_SENT);
         // Make sure the cell is updated right away in case the script is interrupted
         SpreadsheetApp.flush();
       }
     }
   }
}
4

1 回答 1

0

这个改装怎么样?

修改点:

  • 您无法从此 URL 检索文件 blob var TFlogoUrl = "https://drive.google.com/openid=1nzmvP_zzOms1HiBoFCsVLFjDM6ZzM287";。如果要从 URL 检索文件 blob,请使用var TFlogoUrl = "http://drive.google.com/uc?export=view&id=1nzmvP_zzOms1HiBoFCsVLFjDM6ZzM287";. 1nzmvP_zzOms1HiBoFCsVLFjDM6ZzM287是文件 ID。
    • 作为另一种方法,从文件ID中发现 和 的值分别是getSharingAccess()和。因此,您还可以使用. 我推荐这个。getSharingPermission()ANYONE_WITH_LINKVIEWvar TFlogoBlob = DriveApp.getFileById("1nzmvP_zzOms1HiBoFCsVLFjDM6ZzM287").getBlob().setName("TFlogoBlob");
  • 当您想使用内联图像发送电子邮件时,请从 修改inlineImageinlineImages

反映以上几点的脚本如下。

修改后的脚本:

请按如下方式修改您的脚本。

从:
var TFlogoUrl = "https://drive.google.com/openid=1nzmvP_zzOms1HiBoFCsVLFjDM6ZzM287";
var TFlogoBlob = UrlFetchApp.fetch(TFlogoUrl).getBlob().setName("TFlogoBlob");
至:
var id = "1nzmvP_zzOms1HiBoFCsVLFjDM6ZzM287";
var TFlogoBlob = DriveApp.getFileById(id).getBlob().setName("TFlogoBlob");

从:
inlineImage: {TFlogo:TFlogoBlob}
至:
inlineImages: {TFlogo:TFlogoBlob}

参考:

如果我误解了你的问题,请告诉我。我想修改它。

于 2018-09-14T01:46:54.540 回答