@@ -60,9 +60,6 @@ module.exports = function(client) {
6060 * @returns {Promise }
6161 */
6262 send : function ( transmission , options , callback ) {
63-
64- const modifiedTransmission = _ . cloneDeep ( transmission ) ;
65-
6663 // Handle optional options argument
6764 if ( typeof options === 'function' ) {
6865 callback = options ;
@@ -73,16 +70,11 @@ module.exports = function(client) {
7370 return Promise . reject ( new Error ( 'transmission object is required' ) ) . asCallback ( callback ) ;
7471 }
7572
76- // format the payload if we are given an array of recipients
77- if ( _ . has ( modifiedTransmission , 'recipients' ) &&
78- _ . isArray ( modifiedTransmission . recipients ) &&
79- ! _ . has ( modifiedTransmission . recipients [ 0 ] , 'list_id' ) ) {
80- formatPayload ( modifiedTransmission ) ;
81- }
73+ transmission = formatPayload ( transmission ) ;
8274
8375 const reqOpts = {
8476 uri : api ,
85- json : modifiedTransmission ,
77+ json : transmission ,
8678 qs : options
8779 } ;
8880
@@ -92,7 +84,14 @@ module.exports = function(client) {
9284
9385} ;
9486
95- function formatPayload ( transmission ) {
87+ function formatPayload ( originalTransmission ) {
88+ const transmission = _ . cloneDeep ( originalTransmission ) ;
89+
90+ // don't format the payload if we are not given an array of recipients
91+ if ( ! _ . isArray ( transmission . recipients ) ) {
92+ return transmission ;
93+ }
94+
9695 // format all the original recipients to be in the object format
9796 transmission . recipients = _ . map ( transmission . recipients , ( recipient ) => {
9897 recipient . address = addressToObject ( recipient . address ) ;
@@ -101,53 +100,53 @@ function formatPayload(transmission) {
101100 } ) ;
102101
103102 // add the CC headers
104- if ( _ . has ( transmission , 'cc' ) && _ . isArray ( transmission . cc ) ) {
105- addCCHeader ( transmission ) ;
103+ if ( _ . isArray ( transmission . cc ) ) {
104+ _ . set ( transmission , 'content.headers.CC' , generateCCHeader ( transmission ) ) ;
106105 }
107106
108- addListToRecipients ( transmission , 'cc' ) ;
109- addListToRecipients ( transmission , 'bcc' ) ;
107+ const headerTo = generateHeaderTo ( transmission . recipients ) ;
108+
109+ transmission . recipients = addListToRecipients ( transmission , 'cc' , headerTo ) ;
110+ transmission . recipients = addListToRecipients ( transmission , 'bcc' , headerTo ) ;
111+
112+ delete transmission . cc ;
113+ delete transmission . bcc ;
110114
111115 return transmission ;
112116}
113117
114- function addListToRecipients ( transmission , listName ) {
115- if ( _ . has ( transmission , listName ) && _ . isArray ( transmission [ listName ] ) ) {
116- const headerTo = generateHeaderTo ( transmission . recipients ) ;
117- const list = transmission [ listName ] ;
118-
119- transmission . recipients = transmission . recipients . concat ( _ . map ( list , ( recipient ) => {
120- recipient . address = addressToObject ( recipient . address ) ;
118+ function addListToRecipients ( transmission , listName , headerTo ) {
119+ if ( ! _ . isArray ( transmission [ listName ] ) ) {
120+ return transmission . recipients ;
121+ }
121122
122- recipient . address . header_to = headerTo ;
123+ return transmission . recipients . concat ( _ . map ( transmission [ listName ] , ( recipient ) => {
124+ recipient . address = addressToObject ( recipient . address ) ;
123125
124- // remove name from address - name is only put in the header for cc and not at all for bcc
125- if ( _ . has ( recipient . address , 'name' ) ) {
126- delete recipient . address . name ;
127- }
126+ recipient . address . header_to = headerTo ;
128127
129- return recipient ;
130- } ) ) ;
128+ // remove name from address - name is only put in the header for cc and not at all for bcc
129+ if ( _ . has ( recipient . address , 'name' ) ) {
130+ delete recipient . address . name ;
131+ }
131132
132- delete transmission [ listName ] ;
133- }
133+ return recipient ;
134+ } ) ) ;
134135}
135136
136- function addCCHeader ( transmission ) {
137- // default the headers to a blank object
138- transmission . content . headers = transmission . content . headers || { } ;
139-
140- transmission . content . headers . CC = _ . map ( transmission . cc , ( ccRecipient ) => addressToString ( ccRecipient . address ) ) . join ( ', ' ) ;
137+ function generateCCHeader ( transmission ) {
138+ return _ . map ( transmission . cc , ( ccRecipient ) => addressToString ( ccRecipient . address ) ) . join ( ', ' ) ;
141139}
142140
143141function generateHeaderTo ( recipients ) {
142+ // if a recipient has a header_to then it is cc'd or bcc'd and we don't want it in the header_to value
144143 const originalRecipients = _ . filter ( recipients , ( recipient ) => ! _ . has ( recipient . address , 'header_to' ) ) ;
145144
146145 return _ . map ( originalRecipients , ( recipient ) => addressToString ( recipient . address ) ) . join ( ', ' ) ;
147146}
148147
149148function addressToString ( address ) {
150- if ( ! _ . isString ( address ) ) {
149+ if ( _ . isPlainObject ( address ) ) {
151150 if ( _ . has ( address , 'name' ) ) {
152151 address = `"${ address . name } " <${ address . email } >` ;
153152 } else {
@@ -164,24 +163,15 @@ function addressToObject(address) {
164163 if ( _ . isString ( address ) ) {
165164 addressObject = { } ;
166165
167- if ( isEmail ( address ) ) {
168- addressObject . email = address ;
169- } else {
170- const matches = / " ? ( .[ ^ " ] * ) ? " ? \s * < ( .+ ) > / gi. exec ( address ) ;
166+ const matches = / " ? ( .[ ^ " ] * ) ? " ? \s * < ( .+ ) > / gi. exec ( address ) ;
171167
172- if ( matches ) {
173- addressObject . name = matches [ 1 ] ;
174- addressObject . email = matches [ 2 ] ;
175- } else {
176- throw new Error ( `Invalid address format: ${ address } ` ) ;
177- }
168+ if ( matches ) {
169+ addressObject . name = matches [ 1 ] ;
170+ addressObject . email = matches [ 2 ] ;
171+ } else {
172+ addressObject . email = address ;
178173 }
179174 }
180175
181176 return addressObject ;
182177}
183-
184- function isEmail ( email ) {
185- const emailRegex = / ^ [ - a - z 0 - 9 ~ ! $ % ^ & * _ = + } { \' ? ] + ( \. [ - a - z 0 - 9 ~ ! $ % ^ & * _ = + } { \' ? ] + ) * @ ( [ a - z 0 - 9 _ ] [ - a - z 0 - 9 _ ] * ( \. [ - a - z 0 - 9 _ ] + [ a - z ] [ a - z ] ) | ( [ 0 - 9 ] { 1 , 3 } \. [ 0 - 9 ] { 1 , 3 } \. [ 0 - 9 ] { 1 , 3 } \. [ 0 - 9 ] { 1 , 3 } ) ) ( : [ 0 - 9 ] { 1 , 5 } ) ? $ / i;
186- return emailRegex . test ( email ) ;
187- }
0 commit comments