Skip to content

Instantly share code, notes, and snippets.

@tanaikech
Last active March 14, 2025 19:43

Revisions

  1. tanaikech revised this gist Nov 22, 2022. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions submit.md
    Original file line number Diff line number Diff line change
    @@ -46,6 +46,8 @@ function myFunction() {

    - When this script is run, a Gmail message is retrieved and it is converted to a PNG image. In this sample script, the PNG file is created in the root folder.

    - In this sample, the image size is 512 x 512 by `setDimensions(512, 512)`. If your HTML body is large, please modify this size for your actual situation.

    ## Reference

    - This sample script was asked to [this thread on Stackoverflow](https://stackoverflow.com/q/74506937).
  2. tanaikech created this gist Nov 22, 2022.
    51 changes: 51 additions & 0 deletions submit.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    # Converting Gmail Message to Image using Google Apps Script

    ![](https://tanaikech.github.io/image-storage/20221122a/fig1.png)

    This is a workaround for converting a Gmail message to a PNG image using Google Apps Script.

    ## Sample script

    Please set the message ID of Gmail.

    ```javascript
    function myFunction() {
    var id = "###"; // Please set your message ID of Gmail.

    var message = GmailApp.getMessageById(id);
    var date = Utilities.formatDate(
    message.getDate(),
    Session.getScriptTimeZone(),
    "yyyy-MM-dd HH:mm:ss"
    );
    var from = message.getFrom();
    var to = message.getTo();
    var subject = message.getSubject();
    var htmlBody = message.getBody();
    var imageBlob = Charts.newTableChart()
    .setDataTable(
    Charts.newDataTable()
    .addColumn(Charts.ColumnType.STRING, "")
    .addRow([`<p style="font-size: 120%">Date: ${date}</p>`])
    .addRow([`<p style="font-size: 120%">From: ${from}</p>`])
    .addRow([`<p style="font-size: 120%">To: ${to}</p>`])
    .addRow([`<p style="font-size: 120%">Subject: ${subject}</p>`])
    .addRow([htmlBody])
    .build()
    )
    .setOption("allowHtml", true)
    .setDimensions(512, 512)
    .build()
    .getBlob();

    DriveApp.createFile(imageBlob.setName("sample.png"));
    }
    ```

    - In this sample script, the HTML body is used.

    - When this script is run, a Gmail message is retrieved and it is converted to a PNG image. In this sample script, the PNG file is created in the root folder.

    ## Reference

    - This sample script was asked to [this thread on Stackoverflow](https://stackoverflow.com/q/74506937).