title: Use Javascript to create an HTML email in Microsoft Outlook
body: <p>I'd like to create an email from a Javascript web application.  I'm completely aware of the many SO questions on this (e.g. <a href="https://stackoverflow.com/questions/25316902/open-outlook-with-html-in-body-using-chrome">Open Outlook HTML with Chrome</a>).  There are problems with the typical answers:</p>
  
  <ol>
  <li><p>Mailto: link: This will let you create an email, but only in plain text (no HTML) and it does not allow for attachments.</p></li>
  <li><p>Activex: IE only, my application needs to run in Firefox and Chrome too.  FF &amp; Chrome plug-ins to allow ActiveX are security hazards and seem to be buggy.</p></li>
  <li><p>Server-side sends via SMTP: The email does not end up in the "Sent" folder for the user.  Plus hurdles letting the user edit HTML in the browser and attach files.</p></li>
  <li><p>Create an Outlook .MSG file: There seem to be no libraries and little written about doing this.  Apparently the file format actually has an entire FAT file storage system embedded.</p></li>
  </ol>
  
  <p><strong>Key differences</strong> between many other SO questions and mine: </p>
  
  <ul>
  <li>I <strong>do</strong> have access to the client machines, so I could install
  helper applications or add-ins, change settings as needed, etc.    </li>
  <li>The interface does <em>not</em> need to actually send the mail, it only needs
  to set it up for the user.</li>
  <li>I also need to be able to give the email an attachment from JS (e.g. a PDF).</li>
  </ul>
  
  <p>I cannot be the first web app developer to face this and yet I'm unable to find a solution either commercial or open source.</p>
  
  <p><strong>Update:</strong></p>
  
  <p>I used the EML file method and it works well so far.  Here's my JS code to create and trigger it:</p>
  
  <pre><code>var emlContent = "data:message/rfc822 eml;charset=utf-8,";
  emlContent += 'To: '+emailTo+'\n';
  emlContent += 'Subject: '+emailSubject+'\n';
  emlContent += 'X-Unsent: 1'+'\n';
  emlContent += 'Content-Type: text/html'+'\n';
  emlContent += ''+'\n';
  emlContent += htmlDocument;
  
  var encodedUri = encodeURI(emlContent); //encode spaces etc like a url
  var a = document.createElement('a'); //make a link in document
  var linkText = document.createTextNode("fileLink");
  a.appendChild(linkText);
  a.href = encodedUri;
  a.id = 'fileLink';
  a.download = 'filename.eml';
  a.style = "display:none;"; //hidden link
  document.body.appendChild(a);
  document.getElementById('fileLink').click(); //click the link
  </code></pre>