@@ -63,7 +63,6 @@ public function setHeader(string $name, $value, bool $append = false)
6363 if (!$ append ) {
6464 unset($ this ->headers [$ name ]);
6565 }
66-
6766 } elseif (is_array ($ value )) { // email
6867 $ tmp = &$ this ->headers [$ name ];
6968 if (!$ append || !is_array ($ tmp )) {
@@ -78,17 +77,19 @@ public function setHeader(string $name, $value, bool $append = false)
7877 } elseif (preg_match ('#[\r\n]# ' , $ recipient )) {
7978 throw new Nette \InvalidArgumentException ('Name must not contain line separator. ' );
8079 }
80+
8181 Nette \Utils \Validators::assert ($ email , 'email ' , "header ' $ name' " );
8282 $ tmp [$ email ] = $ recipient ;
8383 }
84-
8584 } else {
8685 $ value = (string ) $ value ;
8786 if (!Strings::checkEncoding ($ value )) {
8887 throw new Nette \InvalidArgumentException ('Header is not valid UTF-8 string. ' );
8988 }
89+
9090 $ this ->headers [$ name ] = preg_replace ('#[\r\n]+# ' , ' ' , $ value );
9191 }
92+
9293 return $ this ;
9394 }
9495
@@ -131,8 +132,10 @@ public function getEncodedHeader(string $name): ?string
131132 $ s .= self ::encodeSequence ($ name , $ offset , self ::SEQUENCE_WORD );
132133 $ email = " < $ email> " ;
133134 }
135+
134136 $ s .= self ::append ($ email . ', ' , $ offset );
135137 }
138+
136139 return ltrim (substr ($ s , 0 , -1 )); // last comma
137140
138141 } elseif (preg_match ('#^(\S+; (?:file)?name=)"(.*)"$#D ' , $ this ->headers [$ name ], $ m )) { // Content-Disposition
@@ -230,8 +233,10 @@ public function getEncodedMessage(): string
230233 if ($ this ->parts && $ name === 'Content-Type ' ) {
231234 $ output .= '; ' . self ::EOL . "\tboundary= \"$ boundary \"" ;
232235 }
236+
233237 $ output .= self ::EOL ;
234238 }
239+
235240 $ output .= self ::EOL ;
236241
237242 $ body = $ this ->body ;
@@ -264,9 +269,11 @@ public function getEncodedMessage(): string
264269 if (substr ($ output , -strlen (self ::EOL )) !== self ::EOL ) {
265270 $ output .= self ::EOL ;
266271 }
272+
267273 foreach ($ this ->parts as $ part ) {
268274 $ output .= '-- ' . $ boundary . self ::EOL . $ part ->getEncodedMessage () . self ::EOL ;
269275 }
276+
270277 $ output .= '-- ' . $ boundary . '-- ' ;
271278 }
272279
@@ -289,6 +296,7 @@ private static function encodeSequence(string $s, int &$offset = 0, int $type =
289296 if ($ type && preg_match ('#[^ a-zA-Z0-9!\#$%& \'*+/?^_`{|}~-]# ' , $ s )) { // RFC 2822 atext except =
290297 return self ::append ('" ' . addcslashes ($ s , '" \\' ) . '" ' , $ offset );
291298 }
299+
292300 return self ::append ($ s , $ offset );
293301 }
294302
@@ -309,6 +317,7 @@ private static function encodeSequence(string $s, int &$offset = 0, int $type =
309317 if ($ type === self ::SEQUENCE_VALUE ) {
310318 $ s = '" ' . $ s . '" ' ;
311319 }
320+
312321 $ s = str_replace ("\n " , "\n\t" , $ s );
313322 return $ o . $ s ;
314323 }
@@ -320,6 +329,7 @@ private static function append(string $s, int &$offset = 0): string
320329 $ offset = 1 ;
321330 $ s = self ::EOL . "\t" . $ s ;
322331 }
332+
323333 $ offset += strlen ($ s );
324334 return $ s ;
325335 }
0 commit comments