I am getting a blob from server, which can contain a pdf or tiff file. I have a button called email. On click, i want to get that blob, create an .eml file, attach that blob file as an attachment and then download that .eml file. Upon opening downloaded file, i want local email client ( mainly outlook) to open with attached file.
My attempt
downloadEMLFile(blob, blob.type, fileName);
function downloadEMLFile(blobFile, blobType, fileName) {
// Metadata for EML file
const sender = "[email protected]";
const recipient = "[email protected]";
const subject = "Your Subject";
const body = "Your email body";
// Create a new EML file content
const emlContent = `
From: ${sender}
To: ${recipient}
Subject: ${subject}
X-Unsent: 1
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="boundary"
--boundary
Content-Type: text/plain; charset=utf-8
${body}
--boundary
Content-Type: ${blobType}; name="${fileName}"
Content-Disposition: attachment
Content-Transfer-Encoding: base64
${blobFile}
--boundary--
`;
const emlBlob = new Blob([emlContent], { type: "message/rfc822" });
// Create a download link and trigger the download
const downloadLink = document.createElement("a");
downloadLink.href = URL.createObjectURL(emlBlob);
downloadLink.download = `${subject}.eml`;
downloadLink.click();
}
