Deepak Prabhu
Deepak Prabhu

Reputation: 219

Sending mail with attachment in Google Apps script throws exception

I am trying to attach a html document (from Google drive) in my mail using the below piece of code:

function myFunction() {
    var message = 'Hi';
    var fileId ='0B0azXoe_2qFTzNYa1p5eUd0c2s'; // My html document ID
    var file = DriveApp.getFileById(fileId);
    var subject = 'Saved Transaction Cleardown logs';
    var blob = Utilities.newBlob('mail', 'application/vnd.google-apps.document', 'stc.html');  // I am not sure about the above line, Hope that is where i am wrong
      MailApp.sendEmail('[email protected]', subject,
                    message,
                    { attachments: [file.getAs(MimeType.HTML), blob],
                       name: 'Automatic Emailer Script'
                    });
} 

Exception received: We're sorry, a server error occurred. Please wait a bit and try again.

Upvotes: 1

Views: 983

Answers (1)

Riyafa Abdul Hameed
Riyafa Abdul Hameed

Reputation: 7973

This is a more appropriate way to achieve your objective:

function myFunction() {
    var message = 'Hi';
    var fileId ='0ByLGhvnaCIQsZXcteeRWd0e344eFE'; // My html document ID
    var file = DriveApp.getFileById(fileId);
    var subject = 'Saved Transaction Cleardown logs';
      GmailApp.sendEmail('[email protected]', subject,
                    message,
                    { attachments: [ file],
                       name: 'Automatic Emailer Script'
                    });
}

Change the id of the html file. You don't even need to get the blob. Check here

Upvotes: 1

Related Questions