mime-js

Create MIME message from browser, fork of https://github.com/ikr0m/mime-js
git clone git://git.defalsify.org/mime-js.git
Log | Files | Refs | LICENSE

commit 045174f71ef8380c9e1402ebb16f5ad43e34a071
parent 41431712850be7f10c7bea3dec6b75f76bade976
Author: Ikrom <ikr0m@users.noreply.github.com>
Date:   Mon,  5 Oct 2015 00:23:03 +0500

Merge pull request #2 from guimard/master

Add "Cc" feature
Diffstat:
MReadme.md | 4+++-
Mdist/mime-js.js | 21++++++++++++++-------
Mdist/mime-js.min.js | 4++--
Msrc/mime-js.coffee | 19++++++++++++++-----
4 files changed, 33 insertions(+), 15 deletions(-)

diff --git a/Readme.md b/Readme.md @@ -21,6 +21,7 @@ Mime object has two public methods: Mime.toMimeTxt() and Mime.toMimeObj(). See s ```javascript var originalMail = { "to": "email1@example.com", + "cc": "email2@example.com", "subject": "Today is rainy", "fromName": "John Smith", "from": "john.smith@mail.com", @@ -47,6 +48,7 @@ Message-ID: <i2ozrb4lgrgrpb9hp8wrf4n449xjemi@mail.your-domain.com> Subject: =?UTF-8?B?VG9kYXkgaXMgcmFpbnk=?= From: =?UTF-8?B?Sm9obiBTbWl0aA==?= <john.smith@mail.com> To: email1@example.com +Cc: email2@example.com Content-Type: multipart/mixed; boundary=qr7c8bjwkc81if6r9xpqmra8rrudi --qr7c8bjwkc81if6r9xpqmra8rrudi @@ -77,7 +79,7 @@ MimeObj ------- ```javascript -{"html":"<div>Sample body text</div>","text":"Sample body text","attaches":[],"innerMsgs":[],"to":"email1@example.com","from":"John Smith <john.smith@mail.com>","subject":"Today is rainy"} +{"html":"<div>Sample body text</div>","text":"Sample body text","attaches":[],"innerMsgs":[],"to":"email1@example.com","cc":"email2@example.com","from":"John Smith <john.smith@mail.com>","subject":"Today is rainy"} ``` ------------------------------------------------------------------ diff --git a/dist/mime-js.js b/dist/mime-js.js @@ -10,7 +10,7 @@ (function() { window.Mime = (function() { var MailParser, _util, buildMimeObj, toMimeObj, toMimeTxt; - toMimeTxt = function(mail) { + toMimeTxt = function(mail, txtOnly) { var alternative, attaches, cids, createAlternative, createAttaches, createCids, createHtml, createMixed, createPlain, createRelated, getBoundary, htm, linkify, plain, related, result; linkify = function(inputText) { var replacePattern1, replacePattern2, replacePattern3, replacedText; @@ -102,7 +102,7 @@ } 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; + mimeStr = 'MIME-Version: 1.0' + '\nDate: ' + date + '\nMessage-ID: <' + getBoundary() + '@mail.your-domain.com>' + '\nSubject: ' + subject + '\nFrom: ' + mailFromName + ' <' + mail.from + '>' + '\nTo: ' + mail.to + '\nCc: ' + mail.cc + '\nContent-Type: multipart/mixed; boundary=' + boundary + '\n\n--' + boundary + related; for (j = 0, len = attaches.length; j < len; j++) { attach = attaches[j]; mimeStr += '\n--' + boundary + attach; @@ -110,16 +110,20 @@ return (mimeStr + '\n--' + boundary + '--').replace(/\n/g, '\r\n'); }; plain = createPlain(mail.body); - htm = createHtml(mail); - alternative = createAlternative(plain, htm); - cids = createCids(mail.cids); - related = createRelated(alternative, cids); + if (txtOnly) { + related = plain; + } else { + htm = createHtml(mail); + alternative = createAlternative(plain, htm); + cids = createCids(mail.cids); + related = createRelated(alternative, cids); + } attaches = createAttaches(mail.attaches); result = createMixed(related, attaches); return result; }; MailParser = function(rawMessage) { - var explodeMessage, from, getValidStr, messageParts, rawHeaders, subject, to; + var cc, explodeMessage, from, getValidStr, messageParts, rawHeaders, subject, to; explodeMessage = function(inMessage) { var escBoundary, i, inBody, inBodyParts, inBoundary, inContentType, inContentTypeParts, inHeaderPos, inRawBody, inRawHeaders, match, mimeType, mimeTypeParts, regContentType, regString, specialChars; inHeaderPos = inMessage.indexOf("\r\n\r\n"); @@ -199,11 +203,13 @@ }; subject = getValidStr(/\r\nSubject: (.*)\r\n/g.exec(rawHeaders)); to = getValidStr(/\r\nTo: (.*)\r\n/g.exec(rawHeaders)); + cc = getValidStr(/\r\nCc: (.*)\r\n/g.exec(rawHeaders)); from = getValidStr(/\r\nFrom: (.*)\r\n/g.exec(rawHeaders)); return { messageParts: messageParts, subject: subject, to: to, + cc: cc, from: from }; }; @@ -334,6 +340,7 @@ attaches: [], innerMsgs: [], to: _util.decodeMimeWords(rawMailObj.to), + cc: _util.decodeMimeWords(rawMailObj.cc), from: _util.decodeMimeWords(rawMailObj.from), subject: _util.decodeMimeWords(rawMailObj.subject) }; diff --git a/dist/mime-js.min.js b/dist/mime-js.min.js @@ -1 +1 @@ -(function(){window.Mime=function(){var e,t,n,r,u;return u=function(e){var t,n,r,u,a,o,i,c,s,l,p,d,f,m,g,h;return f=function(e){var t,n,r,u;return t=/(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim,u=e.replace(t,'<a href="$1" target="_blank">$1</a>'),n=/(^|[^\/])(www\.[\S]+(\b|$))/gim,u=u.replace(n,'$1<a href="http://$2" target="_blank">$2</a>'),r=/(([a-zA-Z0-9\-\_\.])+@[a-zA-Z\_]+?(\.[a-zA-Z]{2,6})+)/gim,u=u.replace(r,'<a href="mailto:$1">$1</a>')},p=function(){var e;return e=function(){return Math.random().toString(36).slice(2)},e()+e()},s=function(e){return null==e&&(e=""),"\nContent-Type: text/plain; charset=UTF-8\nContent-Transfer-Encoding: base64\n\n"+Base64.encode(e,!0).replace(/.{76}/g,"$&\n")},i=function(e){var t;return t=e.body||"",t=t.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/,"&gt;").replace(/\n/g,"\n<br/>"),t=f(t),t="<div>"+t+"</div>","\nContent-Type: text/html; charset=UTF-8\nContent-Transfer-Encoding: base64\n\n"+Base64.encode(t,!0).replace(/.{76}/g,"$&\n")},u=function(e,t){var n;return n=p(),"\nContent-Type: multipart/alternative; boundary="+n+"\n\n--"+n+e+"\n\n--"+n+t+"\n\n--"+n+"--"},o=function(e){var t,n,r,u,a,o,i,c;if(e){for(r=[],a=0,o=e.length;o>a;a++)n=e[a],c=n.type,i=n.name,t=n.base64,u=p(),r.push("\nContent-Type: "+c+'; name="'+i+'"\nContent-Transfer-Encoding: base64\nContent-ID: <'+u+">\nX-Attachment-Id: "+u+"\n\n"+t);return r}},l=function(e,t){var n,r,u,a,o;for(null==t&&(t=[]),n=p(),o="\nContent-Type: multipart/related; boundary="+n+"\n\n--"+n+e,u=0,a=t.length;a>u;u++)r=t[u],o+="\n--"+n+r;return o+"\n--"+n+"--"},a=function(e){var t,n,r,u,a,o,i,c;if(e){for(i=[],u=0,a=e.length;a>u;u++)t=e[u],c=t.type,o=t.name,n=t.base64,r=p(),i.push("\nContent-Type: "+c+'; name="'+o+'"\nContent-Disposition: attachment; filename="'+o+'"\nContent-Transfer-Encoding: base64\nX-Attachment-Id: '+r+"\n\n"+n);return i}},c=function(t,n){var r,u,a,o,i,c,s,l;for(u=p(),l="",e.subject&&(l="=?UTF-8?B?"+Base64.encode(e.subject,!0)+"?="),c="=?UTF-8?B?"+Base64.encode(e.fromName||"",!0)+"?=",a=(new Date).toGMTString().replace(/GMT|UTC/gi,"+0000"),s="MIME-Version: 1.0\nDate: "+a+"\nMessage-ID: <"+p()+"@mail.your-domain.com>\nSubject: "+l+"\nFrom: "+c+" <"+e.from+">\nTo: "+e.to+"\nContent-Type: multipart/mixed; boundary="+u+"\n\n--"+u+t,o=0,i=n.length;i>o;o++)r=n[o],s+="\n--"+u+r;return(s+"\n--"+u+"--").replace(/\n/g,"\r\n")},m=s(e.body),d=i(e),t=u(m,d),r=o(e.cids),g=l(t,r),n=a(e.attaches),h=c(g,n)},e=function(e){var n,r,u,a,o,i,c;n=function(e){var r,u,a,o,i,c,s,l,p,d,f,m,g,h,b,y;if(l=e.indexOf("\r\n\r\n"),-1===l&&(e=e.replace(/\n/g,"\r\n"),l=e.indexOf("\r\n\r\n"),-1===l&&(l=e.length)),d=e.slice(0,l).replace(/\r\n\s+/g," ")+"\r\n",p=e.slice(l).replace(/(\r\n)+$/,"").replace(/^(\r\n)+/,""),c="",h=d.match(/Content-Type: (.*)/i),h&&h.length>0?c=h[1]:console.log("Warning: MailParser: Content-type doesn't exist!"),s=c.split(";"),m=s[0].replace(/\s/g,""),g=m.split("/"),"multipart"===g[0].toLowerCase())for(o=[],f=s[1].match(/boundary="?([^"]*)"?/i),!f&&s[2]&&(f=s[2].match(/boundary="?([^"]*)"?/i)),i=t.trim(f[1]).replace(/"/g,""),r=i.replace(/\+/g,"\\+"),b=new RegExp("--"+r,"g"),o=p.replace(b,i).replace(b,i).split(i),o.shift(),o.pop(),u=0;u<o.length;)o[u]=o[u].replace(/(\r\n)+$/,"").replace(/^(\r\n)+/,""),o[u]=n(o[u]),u++;else if(a=p,"text"===g[0]&&(a=a.replace(RegExp("=\\r\\n","g"),""),y=a.match(RegExp("=[A-F0-9][A-F0-9]","g"))))for(u=0;u<y.length;)a=a.replace(y[u],String.fromCharCode(parseInt(y[u].replace(RegExp("="),""),16))),u++;return{rawHeaders:d,rawBody:p,body:a,contentType:c,contentTypeParts:s,boundary:i,bodyParts:o,mimeType:m,mimeTypeParts:g}},a="";try{a=n(e)}catch(s){}return o=a.rawHeaders,u=function(e){return null==e&&(e=[]),e[1]||""},i=u(/\r\nSubject: (.*)\r\n/g.exec(o)),c=u(/\r\nTo: (.*)\r\n/g.exec(o)),r=u(/\r\nFrom: (.*)\r\n/g.exec(o)),{messageParts:a,subject:i,to:c,from:r}},t=function(){var e,n,r,u,a,o,i,c;return i=function(e){return null==e&&(e=""),("function"==typeof e.trim?e.trim():void 0)||e.replace(/^\s+|\s+$/g,"")},u=function(t,n){var r;return null==t&&(t=""),null==n&&(n=""),n=n.toLowerCase(),r=function(){switch(!1){case-1===n.indexOf("koi8-r"):return e(t);case-1===n.indexOf("utf-8"):return Base64._utf8_decode(t);case-1===n.indexOf("windows-1251"):return c(t);default:return t}}()},n=function(e){return e.replace(/\=[\r\n]+/g,"").replace(/\=[0-9A-F]{2}/gi,function(e){return String.fromCharCode(parseInt(e.substr(1),16))})},e=function(e){var t,n,r,u,a,o,i;for(t=unescape("%u2500%u2502%u250C%u2510%u2514%u2518%u251C%u2524%u252C%u2534%u253C%u2580%u2584%u2588%u258C%u2590%u2591%u2592%u2593%u2320%u25A0%u2219%u221A%u2248%u2264%u2265%u00A0%u2321%u00B0%u00B2%u00B7%u00F7%u2550%u2551%u2552%u0451%u2553%u2554%u2555%u2556%u2557%u2558%u2559%u255A%u255B%u255C%u255D%u255E%u255F%u2560%u2561%u0401%u2562%u2563%u2564%u2565%u2566%u2567%u2568%u2569%u256A%u256B%u256C%u00A9%u044E%u0430%u0431%u0446%u0434%u0435%u0444%u0433%u0445%u0438%u0439%u043A%u043B%u043C%u043D%u043E%u043F%u044F%u0440%u0441%u0442%u0443%u0436%u0432%u044C%u044B%u0437%u0448%u044D%u0449%u0447%u044A%u042E%u0410%u0411%u0426%u0414%u0415%u0424%u0413%u0425%u0418%u0419%u041A%u041B%u041C%u041D%u041E%u041F%u042F%u0420%u0421%u0422%u0423%u0416%u0412%u042C%u042B%u0417%u0428%u042D%u0429%u0427%u042A"),n=function(e){return e>=128&&255>=e?t.charAt(e-128):String.fromCharCode(e)},o="",r=u=0,a=e.length;a>u;r=++u)i=e[r],o+=n(e.charCodeAt(r));return o},c=function(e){var t,n,r,u,a,o,i;for(null==e&&(e=""),o="",t=r=0,u=e.length;u>r;t=++r)i=e[t],n=e.charCodeAt(t),a=function(){switch(!1){case 168!==n:return 1025;case 184!==n:return 1105;case!(n>191&&256>n):return n+848;default:return n}}(),o+=String.fromCharCode(a);return o},r=function(e,r){var a,o,i;return e=t.trim(e),o=void 0,a=void 0,i=void 0,(i=e.match(/^\=\?([\w_\-]+)\?([QqBb])\?([^\?]*)\?\=$/i))?(o=i[1],a=(i[2]||"Q").toString().toUpperCase(),e=(i[3]||"").replace(/_/g," "),"B"===a?Base64.decode(e,r):"Q"===a?n(e):e):u(e,r)},a=function(e,t){return e=(e||"").toString().replace(/(=\?[^?]+\?[QqBb]\?[^?]+\?=)\s+(?==\?[^?]+\?[QqBb]\?[^?]*\?=)/g,"$1").replace(/\=\?([\w_\-]+)\?([QqBb])\?[^\?]*\?\=/g,function(e,t,n){return r(e)}.bind(this)),u(e,t)},o=function(e){return null==e&&(e=""),(e+"").replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;")},{decode:u,KOIRDec:e,win1251Dec:c,decodeMimeWords:a,toHtmlEntity:o,trim:i}}(),n=function(n){var u,a,o,i,c,s,l,p,d,f,m,g;f={html:"",text:"",attaches:[],innerMsgs:[],to:t.decodeMimeWords(n.to),from:t.decodeMimeWords(n.from),subject:t.decodeMimeWords(n.subject)},a=function(e,n){var r,u,a;return a=/Content-Transfer-Encoding: quoted-printable/i.test(n),u=/Content-Transfer-Encoding: base64/i.test(n),u?(e=e.replace(/\s/g,""),r="function"==typeof atob?atob(e):void 0,null==r&&(r=Base64.decode(e)),e=r):a&&(e=t.QPDec(e)),e},p=function(n){var u,o,i,c,s,l,d,m,g,h,b,y,C,x,T,v,w,B,A,E,M,D,F,$,O;if(n){for(h=0,y=n.length;y>h;h++)if(w=n[h],x=(null!=(A=w.mimeType)?A:"").toLowerCase(),-1===x.indexOf("multipart"))if(-1===x.indexOf("message/rfc822"))if(B=w.rawHeaders,c=-1!==B.indexOf("Content-Disposition: attachment"),o=w.rawBody,l=/text\/html/.test(x),m=/text\/plain/.test(x),d=/image/.test(x),s=/audio/.test(x),c||d||s){for(g=/Content-Transfer-Encoding: quoted-printable/i.test(B),g&&(o=t.QPDec(o),o=btoa?btoa(o):Base64.encode(o)),E=w.contentTypeParts,b=0,C=E.length;C>b;b++)if(O=E[b],/name=/i.test(O)){T=O.replace(/(.*)=/,"").replace(/"|'/g,"");break}T||(T=d?"image":s?"audio":"attachment",T+="_"+Math.floor(100*Math.random()),F=x.indexOf("/"),$=x.substring(F+1),$.length<4&&(T+="."+$)),D=/(.*)content-id:(.*)<(.*)>/i,u={type:x,base64:o,name:T,cid:null!=(M=D.exec(B))?M[3]:void 0,visible:/png|jpeg|jpg|gif/.test(x)},f.attaches.push(u)}else l||m?(o=a(o,B),o=t.decode(o,w.contentType),l&&(f.html+=o),m&&(f.text+=o)):console.log("Unknown mime type: "+x);else v=e(w.rawBody),i=r(v),f.innerMsgs.push(i);else p(w.bodyParts);return null}};try{if(d=n.messageParts,!d)return f;l=(d.mimeType||"").toLowerCase(),c=/text\/plain/.test(l),i=/text\/html/.test(l),-1!==l.indexOf("multipart")?p(d.bodyParts):c||i?(u=a(d.body,d.rawHeaders),u=t.decode(u,d.contentType),i&&(f.html=u),c&&(f.text=u)):console.log("Warning: mime type isn't supported! mime="+l)}catch(h){throw o=h,new Error(o)}return g=function(e){return"<pre>"+t.toHtmlEntity(e)+"</pre>"},s=function(e){var n,r,u,a,o,i,c,l;if(u=e.innerMsgs,null!=u?u.length:void 0)for(!t.trim(e.html)&&e.text&&(e.html+=g(e.text)),a=0,o=u.length;o>a;a++)r=u[a],i=s(r),l=i.text,n=i.html,n?e.html+=n:l&&(e.html+=wrapPerTag(l),e.text+=l),(null!=(c=i.attaches)?c.length:void 0)>0&&(e.attaches=e.attaches.concat(i.attaches));return e},m=s(f)},r=function(t){var r,u;return u=e(t),r=n(u)},{toMimeTxt:u,toMimeObj:r}}()}).call(this); -\ No newline at end of file +(function(){window.Mime=function(){var e,n,t,r,u;return u=function(e,n){var t,r,u,a,o,i,c,s,l,d,p,f,m,g,h,b;return m=function(e){var n,t,r,u;return n=/(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim,u=e.replace(n,'<a href="$1" target="_blank">$1</a>'),t=/(^|[^\/])(www\.[\S]+(\b|$))/gim,u=u.replace(t,'$1<a href="http://$2" target="_blank">$2</a>'),r=/(([a-zA-Z0-9\-\_\.])+@[a-zA-Z\_]+?(\.[a-zA-Z]{2,6})+)/gim,u=u.replace(r,'<a href="mailto:$1">$1</a>')},p=function(){var e;return e=function(){return Math.random().toString(36).slice(2)},e()+e()},l=function(e){return null==e&&(e=""),"\nContent-Type: text/plain; charset=UTF-8\nContent-Transfer-Encoding: base64\n\n"+Base64.encode(e,!0).replace(/.{76}/g,"$&\n")},c=function(e){var n;return n=e.body||"",n=n.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/,"&gt;").replace(/\n/g,"\n<br/>"),n=m(n),n="<div>"+n+"</div>","\nContent-Type: text/html; charset=UTF-8\nContent-Transfer-Encoding: base64\n\n"+Base64.encode(n,!0).replace(/.{76}/g,"$&\n")},a=function(e,n){var t;return t=p(),"\nContent-Type: multipart/alternative; boundary="+t+"\n\n--"+t+e+"\n\n--"+t+n+"\n\n--"+t+"--"},i=function(e){var n,t,r,u,a,o,i,c;if(e){for(r=[],a=0,o=e.length;o>a;a++)t=e[a],c=t.type,i=t.name,n=t.base64,u=p(),r.push("\nContent-Type: "+c+'; name="'+i+'"\nContent-Transfer-Encoding: base64\nContent-ID: <'+u+">\nX-Attachment-Id: "+u+"\n\n"+n);return r}},d=function(e,n){var t,r,u,a,o;for(null==n&&(n=[]),t=p(),o="\nContent-Type: multipart/related; boundary="+t+"\n\n--"+t+e,u=0,a=n.length;a>u;u++)r=n[u],o+="\n--"+t+r;return o+"\n--"+t+"--"},o=function(e){var n,t,r,u,a,o,i,c;if(e){for(i=[],u=0,a=e.length;a>u;u++)n=e[u],c=n.type,o=n.name,t=n.base64,r=p(),i.push("\nContent-Type: "+c+'; name="'+o+'"\nContent-Disposition: attachment; filename="'+o+'"\nContent-Transfer-Encoding: base64\nX-Attachment-Id: '+r+"\n\n"+t);return i}},s=function(n,t){var r,u,a,o,i,c,s,l;for(u=p(),l="",e.subject&&(l="=?UTF-8?B?"+Base64.encode(e.subject,!0)+"?="),c="=?UTF-8?B?"+Base64.encode(e.fromName||"",!0)+"?=",a=(new Date).toGMTString().replace(/GMT|UTC/gi,"+0000"),s="MIME-Version: 1.0\nDate: "+a+"\nMessage-ID: <"+p()+"@mail.your-domain.com>\nSubject: "+l+"\nFrom: "+c+" <"+e.from+">\nTo: "+e.to+"\nCc: "+e.cc+"\nContent-Type: multipart/mixed; boundary="+u+"\n\n--"+u+n,o=0,i=t.length;i>o;o++)r=t[o],s+="\n--"+u+r;return(s+"\n--"+u+"--").replace(/\n/g,"\r\n")},g=l(e.body),n?h=g:(f=c(e),t=a(g,f),u=i(e.cids),h=d(t,u)),r=o(e.attaches),b=s(h,r)},e=function(e){var t,r,u,a,o,i,c,s;r=function(e){var t,u,a,o,i,c,s,l,d,p,f,m,g,h,b,C;if(l=e.indexOf("\r\n\r\n"),-1===l&&(e=e.replace(/\n/g,"\r\n"),l=e.indexOf("\r\n\r\n"),-1===l&&(l=e.length)),p=e.slice(0,l).replace(/\r\n\s+/g," ")+"\r\n",d=e.slice(l).replace(/(\r\n)+$/,"").replace(/^(\r\n)+/,""),c="",h=p.match(/Content-Type: (.*)/i),h&&h.length>0?c=h[1]:console.log("Warning: MailParser: Content-type doesn't exist!"),s=c.split(";"),m=s[0].replace(/\s/g,""),g=m.split("/"),"multipart"===g[0].toLowerCase())for(o=[],f=s[1].match(/boundary="?([^"]*)"?/i),!f&&s[2]&&(f=s[2].match(/boundary="?([^"]*)"?/i)),i=n.trim(f[1]).replace(/"/g,""),t=i.replace(/\+/g,"\\+"),b=new RegExp("--"+t,"g"),o=d.replace(b,i).replace(b,i).split(i),o.shift(),o.pop(),u=0;u<o.length;)o[u]=o[u].replace(/(\r\n)+$/,"").replace(/^(\r\n)+/,""),o[u]=r(o[u]),u++;else if(a=d,"text"===g[0]&&(a=a.replace(RegExp("=\\r\\n","g"),""),C=a.match(RegExp("=[A-F0-9][A-F0-9]","g"))))for(u=0;u<C.length;)a=a.replace(C[u],String.fromCharCode(parseInt(C[u].replace(RegExp("="),""),16))),u++;return{rawHeaders:p,rawBody:d,body:a,contentType:c,contentTypeParts:s,boundary:i,bodyParts:o,mimeType:m,mimeTypeParts:g}},o="";try{o=r(e)}catch(l){}return i=o.rawHeaders,a=function(e){return null==e&&(e=[]),e[1]||""},c=a(/\r\nSubject: (.*)\r\n/g.exec(i)),s=a(/\r\nTo: (.*)\r\n/g.exec(i)),t=a(/\r\nCc: (.*)\r\n/g.exec(i)),u=a(/\r\nFrom: (.*)\r\n/g.exec(i)),{messageParts:o,subject:c,to:s,cc:t,from:u}},n=function(){var e,t,r,u,a,o,i,c;return i=function(e){return null==e&&(e=""),("function"==typeof e.trim?e.trim():void 0)||e.replace(/^\s+|\s+$/g,"")},u=function(n,t){var r;return null==n&&(n=""),null==t&&(t=""),t=t.toLowerCase(),r=function(){switch(!1){case-1===t.indexOf("koi8-r"):return e(n);case-1===t.indexOf("utf-8"):return Base64._utf8_decode(n);case-1===t.indexOf("windows-1251"):return c(n);default:return n}}()},t=function(e){return e.replace(/\=[\r\n]+/g,"").replace(/\=[0-9A-F]{2}/gi,function(e){return String.fromCharCode(parseInt(e.substr(1),16))})},e=function(e){var n,t,r,u,a,o,i;for(n=unescape("%u2500%u2502%u250C%u2510%u2514%u2518%u251C%u2524%u252C%u2534%u253C%u2580%u2584%u2588%u258C%u2590%u2591%u2592%u2593%u2320%u25A0%u2219%u221A%u2248%u2264%u2265%u00A0%u2321%u00B0%u00B2%u00B7%u00F7%u2550%u2551%u2552%u0451%u2553%u2554%u2555%u2556%u2557%u2558%u2559%u255A%u255B%u255C%u255D%u255E%u255F%u2560%u2561%u0401%u2562%u2563%u2564%u2565%u2566%u2567%u2568%u2569%u256A%u256B%u256C%u00A9%u044E%u0430%u0431%u0446%u0434%u0435%u0444%u0433%u0445%u0438%u0439%u043A%u043B%u043C%u043D%u043E%u043F%u044F%u0440%u0441%u0442%u0443%u0436%u0432%u044C%u044B%u0437%u0448%u044D%u0449%u0447%u044A%u042E%u0410%u0411%u0426%u0414%u0415%u0424%u0413%u0425%u0418%u0419%u041A%u041B%u041C%u041D%u041E%u041F%u042F%u0420%u0421%u0422%u0423%u0416%u0412%u042C%u042B%u0417%u0428%u042D%u0429%u0427%u042A"),t=function(e){return e>=128&&255>=e?n.charAt(e-128):String.fromCharCode(e)},o="",r=u=0,a=e.length;a>u;r=++u)i=e[r],o+=t(e.charCodeAt(r));return o},c=function(e){var n,t,r,u,a,o,i;for(null==e&&(e=""),o="",n=r=0,u=e.length;u>r;n=++r)i=e[n],t=e.charCodeAt(n),a=function(){switch(!1){case 168!==t:return 1025;case 184!==t:return 1105;case!(t>191&&256>t):return t+848;default:return t}}(),o+=String.fromCharCode(a);return o},r=function(e,r){var a,o,i;return e=n.trim(e),o=void 0,a=void 0,i=void 0,(i=e.match(/^\=\?([\w_\-]+)\?([QqBb])\?([^\?]*)\?\=$/i))?(o=i[1],a=(i[2]||"Q").toString().toUpperCase(),e=(i[3]||"").replace(/_/g," "),"B"===a?Base64.decode(e,r):"Q"===a?t(e):e):u(e,r)},a=function(e,n){return e=(e||"").toString().replace(/(=\?[^?]+\?[QqBb]\?[^?]+\?=)\s+(?==\?[^?]+\?[QqBb]\?[^?]*\?=)/g,"$1").replace(/\=\?([\w_\-]+)\?([QqBb])\?[^\?]*\?\=/g,function(e,n,t){return r(e)}.bind(this)),u(e,n)},o=function(e){return null==e&&(e=""),(e+"").replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;")},{decode:u,KOIRDec:e,win1251Dec:c,decodeMimeWords:a,toHtmlEntity:o,trim:i}}(),t=function(t){var u,a,o,i,c,s,l,d,p,f,m,g;f={html:"",text:"",attaches:[],innerMsgs:[],to:n.decodeMimeWords(t.to),cc:n.decodeMimeWords(t.cc),from:n.decodeMimeWords(t.from),subject:n.decodeMimeWords(t.subject)},a=function(e,t){var r,u,a;return a=/Content-Transfer-Encoding: quoted-printable/i.test(t),u=/Content-Transfer-Encoding: base64/i.test(t),u?(e=e.replace(/\s/g,""),r="function"==typeof atob?atob(e):void 0,null==r&&(r=Base64.decode(e)),e=r):a&&(e=n.QPDec(e)),e},d=function(t){var u,o,i,c,s,l,p,m,g,h,b,C,y,x,T,v,w,B,A,E,M,D,F,$,O;if(t){for(h=0,C=t.length;C>h;h++)if(w=t[h],x=(null!=(A=w.mimeType)?A:"").toLowerCase(),-1===x.indexOf("multipart"))if(-1===x.indexOf("message/rfc822"))if(B=w.rawHeaders,c=-1!==B.indexOf("Content-Disposition: attachment"),o=w.rawBody,l=/text\/html/.test(x),m=/text\/plain/.test(x),p=/image/.test(x),s=/audio/.test(x),c||p||s){for(g=/Content-Transfer-Encoding: quoted-printable/i.test(B),g&&(o=n.QPDec(o),o=btoa?btoa(o):Base64.encode(o)),E=w.contentTypeParts,b=0,y=E.length;y>b;b++)if(O=E[b],/name=/i.test(O)){T=O.replace(/(.*)=/,"").replace(/"|'/g,"");break}T||(T=p?"image":s?"audio":"attachment",T+="_"+Math.floor(100*Math.random()),F=x.indexOf("/"),$=x.substring(F+1),$.length<4&&(T+="."+$)),D=/(.*)content-id:(.*)<(.*)>/i,u={type:x,base64:o,name:T,cid:null!=(M=D.exec(B))?M[3]:void 0,visible:/png|jpeg|jpg|gif/.test(x)},f.attaches.push(u)}else l||m?(o=a(o,B),o=n.decode(o,w.contentType),l&&(f.html+=o),m&&(f.text+=o)):console.log("Unknown mime type: "+x);else v=e(w.rawBody),i=r(v),f.innerMsgs.push(i);else d(w.bodyParts);return null}};try{if(p=t.messageParts,!p)return f;l=(p.mimeType||"").toLowerCase(),c=/text\/plain/.test(l),i=/text\/html/.test(l),-1!==l.indexOf("multipart")?d(p.bodyParts):c||i?(u=a(p.body,p.rawHeaders),u=n.decode(u,p.contentType),i&&(f.html=u),c&&(f.text=u)):console.log("Warning: mime type isn't supported! mime="+l)}catch(h){throw o=h,new Error(o)}return g=function(e){return"<pre>"+n.toHtmlEntity(e)+"</pre>"},s=function(e){var t,r,u,a,o,i,c,l;if(u=e.innerMsgs,null!=u?u.length:void 0)for(!n.trim(e.html)&&e.text&&(e.html+=g(e.text)),a=0,o=u.length;o>a;a++)r=u[a],i=s(r),l=i.text,t=i.html,t?e.html+=t:l&&(e.html+=wrapPerTag(l),e.text+=l),(null!=(c=i.attaches)?c.length:void 0)>0&&(e.attaches=e.attaches.concat(i.attaches));return e},m=s(f)},r=function(n){var r,u;return u=e(n),r=t(u)},{toMimeTxt:u,toMimeObj:r}}()}).call(this); +\ No newline at end of file diff --git a/src/mime-js.coffee b/src/mime-js.coffee @@ -13,6 +13,7 @@ window.Mime = do -> # var mail = { # "to": "email1@example.com, email2@example.com", +# "cc": "email3@example.com, email4@example.com", # "subject": "Today is rainy", # "fromName": "John Smith", # "from": "john.smith@mail.com", @@ -20,7 +21,7 @@ window.Mime = do -> # "cids": [], # "attaches" : [] # } - toMimeTxt = (mail) -> + toMimeTxt = (mail, txtOnly) -> linkify = (inputText) -> #URLs starting with http://, https://, or ftp:// replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim @@ -124,6 +125,7 @@ window.Mime = do -> '\nSubject: ' + subject + '\nFrom: ' + mailFromName + ' <' + mail.from + '>' + '\nTo: ' + mail.to + + '\nCc: ' + mail.cc + '\nContent-Type: multipart/mixed; boundary=' + boundary + '\n\n--' + boundary + related @@ -134,10 +136,14 @@ window.Mime = do -> plain = createPlain mail.body - htm = createHtml mail - alternative = createAlternative plain, htm - cids = createCids mail.cids - related = createRelated alternative, cids + if txtOnly + related = plain + else + htm = createHtml mail + alternative = createAlternative plain, htm + cids = createCids mail.cids + related = createRelated alternative, cids + attaches = createAttaches mail.attaches result = createMixed(related, attaches) @@ -222,12 +228,14 @@ window.Mime = do -> subject = getValidStr((/\r\nSubject: (.*)\r\n/g).exec(rawHeaders)) to = getValidStr((/\r\nTo: (.*)\r\n/g).exec(rawHeaders)) + cc = getValidStr((/\r\nCc: (.*)\r\n/g).exec(rawHeaders)) from = getValidStr((/\r\nFrom: (.*)\r\n/g).exec(rawHeaders)) { messageParts: messageParts subject: subject to: to + cc: cc from: from } @@ -333,6 +341,7 @@ window.Mime = do -> attaches: [] innerMsgs: [] to: _util.decodeMimeWords(rawMailObj.to) + cc: _util.decodeMimeWords(rawMailObj.cc) from: _util.decodeMimeWords rawMailObj.from subject: _util.decodeMimeWords rawMailObj.subject