无法在Google Apps脚本中向电子邮件添加嵌入式图像

时间:2018-09-13 12:24:45

标签: email google-apps-script inline-images

我是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();
       }
     }
   }
}

1 个答案:

答案 0 :(得分:0)

此修改如何?

修改点:

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

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

修改后的脚本:

请按如下所示修改脚本。

从:
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}

参考文献:

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