简体   繁体   English

Google Apps Script - 如何从 Sheet 发送 HTML 电子邮件?

[英]Google Apps Script - How to send HTML email from Sheet?

I'm trying to write a code to send an html email from a Sheet.我正在尝试编写代码以从工作表发送 html 电子邮件。 I have a template in a GAS html file.我在 GAS html 文件中有一个模板。 Gmail API is enabled. Gmail API 已启用。

I did manage to send an email this way, complete with replaced placeholders.我确实设法以这种方式发送了一封电子邮件,并替换了占位符。 However, I'm just sending all the text in the html file, tags and all.但是,我只是发送 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);
  }
}

I've provided a sample of the html file as well.我还提供了 html 文件的示例。 Which is exactly what the email says, except it loses the indents.这正是电子邮件所说的,只是它丢失了缩进。

<!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, Arial, 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>

Solved thanks to Irvin Jay G. who took the time to explain why I should have done exactly what they suggested, rather than thinking for myself.感谢Irvin Jay G.解决了这个问题,他花时间解释了为什么我应该完全按照他们的建议去做,而不是自己思考。 Hope you have a wonderful couple of days.希望你有美好的几天。

Solution:解决方案:

You need to use the advanced parameter called htmlBody , as per the MailApp's sendEmail(recipient, subject, body, options) method, to show the file in html view instead of showing the pure html codes on the email message.您需要使用名为htmlBody的高级参数,按照 MailApp 的sendEmail(recipient, subject, body, options)方法,在 html 视图中显示文件,而不是在电子邮件消息中显示纯 html 代码。

I was able to replicate your script and got this result on my test email account:我能够复制您的脚本并在我的测试电子邮件帐户中获得此结果:

在此处输入图片说明

Here's the tweaked script using the htmlBody advanced parameter:这是使用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
    });
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用谷歌应用程序脚本和谷歌工作表数据逐行发送 html 电子邮件 - How to send html email line by line using google apps script and google sheet data Google Apps脚本-如何从Sheet2获取电子邮件地址并发送电子邮件 - Google Apps Script - How to get email addresses from Sheet2 and send email Google Apps 脚本 - 如何从工作表 2 中获取 email 地址并发送 email - Google Apps Script - How to get email addresses from Sheet 2 and send email 如何在Google Apps脚本中发送带有变量的HTML电子邮件,并将其变量作为“发送至”地址 - How to send html email with variable as “send to” address in Google Apps Script 如何在 Google Apps 脚本中使用 MailApp 发送带有附件的 HTML email? - How to send a HTML email with attachment using MailApp in Google Apps Script? 如何从 HTML 文件输入中获取文件并将其作为附件发送到 Google Apps 脚本中的电子邮件? - How to get file(s) from an HTML file input and send it as an attachment to an email in Google Apps Script? Apps 脚本:如何将 Google Sheet 文件通过电子邮件发送到 HTML 服务输入中指定的电子邮件? - Apps Script: How to Email Google Sheet file to Email Specified in HTML Service Input? 如何使用 GMAIL API 从 Google Apps 脚本发送电子邮件? - How to send the email from Google Apps Script using GMAIL API? 使用应用程序脚本从谷歌表中删除 email 时如何从谷歌联系人中删除联系信息 - how to remove contact info from Google contact when email removed from google sheet using apps script Google Apps脚本[电子邮件发送] - Google Apps Script [Email Send]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM