0

I'm creating an eml file on my website with an attachment. I saw in this link: eml with attachment to do it in the following way, but while downloading the eml file it opens with an attachment. When I click the attachment the attachment just opens empty even though its size is more than 0 bytes.

  const makeOutlookFile = () => {
        const text = ` 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.log

Content-Transfer-Encoding: base64

Content-Disposition: attachment; filename="demo.log"

aGVsbG8gd29ybGQ=

--boundary_text_string--`;

        let textFile = null;

        var data = new Blob([text], { type: 'message/rfc822' });

        textFile = window.URL.createObjectURL(data);


        let link = document.createElement("a");

        link.href = textFile;

        link.target = "_blank";

        link.download = "";

        document.body.appendChild(link);

        link.click();

        link.remove();

    };

This is how it open: enter image description here

I searched a lot about how to create an eml file with an attachment but all the answers give this code more or less

Hasan Haghniya
  • 2,347
  • 4
  • 19
  • 29
RivkaZ
  • 41
  • 3

1 Answers1

0

I think may be you encounter with missing new line when attach a base64 string, this is working .eml file (you can copy paste to a .eml file to see):

To: demo@demo.example.com;
Subject: EML with attachments
X-Unsent: 1
Content-Type: multipart/mixed; boundary=--boundary_string

----boundary_string
Content-Type: text/html; charset=UTF-8

<html>
<body>
<p>
EmailBody
<p>
</body>
</html>

----boundary_string
Content-Type: application/octet-stream; name="test.txt"
Content-Transfer-Encoding: base64
Content-Disposition: attachment

ZXhhbXBsZQ==

----boundary_string--