@@ -50,13 +50,15 @@ final class Message
50
50
51
51
public function __construct ($ subject = null , $ html = null , $ plain = null )
52
52
{
53
+ $ this ->boundary = dechex (rand (0 , 16777215 ));
53
54
$ this ->setHeader ("MIME-Version " , "1.0 " );
55
+ $ this ->setHeaderRaw ("Content-Type " , "multipart/mixed; boundary= \"r= {$ this ->boundary }\"" );
56
+ $ this ->setHeader ("Content-Transfer-Encoding " , "7bit " );
54
57
$ mailer = sprintf ("ddrv/mailer-%s (https://github.com/ddrv/mailer) " , Mailer::MAILER_VERSION );
55
58
$ this ->setHeader ("X-Mailer " , $ mailer );
56
59
$ this ->setSubject ($ subject );
57
60
$ this ->setHtmlBody ($ html );
58
61
$ this ->setPlainBody ($ plain );
59
- $ this ->boundary = dechex (rand (0 , 16777215 ));
60
62
}
61
63
62
64
public function setSubject ($ subject )
@@ -143,15 +145,30 @@ public function getBcc()
143
145
return $ this ->bcc ;
144
146
}
145
147
148
+ public function setFrom ($ email , $ name = "" )
149
+ {
150
+ $ email = (string )$ email ;
151
+ if (!$ email ) return false ;
152
+ if (!$ this ->checkEmail ($ email )) return false ;
153
+ $ contact = $ this ->getContact ($ email , $ name );
154
+ $ this ->setHeaderRaw ("From " , $ contact );
155
+ }
156
+
146
157
public function getRecipients ()
147
158
{
148
159
return array_keys (array_replace ($ this ->to , $ this ->cc , $ this ->bcc ));
149
160
}
150
161
151
162
public function setHeader ($ header , $ value )
163
+ {
164
+ $ this ->setHeaderRaw ($ header , $ this ->headerEncode ($ value ));
165
+ return $ this ;
166
+ }
167
+
168
+ private function setHeaderRaw ($ header , $ value )
152
169
{
153
170
$ header = (string )$ header ;
154
- $ value = str_replace ("\r\n" , "" , (string )$ value );
171
+ // $value = str_replace(array( "\r", " \n") , "", (string)$value);
155
172
if ($ value ) {
156
173
$ this ->headers [mb_strtolower ($ header )] = "$ header: $ value " ;
157
174
} else {
@@ -226,8 +243,6 @@ public function getSubject()
226
243
227
244
public function getHeaders ()
228
245
{
229
- $ this ->setHeader ("Content-Type " , "multipart/mixed; boundary= \"r= {$ this ->boundary }\"" );
230
- $ this ->setHeader ("Content-Transfer-Encoding " , "7bit " );
231
246
$ headers = array_values ($ this ->headers );
232
247
return $ headers ;
233
248
}
@@ -359,26 +374,26 @@ private function addAddress($type, $email, $name)
359
374
360
375
private function replaceHeaderTo ()
361
376
{
362
- $ this ->setHeader ("To " , implode (", " , $ this ->to ));
377
+ $ this ->setHeaderRaw ("To " , implode (", " , $ this ->to ));
363
378
}
364
379
365
380
private function replaceHeaderCc ()
366
381
{
367
- $ this ->setHeader ("Cc " , implode (", " , $ this ->cc ));
382
+ $ this ->setHeaderRaw ("Cc " , implode (", " , $ this ->cc ));
368
383
}
369
384
370
385
private function replaceHeaderBcc ()
371
386
{
372
- $ this ->setHeader ("Bcc " , implode (", " , $ this ->bcc ));
387
+ $ this ->setHeaderRaw ("Bcc " , implode (", " , $ this ->bcc ));
373
388
}
374
389
375
390
private function getContact ($ email , $ name = "" )
376
391
{
377
392
$ email = (string )$ email ;
378
393
$ name = preg_replace ("/[^\pL\s\,\.\d]/ui " , "" , (string )$ name );
379
394
$ name = trim ($ name );
380
- if (preg_match ("/[\,\.] /ui " , $ name )) $ name = "\" $ name\"" ;
381
- if ($ name ) $ name . = " " ;
395
+ if (preg_match ("/[^a-z0-9\s]+ /ui " , $ name )) $ name = $ this -> headerEncode ( $ name) ;
396
+ if ($ name ) $ name = "$ name " ;
382
397
return "$ name< $ email> " ;
383
398
}
384
399
@@ -414,4 +429,10 @@ public function unserialize($serialized)
414
429
$ this ->$ key = array_key_exists ($ key , $ raw ) ? $ raw [$ key ] : $ default ;
415
430
}
416
431
}
432
+
433
+ private function headerEncode ($ value )
434
+ {
435
+ $ value = mb_encode_mimeheader ($ value , "UTF-8 " , "B " , "\r\n" , 0 );
436
+ return $ value ;
437
+ }
417
438
}
0 commit comments