commit 134b06330e353152148ab314df6f799ae46d2225
parent 2c19a10b4299605b333b14b2d280cc1b828db8e3
Author: Ikrom <ikromur@gmail.com>
Date: Sun, 10 May 2015 11:39:19 +0500
Version 0.2.0 Used Gulpjs to compile.
Diffstat:
6 files changed, 198 insertions(+), 139 deletions(-)
diff --git a/Readme.md b/Readme.md
@@ -1,12 +1,20 @@
mime-js
=======
-Sometimes you need to create MIME message in browser. With *mime-js* you can create expected MIME message text. This project is created with Play framework, sbt.
+Sometimes you need to create MIME message in browser. With *mime-js* you can create expected MIME message text.
+
+Install
+-------
+
+Run from terminal:
+
+`npm install`
+`gulp`
Usage
-----
-Call **createMimeMessage** function with *mail* object:
+Call **Mime.toMimeTxt** function with *mail* object:
```javascript
var mail = {
@@ -58,8 +66,3 @@ PGRpdj5TYW1wbGUgYm9keSB0ZXh0PC9kaXY+
**cids** - For inline images
**attaches** - any file in base64 format
-
-You may need to look into [mime-js.coffee][1] ([mime-js.js][2])
-
- [1]: https://github.com/ikr0m/mime-js/blob/master/app/assets/javascripts/mime-js.coffee
- [2]: https://github.com/ikr0m/mime-js/blob/master/public/javascripts/mime-js.js
-\ No newline at end of file
diff --git a/demo/index..html b/demo/index..html
@@ -1,28 +0,0 @@
-<!DOCTYPE html>
-
-<html>
-<head>
- <title>MIME JS</title>
- <script src="../dist/base64.js" type="text/javascript"></script>
- <script src="../dist/mime-js.js" type="text/javascript"></script>
-</head>
-<body>
- <b>Mail obj:</b><div id="mail"></div>
- <hr>
- <div id="mime"></div>
-
- <script type="application/javascript">
- var mail = {
- "to": "email1@example.com",
- "subject": "Today is rainy",
- "fromName": "John Smith",
- "from": "john.smith@mail.com",
- "body": "Sample body text",
- "cids": [],
- "attaches" : []
- };
- document.getElementById("mail" ).innerText = JSON.stringify(mail);
- document.getElementById("mime" ).innerText = createMimeMessage(mail);
- </script>
-</body>
-</html>
diff --git a/demo/index.html b/demo/index.html
@@ -0,0 +1,28 @@
+<!DOCTYPE html>
+
+<html>
+<head>
+ <title>MIME JS</title>
+ <script src="../src/base64.js" type="text/javascript"></script>
+ <script src="../dist/mime-js.js" type="text/javascript"></script>
+</head>
+<body>
+ <b>Mail obj:</b><pre id="mail"></pre>
+ <hr>
+ <div id="mime"></div>
+
+ <script type="application/javascript">
+ var mail = {
+ "to": "email1@example.com",
+ "subject": "Today is rainy",
+ "fromName": "John Smith",
+ "from": "john.smith@mail.com",
+ "body": "Sample body text",
+ "cids": [],
+ "attaches" : []
+ };
+ document.getElementById("mail" ).innerText = JSON.stringify(mail);
+ document.getElementById("mime" ).innerText = Mime.toMimeTxt(mail);
+ </script>
+</body>
+</html>
diff --git a/gulpfile.js b/gulpfile.js
@@ -0,0 +1,20 @@
+var gulp = require('gulp'),
+ concat = require('gulp-concat'),
+ coffee = require('gulp-coffee'),
+ uglify = require('gulp-uglify');
+
+gulp.task('coffee-uglify', function () {
+ var stream = gulp.src([
+ "src/mime-js.coffee"
+ ]);
+ stream
+ .pipe(coffee())
+ .pipe(gulp.dest('./dist/'))
+ .pipe(uglify())
+ .pipe(concat({path: 'mime-js.min.js'}))
+ .pipe(gulp.dest('./dist/'))
+});
+
+gulp.task('default', [
+ 'coffee-uglify'
+]);
+\ No newline at end of file
diff --git a/package.json b/package.json
@@ -0,0 +1,12 @@
+{
+ "author": "Ikrom",
+ "description": "Create MIME message from browser.",
+ "name": "MimeJS",
+ "version": "0.2.0",
+ "devDependencies": {
+ "gulp": "^3.8.11",
+ "gulp-coffee": "^2.3.1",
+ "gulp-concat": "^2.5.2",
+ "gulp-uglify": "^1.1.0"
+ }
+}
diff --git a/src/mime-js.coffee b/src/mime-js.coffee
@@ -6,108 +6,132 @@
License: X11/MIT
###
-
-window.createMimeMessage = (mail) ->
- getBoundary = ->
- Math.random().toString(36).slice(2) + Math.random().toString(36).slice(2)
-
- createPlain = (textContent = '') ->
- '\nContent-Type: text/plain; charset=UTF-8' +
+window.Mime = do ->
+ linkify = (inputText) ->
+ #URLs starting with http://, https://, or ftp://
+ replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim
+ replacedText = inputText.replace(replacePattern1,
+ "<a href=\"$1\" target=\"_blank\">$1</a>")
+
+ #URLs starting with "www." (without // before it, or it'd re-link the ones done above).
+ replacePattern2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim
+ replacedText = replacedText.replace(replacePattern2,
+ "$1<a href=\"http://$2\" target=\"_blank\">$2</a>")
+
+ replacePattern3 = /(([a-zA-Z0-9\-\_\.])+@[a-zA-Z\_]+?(\.[a-zA-Z]{2,6})+)/gim
+ replacedText = replacedText.replace(replacePattern3,
+ '<a href="mailto:$1">$1</a>')
+
+ replacedText
+
+ getBoundary = ->
+ _random = ->
+ Math.random().toString(36).slice(2)
+ _random() + _random()
+
+ createPlain = (textContent = '') ->
+ '\nContent-Type: text/plain; charset=UTF-8' +
+ '\nContent-Transfer-Encoding: base64' +
+ '\n\n' + (Base64.encode textContent, true).replace(/.{76}/g, "$&\n")
+
+ createHtml = (msg) ->
+ htmlContent = msg.body || ""
+ htmlContent = htmlContent.replace(/&/g, '&').replace(/</g, '<')
+ .replace(/>/, '>').replace(/\n/g, '\n<br/>')
+
+ htmlContent = linkify(htmlContent)
+
+ htmlContent = '<div>' + htmlContent + '</div>'
+ '\nContent-Type: text/html; charset=UTF-8' +
+ '\nContent-Transfer-Encoding: base64' +
+ '\n\n' + (Base64.encode htmlContent, true).replace(/.{76}/g, "$&\n")
+
+ createAlternative = (text, html) ->
+ boundary = getBoundary()
+
+ '\nContent-Type: multipart/alternative; boundary=' + boundary +
+ '\n\n--' + boundary + text +
+ '\n\n--' + boundary + html +
+ '\n\n--' + boundary + '--'
+
+ createCids = (cids) ->
+ return if !cids
+ cidArr = []
+ for cid in cids
+ type = cid.type
+ name = cid.name
+ base64 = cid.base64
+ id = getBoundary()
+
+ cidArr.push '\nContent-Type: ' + type + '; name=\"' + name + '\"' +
'\nContent-Transfer-Encoding: base64' +
- '\n\n' + (Base64.encode textContent, true).replace(/.{76}/g, "$&\n")
-
- createHtml = (msg) ->
- htmlContent = msg.body || ""
- htmlContent = htmlContent.replace(/&/g, '&').replace(/</g,
- '<').replace(/>/, '>').replace(/\n/g, '\n<br/>')
-
- htmlContent = '<div>' + htmlContent + '</div>'
- '\nContent-Type: text/html; charset=UTF-8' +
+ '\nContent-ID: <' + id + '>' +
+ '\nX-Attachment-Id: ' + id +
+ '\n\n' + base64
+ cidArr
+
+ createRelated = (alternative, cids = []) ->
+ boundary = getBoundary()
+
+ relatedStr = '\nContent-Type: multipart/related; boundary=' + boundary +
+ '\n\n--' + boundary + alternative
+ for cid in cids
+ relatedStr += ('\n--' + boundary + cid)
+
+ relatedStr + '\n--' + boundary + '--'
+
+ createAttaches = (attaches) ->
+ return if !attaches
+ result = []
+ for attach in attaches
+ type = attach.type
+ name = attach.name
+ base64 = attach.base64
+ id = getBoundary()
+
+ result.push '\nContent-Type: ' + type + '; name=\"' + name + '\"' +
+ '\nContent-Disposition: attachment; filename=\"' + name + '\"' +
'\nContent-Transfer-Encoding: base64' +
- '\n\n' + (Base64.encode htmlContent, true).replace(/.{76}/g, "$&\n")
-
- createAlternative = (text, html) ->
- boundary = getBoundary()
-
- '\nContent-Type: multipart/alternative; boundary=' + boundary +
- '\n\n--' + boundary + text +
- '\n\n--' + boundary + html +
- '\n\n--' + boundary + '--'
-
- createCids = (cids) ->
- return if !cids
- cidArr = []
- for cid in cids
- type = cid.type
- name = cid.name
- base64 = cid.base64
- id = getBoundary()
-
- cidArr.push '\nContent-Type: ' + type + '; name=\"' + name + '\"' +
- '\nContent-Transfer-Encoding: base64' +
- '\nContent-ID: <' + id + '>' +
- '\nX-Attachment-Id: ' + id +
- '\n\n' + base64
- cidArr
-
- createRelated = (alternative, cids = []) ->
- boundary = getBoundary()
-
- relatedStr = '\nContent-Type: multipart/related; boundary=' + boundary +
- '\n\n--' + boundary + alternative
- for cid in cids
- relatedStr += ('\n--' + boundary + cid)
-
- relatedStr + '\n--' + boundary + '--'
-
- createAttaches = (attaches) ->
- return if !attaches
- result = []
- for attach in attaches
- type = attach.type
- name = attach.name
- base64 = attach.base64
- id = getBoundary()
-
- result.push '\nContent-Type: ' + type + '; name=\"' + name + '\"' +
- '\nContent-Disposition: attachment; filename=\"' + name + '\"' +
- '\nContent-Transfer-Encoding: base64' +
- '\nX-Attachment-Id: ' + id +
- '\n\n' + base64
- result
-
- createMixed = (related, attaches = []) ->
- boundary = getBoundary()
- if mail.subject
- subject = '=?UTF-8?B?' + Base64.encode(mail.subject, true) + '?='
- subject ?= ''
- mailFromName = '=?UTF-8?B?' + Base64.encode(mail.fromName || "",
- true) + '?='
- date = (new Date().toGMTString()).replace(/GMT|UTC/gi, '+0000')
- mimeStr = 'MIME-Version: 1.0' +
- '\nDate: ' + date +
- '\nDelivered-To: ' + mail.to +
- '\nMessage-ID: <' + getBoundary() + '@mail.your-domain.com>' +
- '\nSubject: ' + subject +
- '\nFrom: ' + mailFromName + ' <' + mail.from + '>' +
- '\nTo: ' + mail.to +
- '\nContent-Type: multipart/mixed; boundary=' + boundary +
- '\n\n--' + boundary + related
-
- for attach in attaches
- mimeStr += ('\n--' + boundary + attach)
-
- (mimeStr + '\n--' + boundary + '--').replace /\n/g, '\r\n'
-
- try
- plain = createPlain mail.body
- htm = createHtml mail
- alternative = createAlternative plain, htm
- cids = createCids mail.cids
- related = createRelated alternative, cids
- attaches = createAttaches mail.attaches
-
- createMixed related, attaches
-
- catch err
- throw new Error err
+ '\nX-Attachment-Id: ' + id +
+ '\n\n' + base64
+ result
+
+ createMixed = (related, attaches, mail) ->
+ boundary = getBoundary()
+ subject = ''
+ if mail.subject
+ subject = '=?UTF-8?B?' + Base64.encode(mail.subject, true) + '?='
+
+ mailFromName = '=?UTF-8?B?' + Base64.encode(mail.fromName || "",
+ true) + '?='
+ date = (new Date().toGMTString()).replace(/GMT|UTC/gi, '+0000')
+ mimeStr = 'MIME-Version: 1.0' +
+ '\nDate: ' + date +
+ '\nMessage-ID: <' + getBoundary() + '@mail.your-domain.com>' +
+ '\nSubject: ' + subject +
+ '\nFrom: ' + mailFromName + ' <' + mail.from + '>' +
+ '\nTo: ' + mail.to +
+ '\nContent-Type: multipart/mixed; boundary=' + boundary +
+ '\n\n--' + boundary + related
+
+ for attach in attaches
+ mimeStr += ('\n--' + boundary + attach)
+
+ (mimeStr + '\n--' + boundary + '--').replace /\n/g, '\r\n'
+
+ createMimeStr = (mail) ->
+ plain = createPlain mail.body
+ htm = createHtml mail
+ alternative = createAlternative plain, htm
+ cids = createCids mail.cids
+ related = createRelated alternative, cids
+ attaches = createAttaches mail.attaches
+
+ result = createMixed related, attaches, mail
+
+ result
+
+ {
+ toMimeTxt: createMimeStr
+ }
+