0

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.

Daksh M.
  • 4,589
  • 4
  • 30
  • 46
Neha
  • 95
  • 1
  • 12
  • 1
    It is not possible to inject javascript into emails. You can add a link with parameters based on which you can do something on your backend. – extempl Oct 25 '18 at 17:47
  • Seconded. You cannot use JavaScript in HTML emails, among many other restrictions. This related question might be helpful: https://stackoverflow.com/questions/127498/what-guidelines-for-html-email-design-are-there – Adrian Oct 25 '18 at 17:56
  • 2
    Possible duplicate of [Add javascript in the email](https://stackoverflow.com/questions/5193860/add-javascript-in-the-email) – Daksh M. Oct 25 '18 at 18:19

1 Answers1

-1

Emails do not allow the use of JavaScript in HTML.

Email providers usually strip away the javascript content in the html template to make sure there is no malicious script running in the background that can compromise the user's data.

I'll recommend using <a> (anchor tags) to point to links, instead of using buttons in an email.

Daksh M.
  • 4,589
  • 4
  • 30
  • 46