.EML - Email Generation in JS

by seanodotcom

HTML

<!-- https://jsfiddle.net/seanodotcom/yd1n8Lfh -->
<textarea id="textbox" style="width: 300px; height: 600px;">
To: User <user@domain.demo>
Subject: Subject
X-Unsent: 1
Content-Type: text/html

<html>
<head>
<style>
    body, html, table {
        font-family: Calibri, Arial, sans-serif;
    }
    .pastdue { color: crimson; }
    table {
    	border: 1px solid silver;
    	padding: 6px;
    }
    thead {
        text-align: center;
        font-size: 1.2em;
        color: navy;
        background-color: silver;
        font-weight: bold;
    }
    tbody td {
    	text-align: center;
    }
</style>
</head>
<body>
<table width=100%>
	<tr>
		<td><img src="http://www.laurell.com/images/logo/laurell_logo_storefront.jpg" width="200" height="57" alt=""></td>
		<td align="right"><h1><span class="pastdue">PAST DUE</span> INVOICE</h1></td>
	</tr>
</table>
<table width=100%>
	<thead>
		<th>Invoice #</th>
		<th>Days Overdue</th>
		<th>Amount Owed</th>
	</thead>
	<tbody>
	<tr>
		<td>OU812</td>
		<td>9</td>
		<td>$4395.00</td>
	</tr>
	<tr>
		<td>OU812</td>
		<td>9</td>
		<td>$4395.00</td>
	</tr>
	<tr>
		<td>OU812</td>
		<td>9</td>
		<td>$4395.00</td>
	</tr>
	</tbody>
</table>
</body>
</html>
</textarea> <br>
<button id="create">Create file</button><br><br>
<a download="message.eml" id="downloadlink" style="display: none">Download</a>

JavaScript

(function () {
var textFile = null,
  makeTextFile = function (text) {
    var data = new Blob([text], {type: 'text/plain'});
    if (textFile !== null) {
      window.URL.revokeObjectURL(textFile);
    }
    textFile = window.URL.createObjectURL(data);
    return textFile;
  };

  var create = document.getElementById('create'),
    textbox = document.getElementById('textbox');
  create.addEventListener('click', function () {
    var link = document.getElementById('downloadlink');
    link.href = makeTextFile(textbox.value);
    link.style.display = 'block';
  }, false);
})();