首页 > 解决方案 > Button is not executing javascript in Email

问题描述

I am sending an email in go which contains html template as it's email content. The html file has a table and a button at the bottom. I have written a script code to handle the click of the button in the email but the script tag seems not being called after the button click. Hence nothing is happening.

This is my html file:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<table bgcolor="#FFFFFF" width="100%" border="0" cellspacing="0" cellpadding="0">
    <tr>
        <td>
            Hello,
        </td>
    </tr>
    <tr>
        <td>
            You have been assigned a task. Please click on the button below.
        </td>
    </tr>
    <tr>
        <td>
            <button type="button" id="button">Click me</button>
        </td>
    </tr>
</table>

<script type="application/javascript">
    document.getElementById("button").onclick = function() {
        //perform actions on button click
    };
</script>
</body>
</html>

These are the header that I am using for sending the email:

header["MIME-Version"] = "1.0"
header["Content-Type"] = ""text/html"; charset=\"utf-8\""

The email is sent and being rendered successfully but the click of the button does nothing. Any help will be highly appreciated.

标签: javascripthtmlemail

解决方案


电子邮件不允许在 HTML 中使用 JavaScript。

电子邮件提供商通常会去除 html 模板中的 javascript 内容,以确保后台没有运行可能危及用户数据的恶意脚本。

我建议使用<a>(锚标记)指向链接,而不是使用电子邮件中的按钮。


推荐阅读