【问题标题】:send email with different PDFs to different emails attachment将具有不同 PDF 的电子邮件发送到不同的电子邮件附件
【发布时间】:2019-01-30 01:24:14
【问题描述】:

我正在尝试使用不同的 PDF (10) 自动向多个发件人 (10 封电子邮件) 发送电子邮件。有没有办法在电子邮件地址旁边的电子表格列中附加 pdf?然后以 pdf 格式的销售报价作为附件发送电子邮件。这可以在google sheet中的JS中完成吗?

感谢有人帮助我。

【问题讨论】:

  • 你在客户端做这个? JavaScript 除了 mailto 并没有附加文件的能力之外,实际上并没有任何发送电子邮件的能力。

标签: javascript google-apps-script google-sheets google-docs email-attachments


【解决方案1】:

如果您使用的是 Google 表格,则可以,但您必须要有创意。事实上,您可以使用 google 脚本从 google sheet 发送带有 javascript 的电子邮件。我认为,如果您在同一个工作簿中使用隐藏工作表获得创意,让公式使用 PDF 的布局填充该工作表,然后将该工作表导出为 PDF 以附加到电子邮件......这是可行的。

你可能会找到一些灵感here

从谷歌表格发送电子邮件的基本示例here

【讨论】:

    【解决方案2】:

    因此,JavaScript 在客户端运行。这意味着您的代码正在运行用户浏览器。简而言之,您不能仅使用 JavaScript 发送电子邮件。您将需要一台服务器来执行此操作。

    例如,您可以将 Spring Boot 与 Java 一起使用,该 Java 已经实现了很多使您的任务(即发送电子邮件)变得超级简单的方法。

    看看http://spring.io/projects/spring-boot

    【讨论】:

      【解决方案3】:

      您可以按照以下步骤进行操作,

      1. 将 10 个附件上传到一个 Google Drive 文件夹

      2. 在您的 google 表格中添加以下脚本以获取文件 ID

      function listFilesInFolder() {
      
          SpreadsheetApp.getActiveSpreadsheet().getSheetByName("FileID").activate(); //The name of the google sheet is FileID in my case
      
          var sheet = SpreadsheetApp.getActiveSheet();
          sheet.appendRow(["File Name", "File-Id"]);
      
        //change the folder ID below to reflect your folder's ID (look in the URL when you're in your folder)
      
          var folder = DriveApp.getFolderById("*[Enter Folder ID here]*");
          var contents = folder.getFiles();
      
          var cnt = 0;
          var file;
      
          while (contents.hasNext()) {
              var file = contents.next();
              cnt++;
      
                 data = [
                      file.getName(),
                      file.getId(),
                  ];
      
                  sheet.appendRow(data);
          }
      }
      
      1. 构建一个简单的脚本来发送电子邮件,并将您的 10 封电子邮件放在一列中,将上述代码生成的所有文件 ID 放在下一列中。以下是从电子表格发送电子邮件的示例脚本,
      function sendEmails() {
      
        SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Emails").activate(); // Emails is the sheet which has the email and File ID columns
      
        var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
        var lr = ss.getLastRow();
      
        var templateText =  SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Template").getRange(1, 1).getValue();  // Template is the sheet which has my email body
      
        for (var i = 2;i<=lr;i++){
      
          var currentEmail = ss.getRange(i, 1).getValue();  //Email IDs are on the first column for me
      
          var currentAttachment = ss.getRange(i, 2).getValue();    //Assuming your File IDs are on the second column
      
      
          var waiver = DriveApp.getFileById(currentAttachment);
          var liabilityWaiver = waiver.getAs(MimeType.PDF);
      
      MailApp.sendEmail(currentEmail, subjectLine, messageBody, {attachments:[liabilityWaiver],
                                                                     cc:'abc@abc.com'});  // the cc is in case you want to CC someone else on the email
      
      

      我希望这会有所帮助!

      【讨论】:

        猜你喜欢
        • 2020-12-20
        • 1970-01-01
        • 2018-11-06
        • 2015-10-30
        • 2014-11-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-09
        相关资源
        最近更新 更多