Use Javascript to create an HTML email in Microsoft Outlook

后端 未结 4 1752
野性不改
野性不改 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条回答
  • 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', '<a id="downloadEmail" download="ShiftReport.eml" style="display: none">Download</a>');
        }
        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.

    0 讨论(0)
  • 2020-12-08 00:03

    Using the idea of plain text eml files, I came up with this: http://jsfiddle.net/CalvT/un3hapej/

    This is an edit of something I found - to create a .txt file then download it. As .eml files are practically .txt files, I figured this would work. And it does. I've left the textarea with the sample email in so you can easily test. When you click on create file, it then gives you a download link to download your .eml file. The only hurdle I can see is making the browser open the .eml file after it has been downloaded.

    EDIT: And thinking about it, as you have access to the client machines, you could set the browser to always open files of that type. For instance in Chrome, you can click on the arrow beside the download and select always open files of this type.

    Here's the code

    HTML:

    (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);
    })();
    <textarea id="textbox" style="width: 300px; height: 200px;">
    To: User <user@domain.demo>
    Subject: Subject
    X-Unsent: 1
    Content-Type: text/html
    
    <html>
    <body>
    Test message
    </body>
    </html>
      
    </textarea>
    
    <button id="create">Create file</button>
      
    <a download="message.eml" id="downloadlink" style="display: none">Download</a>

    0 讨论(0)
  • 2020-12-08 00:04

    MSG file format is documented, but it is certainly not fun... Why not create an EML (MIME) file?

    To those who want to delete or downvote this answer: the suggestion is to use the EML (MIME) format. According to the OP, he considered the MSG file format (#4), but was discouraged due to complexity or lack of JS libraries that process that format. If MSG file was considered, MIME is a much better choice - it is text based, so no special libraries are required to create it. Outlook will be able to open it just as easily as an MSG file. To make sure it is treated as an unsent message by Outlook, set the X-Unsent MIME header to 1.

    UPDATE: The simplest EML file would look like the following:

    To: Joe The User <joe@domain.demo>
    Subject: Test EML message
    X-Unsent: 1
    Content-Type: text/html
    
    <html>
    <body>
    Test message with <b>bold</b> text.
    </body>
    </html>
    
    0 讨论(0)
  • 2020-12-08 00:06

    Nobody seems to have answered the attachment question, so here's my solution: create the EML as a multipart/mixed message.

    Content-Type: multipart/mixed; boundary=--boundary_text_string
    

    With this, you can have multiple parts in your email. Multiple parts let you add attachments, like this.

    Content-Type: application/octet-stream; name=demo.pdf
    Content-Transfer-Encoding: base64
    Content-Disposition: attachment
    

    Start with your email headers, then add your boundary, then the part contents (newline locations are very important, clients won't parse your file correctly otherwise). You can add multiple parts. Below is an example. Note that the last boundary is different from the others (2 dashes at the end).

    To: Demo-Recipient <demo@demo.example.com>
    Subject: EML with attachments
    X-Unsent: 1
    Content-Type: multipart/mixed; boundary=--boundary_text_string
    
    ----boundary_text_string
    Content-Type: text/html; charset=UTF-8
    
    <html>
    <body>
    <p>Example</p>
    </body>
    </html>
    
    ----boundary_text_string
    Content-Type: application/octet-stream; name=demo.txt
    Content-Transfer-Encoding: base64
    Content-Disposition: attachment
    ZXhhbXBsZQ==
    
    ----boundary_text_string
    Content-Type: application/octet-stream; name=demo.log
    Content-Transfer-Encoding: base64
    Content-Disposition: attachment
    ZXhhbXBsZQ==
    
    ----boundary_text_string--
    

    This gives you a eml file with two attachments. See RFC 1371 if you want to know more specifics on how this works.

    0 讨论(0)
提交回复
热议问题