Skip to content

Commit 08ad1ce

Browse files
committed
Updated MailMessage (to, cc supports display name).
1 parent 2652f09 commit 08ad1ce

4 files changed

Lines changed: 86 additions & 32 deletions

File tree

changes.txt

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
======= 1.9.4 (BETA)
22

3-
- added: U.keywords(content, [forSearch], [alternative(true|false|soundex)], [max_count(200)], [max_length(20)], [min_length(2)]);
4-
- added: String.prototype.keywords([forSearch], [alternative(true|false|soundex)], [max_count(200)], [max_length(20)], [min_length(2)])
5-
- added: String.prototype.soundex()
6-
7-
- updated: Date.format() supports "w" and "ww" for week number
8-
- updated: Date.add() supports "w", "ww", "week", "weeks"
3+
- added: `U.keywords(content, [forSearch], [alternative(true|false|soundex)], [max_count(200)], [max_length(20)], [min_length(2)]);`
4+
- added: `String.prototype.keywords([forSearch], [alternative(true|false|soundex)], [max_count(200)], [max_length(20)], [min_length(2)])`
5+
- added: `String.prototype.soundex()`
6+
7+
- updated: `Date.format()` supports `w` and `ww` for week number
8+
- updated: `Date.add()` supports `w`, `ww`, `week`, `weeks`
9+
- updated: MailMessage supports display name `mail.from('Name <vali@demail>');`
10+
- updated: MailMessage supports display name `mail.to('Name <vali@demail>');`
11+
- updated: MailMessage supports display name `mail.to(email, [name], [clear]);`
12+
- updated: MailMessage supports display name `mail.cc('Name <vali@demail>');`
13+
- updated: MailMessage supports display name `mail.cc(email, [name], [clear]);`
914

