Use Javascript to create an HTML email in Microsoft Outlook

后端 未结 4 1755
野性不改
野性不改 2020-12-07 23:47

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. Open Outlook HTML with Chrome). There are prob

4条回答
  •  Happy的楠姐
    2020-12-07 23:55

    I had encoding problem when creating an .eml file with non-english characters and then opening it in Outlook. The problem was that I put the "charset=" to the wrong place and did not put the quotes (") around the encoding. The solution:

    function createShiftReportEmail() {
        const title = "Shift Összefoglaló";
        const body = "ÁÉŐÚŰÓÜÖÍűáéúőóöí";
    
        const emlContent = new Blob([`data:message/rfc822 eml,\nSubject: ${title}\nX-Unsent: 1\nContent-Type: text/plain;charset="utf-8"\n\n${body}`]);
    
        if (!document.querySelector('#downloadEmail')) {
            document.body.insertAdjacentHTML('beforeend', '');
        }
        const downloadBtn = document.querySelector('#downloadEmail');
        downloadBtn.href = URL.createObjectURL(emlContent);
        downloadBtn.click();
    }
    

    Edit: Turns out quotes (") are not even necessary. Only the placement was wrong for me.

提交回复
热议问题