3

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.

Woohaik
  • 1,084
  • 1
  • 6
  • 24
  • You can not just willy-nilly make my browser automatically start external applications. At most you can click "open" instead of "save as" in the download dialog, so that the browser will put the file into a temporary location, and then will pass it on to the OS to try and open with whatever application might be connected to that file suffix / mime type. – CBroe Nov 08 '21 at 11:38

0 Answers0