Skip to content
Browse files

Fixed #48 - replace all whitechar (newlines, tabs, etc) with spaces)

  • Loading branch information...
1 parent 10eb182 commit 91fa0279215ea0d0d2eceb3b599c38522143f5c9 andris9 committed Oct 26, 2011
Showing with 85 additions and 84 deletions.
  1. +84 −83 lib/mail.js
  2. +1 −1 package.json
View
167 lib/mail.js
@@ -2,7 +2,7 @@ var mimelib = require("mimelib-noiconv"),
util = require("util"),
EventEmitter = require("events").EventEmitter,
fs = require("fs");
-
+
/*
* Load all engines, assuming each .js file in the root of the engines
* directory is an engine. To avoid collisions, dependencies should be
@@ -25,12 +25,12 @@ var X_MAILER_NAME = "Nodemailer",
/**
* mail
- *
+ *
* Provides an API to send e-mails with Node.js through a SMTP server.
* This API is unicode friendly, you don't have to escape non-ascii chars.
- *
+ *
* NB! Before sending any e-mails, update mail.SMTP with correct values
- *
+ *
**/
@@ -49,7 +49,7 @@ exports.EmailMessage = EmailMessage;
* mail.send_mail(params, callback) -> undefined
* - params (Object): e-mail data
* - callback (Function): will be run after completion
- *
+ *
* Shortcut function to send e-mails. See EmailMessage for param structure
**/
exports.send_mail = function(params, callback){
@@ -64,9 +64,9 @@ var gencounter = 0,
/**
* new mail.EmailMessage(params)
* - params (Object): message data (can be altered afterwards)
- *
+ *
* Creates an object to send an e-mail.
- *
+ *
* params can hold the following data
*
* - **server** server to send message to (will default to exports.SMTP)
@@ -83,9 +83,9 @@ var gencounter = 0,
* - **attachments** an array of attachements of {filename, contents, cid}
* filename (mandatory) is a String, contents can be String or Buffer,
* cid is optional.
- *
+ *
* - **debug** if set, outputs the whole communication of SMTP server to console
- *
+ *
* All the params can be edited/added after defining the object
*
* Events:
@@ -98,7 +98,7 @@ var gencounter = 0,
* em.to = 'andris@kreata.ee'
* em.body = "Hello world!";
* em.send(function(error, success){});
- *
+ *
* NB! mail.SMTP needs to be set before sending any e-mails!
**/
function EmailMessage(params){
@@ -111,24 +111,25 @@ function EmailMessage(params){
this.cc = params.cc;
this.bcc = params.bcc;
this.reply_to = params.reply_to;
- this.subject = params.subject || "";
+ // replace newlines etc. with regular spaces in subject
+ this.subject = (params.subject || "").replace(/\s/g, " ");
this.body = params.body || "";
this.html = params.html;
this.attachments = params.attachments || [];
-
+
this.debug = !!params.debug;
-
+
this.charset = params.charset || "UTF-8";
this.encoding = params.encoding || "quoted-printable";
-
+
this.callback = null;
}
var utillib = require("util");
util.inherits(EmailMessage,EventEmitter);
/**
* mail.EmailMessage#prepareVariables() -> undefined
- *
+ *
* Prepares some needed variables
**/
EmailMessage.prototype.prepareVariables = function(){
@@ -138,7 +139,7 @@ EmailMessage.prototype.prepareVariables = function(){
//'=_' is not valid quoted printable
//'?' is not in any known base64 extension
this.content_boundary = "----NODEMAILER-?=_"+(++gencounter)+"-"+Date.now();
-
+
// defaults to multipart/mixed but if there's attachments with cid value set
// use multipart/related - mail clients hide the duplicates this way
var mixed = "mixed";
@@ -159,19 +160,19 @@ EmailMessage.prototype.prepareVariables = function(){
/**
* mail.EmailMessage#generateHeaders() -> String
- *
+ *
* Generates a header string where lines are separated by \r\n
**/
EmailMessage.prototype.generateHeaders = function(){
-
+
var headers = [];
-
+
// Mime
headers.push([
"X-Mailer",
X_MAILER_NAME+" ("+X_MAILER_VERSION+")"
].join(": "));
-
+
// add custom
var keys = Object.keys(this.headers);
for(var i=0, len=keys.length; i<len; i++){
@@ -180,7 +181,7 @@ EmailMessage.prototype.generateHeaders = function(){
this.headers[keys[i]]
].join(": "));
}
-
+
// Date
var date = new Date();
headers.push([
@@ -194,27 +195,27 @@ EmailMessage.prototype.generateHeaders = function(){
headers.push([
upperFirst("From"),
from
- ].join(": "));
+ ].join(": "));
}
-
+
// To
var to = this.generateAddresses(this.to, 0, "toAddress");
if(to.length){
headers.push([
upperFirst("To"),
to
- ].join(": "));
+ ].join(": "));
}
-
+
// CC
var cc = this.generateAddresses(this.cc, 0, "toAddress");
if(cc.length){
headers.push([
upperFirst("Cc"),
cc
- ].join(": "));
+ ].join(": "));
}
-
+
// BCC
var bcc = this.generateAddresses(this.bcc, 0, "toAddress");
if(exports.sendmail && bcc.length){
@@ -232,33 +233,33 @@ EmailMessage.prototype.generateHeaders = function(){
reply_to
].join(": "));
}
-
+
// Subject
headers.push([
upperFirst("Subject"),
- this.subject && (hasUTFChars(this.subject) &&
+ this.subject && (hasUTFChars(this.subject) &&
mimelib.encodeMimeWord(this.subject, "Q") || this.subject) || ''
].join(": "));
// Mime
headers.push([
"MIME-Version",
"1.0"
- ].join(": "));
-
+ ].join(": "));
+
// Content-type
headers.push([
"Content-Type",
this.content_type
- ].join(": "));
-
+ ].join(": "));
+
if(!this.content_multipart){
headers.push([
"Content-Transfer-Encoding",
this.content_transfer_encoding
].join(": "));
}
-
+
// output
return headers.map(function(elm){
return mimelib.foldLine(elm);
@@ -267,7 +268,7 @@ EmailMessage.prototype.generateHeaders = function(){
/**
* mail.EmailMessage#generateBody() -> String
- *
+ *
* Generates a body string. If this is a multipart message then different
* parts will be separated by boundary, body+html are put into separate
* multipart/alternate block
@@ -281,23 +282,23 @@ EmailMessage.prototype.generateBody = function(){
return this.body;
}
}
-
+
var body_boundary = this.content_mixed?
"----NODEMAILER-?=_"+(++gencounter)+"-"+Date.now():
this.content_boundary,
rows = [];
-
+
if(this.content_mixed){
rows.push("--"+this.content_boundary);
rows.push("Content-Type: multipart/alternative; boundary=\""+body_boundary+"\"");
rows.push("");
}
-
+
if(!this.body.trim() && this.html){
this.body = stripHTML(this.html);
}
-
+
// body
rows.push("--"+body_boundary);
rows.push("Content-Type: text/plain; charset="+this.charset);
@@ -311,7 +312,7 @@ EmailMessage.prototype.generateBody = function(){
rows.push(this.body.trim());
}
rows.push("");
-
+
// html
if(this.html){
rows.push("--"+body_boundary);
@@ -327,11 +328,11 @@ EmailMessage.prototype.generateBody = function(){
}
rows.push("");
}
-
+
if(this.content_mixed){
rows.push("--"+body_boundary+"--");
}
-
+
// attachments
var current;
for(var i=0; i<this.attachments.length; i++){
@@ -347,35 +348,35 @@ EmailMessage.prototype.generateBody = function(){
disposition: "attachment",
content_id: this.attachments[i].cid || ((++gencounter)+"."+Date.now()+"@"+(this.SERVER && this.SERVER.hostname || "localhost"))
};
-
-
+
+
rows.push("--"+this.content_boundary);
-
+
rows.push("Content-Type: "+current.mime_type+"; name=\""+current.filename+"\"");
rows.push("Content-Disposition: "+current.disposition+"; filename=\""+current.filename+"\"");
rows.push("Content-ID: <"+current.content_id+">");
-
+
rows.push("Content-Transfer-Encoding: base64");
rows.push("");
-
+
/* Changed to conform with RFC-2045 section 6.7. */
rows.push(current.contents.toString("base64").replace(/.{76}/g,"$&\r\n"));
-
+
}
-
-
+
+
rows.push("--"+this.content_boundary+"--");
-
+
return rows.join("\r\n");
-
+
};
/**
* mail.EmailMessage#generateAddresses(addresses, limit, use_list) -> String
* - addresses (String): Comma separated list of addresses
* - limit (String): How many addresses will be used from the list
* - use_list (String): property name where to add plain e-mail addresses
- *
+ *
* Parses an address string, finds the data and normalizes it. If use_string
* is set, (ie. "toAddress") then found e-mail addresses are appended to
* a list with the same name (this.toAddress). Plain e-mail addresses are
@@ -384,25 +385,25 @@ EmailMessage.prototype.generateBody = function(){
EmailMessage.prototype.generateAddresses = function(addresses, limit, use_list){
var parsed, output = [], current;
-
+
limit = limit || 0;
-
+
try{
parsed = mimelib.parseAddresses(addresses);
}catch(E){parsed = [];}
-
+
var list = [];
for(var i=0; i<parsed.length; i++){
current = parsed[i];
current.address = current.address && current.address.trim();
if(!current.address)continue;
-
+
list.push(current.address);
-
+
if(hasUTFChars(current.address)){
current.address = mimelib.encodeMimeWord(current.address, "Q");
}
-
+
if(current.name){
current.name = upperFirst(current.name.trim(), true);
if(hasUTFChars(current.name)){
@@ -420,22 +421,22 @@ EmailMessage.prototype.generateAddresses = function(addresses, limit, use_list){
output = output.slice(0, limit);
list = list.slice(0, limit);
}
-
+
if(use_list){
if(!this[use_list]){
this[use_list] = list;
}else{
this[use_list] = this[use_list].concat(list);
}
}
-
+
return output.join(", ");
};
/**
* mail.EmailMessage#send(callback) -> undefined
* - callback (Function): function to be called if sending succeedes or fails
- *
+ *
* Generates a full message body and forwards it to the SMTP server.
* callback gets two params - error and success. If error is set, then
* something bad happened, if there's no error but success is false, then
@@ -451,11 +452,11 @@ EmailMessage.prototype.send = function(callback){
console.log(str);
console.log(Array(str.length+1).join("=")+"\n");
}
-
+
if(this.SERVER && !exports.SMTP){
return this.send_using_engine(this.SERVER, "SMTP", instanceId, callback);
}
-
+
//Use the engine configured by the user
keys = Object.keys(engines);
for(i=0, len = keys.length; i < len; i++) {
@@ -464,7 +465,7 @@ EmailMessage.prototype.send = function(callback){
return this.send_using_engine(exports[engine], engine, instanceId, callback);
}
}
-
+
callback(new Error("Transfer method not defined"));
};
@@ -474,14 +475,14 @@ EmailMessage.prototype.send = function(callback){
* - engineId (String): engine name
* - instanceId (Number): number of the message
* - callback (Function): function to be called if sending succeedes or fails
- *
+ *
* Initiates a message sending using defined engine
**/
EmailMessage.prototype.send_using_engine = function(engine, engineId, instanceId, callback){
-
+
engine.instanceId = instanceId;
engines[engineId].send(this, engine, callback);
-
+
if(this.debug){
console.log("Used transfer method: " + engineId + "\n");
}
@@ -491,7 +492,7 @@ EmailMessage.prototype.send_using_engine = function(engine, engineId, instanceId
/**
* getMimeType(filename) -> String
* - filename (String): Failinimi, mille mime tüüpi otsida
- *
+ *
* Otsib välja faililaiendi alusel õige mime tüübi, vaikimisi
* kui tüüpi ei leita on application/octet-stream
**/
@@ -505,11 +506,11 @@ function getMimeType(filename){
/**
* upperFirst(str) -> String
* - str (String): string to be converted
- *
+ *
* Converts first letters upper case, other lower case
- *
+ *
* "x-name-value" -> "X-Name-Value"
- *
+ *
**/
function upperFirst(str, keepUpper){
if(!keepUpper){
@@ -523,7 +524,7 @@ function upperFirst(str, keepUpper){
/**
* hasUTFChars(str) -> Boolean
* - str (String): String to be checked for non-ascii chars
- *
+ *
* Tries to detect if a string has non-ascii characters. In this case the
* string needs to be encoded before sent to the SMTP server
**/
@@ -535,27 +536,27 @@ function hasUTFChars(str){
/**
* stripHTML(str) -> String
* - str (String): HTML string to be converted
- *
+ *
* Converts a HTML string into plain text
**/
function stripHTML(str){
if(!str)return str;
-
+
str = str instanceof Buffer ? str.toString("utf-8"):str;
-
+
str = str.replace(/\r?\n/g," ");
str = str.replace(/<(?:\/p|br|\/tr|\/table|\/div)>/g,"\n");
// hide newlines with two 00 chars (enables multiline matches)
str = str.replace(/\r?\n/g,"-\u0000\u0000-");
-
+
// H1-H6, add underline
str = str.replace(/<[hH]\d[^>]*>(.*?)<\/[hH]\d[^>]*>/g,function(a,b){
var line = "";
b = b.replace(/<[^>]*>/g," ");
b = b.replace(/\s\s+/g," ");
b = b.trim();
-
+
if(!b)
return "";
for(var i=0, len = b.length; i<len; i++){
@@ -569,7 +570,7 @@ function stripHTML(str){
b = b.replace(/<[^>]*>/g," ");
b = b.replace(/\s\s+/g," ");
b = b.trim();
-
+
if(!b)
return "";
return "-®®®®-* "+b+"\n";
@@ -580,18 +581,18 @@ function stripHTML(str){
b = b.replace(/<[^>]*>/g," ");
b = b.replace(/\s\s+/g," ");
b = b.trim();
-
+
if(!b)
return "";
b = b.replace(/[ \t]*\n[ \t]*/g,"\n-®®®®--®®®®-");
-
+
return "\n-®®®®--®®®®-"+b.trim()+"\n\n";
});
- // restore
+ // restore
str = str.replace(/\s*-\u0000\u0000-\s*/g,"\n");
-
+
// remove all remaining html tags
str = str.replace(/<[^>]*>/g," ");
// remove duplicate spaces
View
2 package.json
@@ -1,7 +1,7 @@
{
"name": "nodemailer",
"description": "Easy to use module to send e-mails, supports unicode and SSL/TLS",
- "version": "0.2.2",
+ "version": "0.2.3",
"author" : "Andris Reinman",
"maintainers":[
{

0 comments on commit 91fa027

Please sign in to comment.
Something went wrong with that request. Please try again.