1015
- fixed: async() error handling
1116
- fixed: NoSQL embedded paths

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ function Framework() {
406406

407407
this.id = null;
408408
this.version = 1940;
409-
this.version_header = '1.9.4-12';
409+
this.version_header = '1.9.4-13';
410410

411411
var version = process.version.toString().replace('v', '').replace(/\./g, '');
412412
if (version[0] !== '0' || version[1] !== '0')

mail.js

Lines changed: 73 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
/**
2323
* @module FrameworkMail
24-
* @version 1.9.3
24+
* @version 1.9.4
2525
*/
2626

2727
'use strict'
@@ -34,6 +34,7 @@ var fs = require('fs');
3434
var path = require('path');
3535
var CRLF = '\r\n';
3636
var UNDEFINED = 'undefined';
37+
var BOOLEAN = 'boolean';
3738
var REG_ESMTP = /\besmtp\b/i;
3839

3940
var errors = {
@@ -164,13 +165,19 @@ Message.prototype.sender = function(address, name) {
164165
};
165166

166167
/**
167-
* Set sender
168-
* @param {String} address A valid e-mail address.
169-
* @param {String} name User name.
168+
* Set sender email and name
169+
* @param {String} address A valid e-mail address.
170+
* @param {String} name An user name.
170171
* @return {Message}
171172
*/
172173
Message.prototype.from = function(address, name) {
173174

175+
if (address[address.length - 1] === '>') {
176+
var index = address.indexOf('<');
177+
name = address.substring(0, index - 1);
178+
address = address.substring(index + 1, address.length - 1);
179+
}
180+
174181
if (!address.isEmail())
175182
throw new Error(errors.notvalid);
176183

@@ -184,26 +191,61 @@ Message.prototype.from = function(address, name) {
184191

185192
/**
186193
* Add a recipient
187-
* @param {String} address A valid e-mail addrčess.
194+
* @param {String} address A valid e-mail address.
195+
* @param {String} name An user name (optional).
196+
* @param {Boolean} clear Clear all "to" address (optional, default: false).
188197
* @return {Message}
189198
*/
190-
Message.prototype.to = function(address) {
199+
Message.prototype.to = function(address, name, clear) {
200+
201+
if (typeof(name) === BOOLEAN) {
202+
clear = name;
203+
name = undefined;
204+
}
205+
206+
if (address[address.length - 1] === '>') {
207+
var index = address.indexOf('<');
208+
name = address.substring(0, index - 1);
209+
address = address.substring(index + 1, address.length - 1);
210+
}
191211

192212
if (!address.isEmail())
193213
throw new Error(errors.notvalid);
194214

195215
var self = this;
196-
self.addressTo.push(address);
216+
217+
if (clear)
218+
self.addressTo = new Array(0);
219+
220+
if (!name) {
221+
self.addressTo.push(address);
222+
return self;
223+
}
224+
225+
self.addressTo.push({ email: address, name: name });
197226
return self;
198227

199228
};
200229

201230
/**
202231
* Add a CC recipient
203-
* @param {String} address A valid e-mail addrčess.
232+
* @param {String} address A valid e-mail address.
233+
* @param {String} name An user name (optional).
234+
* @param {Boolean} clear Clear all "cc" address (optional, default: false).
204235
* @return {Message}
205236
*/
206-
Message.prototype.cc = function(address, clear) {
237+
Message.prototype.cc = function(address, name, clear) {
238+
239+
if (typeof(name) === BOOLEAN) {
240+
clear = name;
241+
name = undefined;
242+
}
243+
244+
if (address[address.length - 1] === '>') {
245+
var index = address.indexOf('<');
246+
name = address.substring(0, index - 1);
247+
address = address.substring(index + 1, address.length - 1);
248+
}
207249

208250
if (!address.isEmail())
209251
throw new Error(errors.notvalid);
@@ -213,14 +255,19 @@ Message.prototype.cc = function(address, clear) {
213255
if (clear)
214256
self.addressCC = new Array(0);
215257

216-
self.addressCC.push(address);
217-
return self;
258+
if (!name) {
259+
self.addressCC.push(address);
260+
return self;
261+
}
218262

263+
self.addressCC.push({ email: address, name: name });
264+
return self;
219265
};
220266

221267
/**
222268
* Add a BCC recipient
223-
* @param {String} address A valid e-mail addrčess.
269+
* @param {String} address A valid e-mail address.
270+
* @param {Boolean} clear Clear all "bcc" address (optional, default: false).
224271
* @return {Message}
225272
*/
226273
Message.prototype.bcc = function(address, clear) {
@@ -235,12 +282,11 @@ Message.prototype.bcc = function(address, clear) {
235282

236283
self.addressBCC.push(address);
237284
return self;
238-
239285
};
240286

241287
/**
242288
* Add a reply to address
243-
* @param {String} address A valid e-mail addrčess.
289+
* @param {String} address A valid e-mail address.
244290
* @return {Message}
245291
*/
246292
Message.prototype.reply = function(address, clear) {
@@ -255,7 +301,6 @@ Message.prototype.reply = function(address, clear) {
255301

256302
self.addressReply.push(address);
257303
return self;
258-
259304
};
260305

261306
/**
@@ -480,31 +525,35 @@ Message.prototype._send = function(socket, options, autosend) {
480525
length = self.addressTo.length;
481526
var builder = '';
482527
var mail;
528+
var item;
483529

484530
if (length) {
485-
486531
for (var i = 0; i < length; i++) {
487-
mail = '<' + self.addressTo[i] + '>';
532+
item = self.addressTo[i];
533+
if (item instanceof Object)
534+
mail = '<' + item.email + '>';
535+
else
536+
mail = '<' + item + '>';
488537
buffer.push('RCPT TO: ' + mail);
489-
builder += (builder !== '' ? ', ' : '') + mail;
538+
builder += (builder ? ', ' : '') + (item instanceof Object ? unicode_encode(item.name) + ' ' : '') + mail;
490539
}
491-
492540
message.push('To: ' + builder);
493541
builder = '';
494542
}
495543

496544
length = self.addressCC.length;
497545
if (length) {
498-
499546
for (var i = 0; i < length; i++) {
500-
mail = '<' + self.addressCC[i] + '>';
547+
item = self.addressCC[i];
548+
if (item instanceof Object)
549+
mail = '<' + item.email + '>';
550+
else
551+
mail = '<' + item + '>';
501552
buffer.push('RCPT TO: ' + mail);
502-
builder += (builder !== '' ? ', ' : '') + mail;
553+
builder += (builder ? ', ' : '') + (item instanceof Object ? unicode_encode(item.name) + ' ' : '') + mail;
503554
}
504-
505555
message.push('Cc: ' + builder);
506556
builder = '';
507-
508557
}
509558

510559
length = self.addressBCC.length;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
"name": "Gera G. Güiles",
4848
"email": "gerardo.gonzalezg@gmail.com"
4949
}],
50-
"version": "1.9.4-12",
50+
"version": "1.9.4-13",
5151
"homepage": "http://www.totaljs.com",
5252
"bugs": {
5353
"url": "https://github.com/totaljs/framework/issues",

0 commit comments

Comments
 (0)