Víctor Enrich
Víctor Enrich

Reputation: 71

I am getting an Exception error when trying to create Gmail drafts with a script, can you help me fix this?

I am trying to create gmail drafts using the following script

  function sendEmails(){

      var ss = SpreadsheetApp.getActive().getSheetByName('SendMail')
      var lr = ss.getLastRow();
      var lc = ss.getLastColumn();

      var quotaLeft = MailApp.getRemainingDailyQuota();

       if((lr-1) > quotaLeft) {
         Browser.msgBox("You have " + quotaLeft + " left and you're trying to send " + 
        (lr-1) + " emails. Emails were not send.");
       } else {

        for (var i = 2;i<=lr;i++){

      var currentEmail = ss.getRange(i, 1).getValue();
      var currentSubject = ss.getRange(i, 2).getValue();
      var templateText = ss.getRange(i, 3).getValue();
      var currentname = ss.getRange(i, 4).getValue();
      var currentcity = ss.getRange(i, 8).getValue(); 
      var currentfirm = ss.getRange(i, 12).getValue();     
      var reply = ss.getRange(i, 5).getValue();
      var imageID = ss.getRange(i, 6, 1, lc).getValues().toString().split(',').filter(String);
      var image = {};    
      var message = templateText.replace("{name}",currentname).replace("{city}",currentcity).replace("{firm}",currentfirm);                     
      var signature = ss.getRange(i, 7).getValue(); 

          for (var x = 0; x < imageID.length; x++){
            try{
          image["inlineImage"+x] = DriveApp.getFileById(imageID[x]).getBlob();   
          message += '<br/><img src="cid:' + "inlineImage"+x +'" />';
            } catch (e) {
                templateText += "<p>Image merge failed: " + e;         
           }
           }

      if(image){
      GmailApp.createDraft({
        to: currentEmail,
        replyTo: reply,
        subject: currentSubject,
        htmlBody: message + signature,
        inlineImages: image,    
      });

        } else {

      GmailApp.createDraft({
        to: currentEmail,
        replyTo: reply,
        subject: currentSubject,
        htmlBody: message + signature,  
      });
      }

        } //close for loop

      } //close else statement

     } //close sendEmails

This script worked perfectly when, instead of using GmailApp.createDraft, I was using mailApp.sendEmail.

When running the script nothing happens and I get the following error:

"Exception: The parameters (String) don't match the method signature for GmailApp.createDraft"

Is there a way to find what is causing the error to appear?

Thanks

Upvotes: 0

Views: 138

Answers (1)

Tanaike
Tanaike

Reputation: 201358

The arguments of createDraft(recipient, subject, body, options) are recipient, subject, body, options. From This script worked perfectly when, instead of using GmailApp.createDraft, I was using mailApp.sendEmail., I think that this is the reason of your error message. So in your script, please modify as follows.

From:

if(image){
  GmailApp.createDraft({
  to: currentEmail,
  replyTo: reply,
  subject: currentSubject,
  htmlBody: message + signature,
  inlineImages: image,    
  });
} else {
  GmailApp.createDraft({
  to: currentEmail,
  replyTo: reply,
  subject: currentSubject,
  htmlBody: message + signature,  
  });
}

To:

if(image){
  GmailApp.createDraft(
    currentEmail,
    currentSubject,
    "",
    {replyTo: reply, htmlBody: message + signature, inlineImages: image}
  );
} else {
  GmailApp.createDraft(
    currentEmail,
    currentSubject,
    "",
    {replyTo: reply, htmlBody: message + signature}
  );
}

Reference:

Upvotes: 1

Related Questions