微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Google Apps 脚本 - 如何从工作表发送 HTML 电子邮件?

如何解决Google Apps 脚本 - 如何从工作表发送 HTML 电子邮件?

我正在尝试编写代码以从工作表发送 html 电子邮件。我在 GAS html 文件中有一个模板。 Gmail API 已启用。

我确实设法通过这种方式发送了一封电子邮件,并替换了占位符。但是,我只是发送 html 文件中的所有文本、标签和所有内容

function htmlTry() {
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sample");
  var lastRow     = spreadsheet.getLastRow();
  var name        = spreadsheet.getRange(lastRow,3).getValue();
  var problem     = spreadsheet.getRange(lastRow,6).getValue();

  if(problem == "Yes") {
    var htmlBody  = HtmlService.createHtmlOutputFromFile('htmlFile').getContent()
      .replace("#name",name)
      .replace("?name",name)
      .replace("/name",name);
      
    MailApp.sendEmail ("sample@email.com","Problem: " + name,htmlBody);
  }
}

我还提供了 html 文件的示例。这正是电子邮件所说的,只是它丢失了缩进。

<!DOCTYPE html>
<html>
<head>
    <Meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <Meta name="viewport" content="width=device-width,initial-scale=1.0" />
    <title>Problem</title>
    <style>
        body,table,thead,tbody,tr,td,img {
            padding: 0;
            background-color: #23A5F3;
        }

        .wrapper {
            padding-left: 10px;
            padding-right: 10px;
        }

        h1,h2,h3,h4,h5,h6,p {
            font-family: Trebuchet MS,Arial,sans-serif;
            color:#9CD6FA;
        }

        p,a,li {
            font-family: Trebuchet MS,sans-serif;
            color:#000000;
        }

    </style>
</head>

<body style="background-color:#FFFFFF;">
    <table width="100%">
        <tbody>
            <tr>
                <td class="wrapper" width="600" align="center">
                    <table class="section header" cellpadding="0" cellspacing="0" width="600">
                        <tr>
                            <td class="column">
                                <table>
                                    <tbody>
                                        <tr>
                                            <td align="left" style="background-color: #FFFFFF;">
                                                <p style="text-align:justify;">Lorem ipsum</p>
                                            </td>
                                        </tr>
                                    </tbody>
                                </table>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </tbody>
    </table>
</body>
</html>

感谢 Irvin Jay G. 解决了这个问题,他花时间解释了为什么我应该完全按照他们的建议去做,而不是自己思考。希望你有美好的几天。

解决方法

解决方案:

您需要使用名为 htmlBody 的高级参数,按照 MailApp 的 sendEmail(recipient,subject,body,options) 方法,在 html 视图中显示文件,而不是在电子邮件消息中显示纯 html 代码。

我能够复制您的脚本并在我的测试电子邮件帐户中获得此结果:

enter image description here

这是使用 htmlBody 高级参数调整后的脚本:

function htmlTry() {
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sample");
  var lastRow     = spreadsheet.getLastRow();
  var name        = spreadsheet.getRange(lastRow,3).getValue();
  var problem     = spreadsheet.getRange(lastRow,6).getValue();

  if(problem == "Yes") {
    var htmlBody  = HtmlService.createHtmlOutputFromFile('htmlFile').getContent()
      .replace("#name",name)
      .replace("?name",name)
      .replace("/name",name);

    MailApp.sendEmail("sample@email.com",'Problem: '+ name,htmlBody,{
    htmlBody: htmlBody
    });
  }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。