I am working on a requirement where I need to open html formatted email after clicking on a button in a html page. After reviewing different technics I found below solution which creates an .EML
file.
funcion openEmail() {
var emailTo='test@gmail.com';
var emailSubject='Dummy Subject';
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 += '<b>Dummy Content</b>';
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 = 'test.eml';
a.style = "display:none;"; //hidden link
document.body.appendChild(a);`enter code here`
}
But I need to open it directy with configured email application. like outlook, gmail etc. I tried mailto
, location
things but formatted email is not possible there.