简体   繁体   中英

send email with different PDFs to different emails attachment

I am trying to do automate send email to multiple sender(10 emails) with different PDFs(10). Is there a way to attach a pdf in the spreadsheet column next to email address? Then send email with sales quote in pdf as attachment. Is this can be done in JS in google sheet.

Appreciate if someone help me out.

If you're using Google Sheets it's possible, but you have to get creative. In fact you can send emails, with javascript, from google sheets using google scripts. I think if you get creative with a hidden sheet in the same workbook, have a formula populate that sheet with the layout of the PDF, then export that sheet as a PDF to attach to the email... it's doable.

You may find some inspiration here .

Basic send email from google sheets example here .

So, JavaScript runs in the client side. It means that your code is running the user browser. In a brief, you can't send emails just using JavaScript. You're going to need a server to do that.

For example, you may use Spring Boot with Java that already has a lot implemented method that makes your task, ie, sending an email, super easy.

Have a look at http://spring.io/projects/spring-boot

You can do this by following the below steps,

  1. Uploading the 10 attachments on one google drive folder

  2. On your google sheet add the below script to get the file 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. Build a simple script to send emails and have your 10 emails on one column and all the File IDs generated from the above code on the next column. Below is a sample script to send emails from spreadsheets,
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

I hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM