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');
3434var path = require ( 'path' ) ;
3535var CRLF = '\r\n' ;
3636var UNDEFINED = 'undefined' ;
37+ var BOOLEAN = 'boolean' ;
3738var REG_ESMTP = / \b e s m t p \b / i;
3839
3940var 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 */
172173Message . 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 */
226273Message . 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 */
246292Message . 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 ;
0 commit comments