Search code examples
javascriptmessagemime

JavaScript: create MIME message


I have user interface to send message. User enters a subject, message body, emails to send, attaches some files. After submit I need to send message as MIME message, like this:

From: John Doe <example@example.com>
MIME-Version: 1.0
Content-Type: multipart/mixed;
        boundary="XXXXboundary text"

This is a multipart message in MIME format.

--XXXXboundary text 
Content-Type: text/plain

this is the body text

--XXXXboundary text 
Content-Type: text/plain;
Content-Disposition: attachment;
        filename="test.txt"

this is the attachment text

--XXXXboundary text--

How can I gather user entered information as MIME message? I searched to build MIME message on client side with JavaScript but no success. If attachments exist, I need to convert them base64 string, then send within MIME message. Thanks.


Solution

  • I've created a javascript plugin to create MIME message in javascript. https://github.com/ikr0m/mime-js. Create mail object with necessary properties and call createMimeMessage function. It returns ready MIME message as javascript string.

    var mail = {  
        "to": "email1@example.com, email2@example.com",
        "subject": "Today is rainy",
        "fromName": "John Smith",
        "from": "john.smith@mail.com",
        "body": "Sample body text",
        "cids": [],
        "attaches" : []
    }
    var mimeMessage = createMimeMessage(mail);
    console.log(mimeMessage);    
    

    I hope it helps.