Google Script : cara mengirim file melalui email

 

Berikut adalah contoh script singkat untuk mengirim file dari komputer menggunaka Google Script.

FORM HTML :

<!DOCTYPE html>

<html>

<head>

    <title>Sample script for uploading file to Google Drive without authorization</title>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.js"></script>

</head>

<body>

    <form action="https://script.google.com/macros/s/AKfycbzPMl1emJ3C........................uWBjb1ICMw/exec" id="form" method="post">

        Upload a file

        <div id="data"></div>

        <input name="file" id="uploadfile" type="file">

        <input id="submit" type="submit">

    </form>

    <script>

    $('#uploadfile').on("change", function() {

        var file = this.files[0];

        var fr = new FileReader();

        fr.fileName = file.name;

        fr.onload = function(e) {

            e.target.result

            html = '<input type="hidden" name="data" value="' + e.target.result.replace(/^.*,/, '') + '" >';

            html += '<input type="hidden" name="mimetype" value="' + e.target.result.match(/^.*(?=;)/)[0] + '" >';

            html += '<input type="hidden" name="filename" value="' + e.target.fileName + '" >';

            $("#data").empty().append(html);

        }

        fr.readAsDataURL(file);

    });

    </script>

</body>


GOOGLE SCRIPT :

function doPost(e) {

  var data = Utilities.base64Decode(e.parameters.data);

  var blob = Utilities.newBlob(data, e.parameters.mimetype, e.parameters.filename);

  var emailAddress = "emailcontoh@email.com";

  var message = "Lampiran Gambar";

  var subject = "Tes";

  MailApp.sendEmail({

      to: emailAddress,

      subject: subject,

      htmlBody: message,

      attachments: blob

    })

  var output = HtmlService.createHtmlOutput("<b>Selesai!</b>");

  output.setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);

  return output;

  // return ContentService.createTextOutput("Done.") <--- Here, an error occurred.

}

Jangan lupa ! Google Script di Deploy dan setel ke Anyone, even Anonimous.

Semoga bermanfaat.

Previous
Next Post »