Ikrom
Ikrom

Reputation: 5123

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 <[email protected]>
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.

Upvotes: 4

Views: 9351

Answers (2)

Ikrom
Ikrom

Reputation: 5123

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": "[email protected], [email protected]",
    "subject": "Today is rainy",
    "fromName": "John Smith",
    "from": "[email protected]",
    "body": "Sample body text",
    "cids": [],
    "attaches" : []
}
var mimeMessage = createMimeMessage(mail);
console.log(mimeMessage);    

I hope it helps.

Upvotes: 10

Anentropic
Anentropic

Reputation: 33873

I think you should look to the world of Node.js modules... you won't be able to actually send your MIME mail message from the browser client since there is no SMTP support provided. But you can POST the pre-formatted message to your web server via XHR and then have the web server actually send the message.

This looks like it could do what you need:
https://www.npmjs.org/package/mailcomposer

i.e. you can prepare a MIME message as a string

this tool may help in terms of using Node.js modules in the browser:
http://browserify.org/

Upvotes: 0

Related Questions