Skip to content

Commit 16ae197

Browse files
committed
create method for body encoding and small cosmetics fixes
1 parent 428122a commit 16ae197

File tree

2 files changed

+48
-44
lines changed

2 files changed

+48
-44
lines changed

src/Contract/Message.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ interface Message extends Serializable
99
/**
1010
* @param string $email Sender email.
1111
* @param string|null $name Sender name.
12-
* @return self
12+
* @return $this
1313
*/
1414
public function setSender($email, $name = null);
1515

@@ -19,7 +19,7 @@ public function setSender($email, $name = null);
1919
public function getRecipients();
2020

2121
/**
22-
* @return string Rew string as email headers
22+
* @return string Raw string as email headers
2323
*/
2424
public function getHeadersRaw();
2525

src/Message.php

Lines changed: 46 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ final class Message implements MessageContract
138138
'eps' => 'application/postscript',
139139
'ps' => 'application/postscript',
140140

141-
// ms office
141+
// MS Office
142142
'doc' => 'application/msword',
143143
'rtf' => 'application/rtf',
144144
'xls' => 'application/vnd.ms-excel',
@@ -187,7 +187,7 @@ public function __clone()
187187

188188
/**
189189
* @param string|null $subject Subject of message.
190-
* @return self
190+
* @return $this
191191
*/
192192
public function setSubject($subject)
193193
{
@@ -198,7 +198,7 @@ public function setSubject($subject)
198198

199199
/**
200200
* @param string|null $html HTML text of message.
201-
* @return self
201+
* @return $this
202202
*/
203203
public function setHtml($html = null)
204204
{
@@ -216,7 +216,7 @@ public function setHtml($html = null)
216216

217217
/**
218218
* @param string|null $text Plain text of message.
219-
* @return self
219+
* @return $this
220220
*/
221221
public function setText($text = null)
222222
{
@@ -258,7 +258,7 @@ public function setSender($email, $name = null)
258258
* @param string $email Recipient email.
259259
* @param string|null $name Recipient name.
260260
* @param string $type Recipient type. May be 'to', 'cc' or 'bcc'. Default 'to'.
261-
* @return self
261+
* @return $this
262262
* @throws InvalidEmailException
263263
*/
264264
public function addRecipient($email, $name = null, $type = self::RECIPIENT_TO)
@@ -290,7 +290,7 @@ public function getRecipientName($email)
290290

291291
/**
292292
* @param string $email Recipient email.
293-
* @return self
293+
* @return $this
294294
*/
295295
public function removeRecipient($email)
296296
{
@@ -303,7 +303,7 @@ public function removeRecipient($email)
303303

304304
/**
305305
* @param string $type Recipient type. May be 'to', 'cc', 'bcc' or null. Default null.
306-
* @return self
306+
* @return $this
307307
*/
308308
public function removeRecipients($type = null)
309309
{
@@ -327,7 +327,7 @@ public function removeRecipients($type = null)
327327
* @param string $name
328328
* @param string $content
329329
* @param string|null $mime
330-
* @return self
330+
* @return $this
331331
*/
332332
public function attachFromString($name, $content, $mime = null)
333333
{
@@ -348,7 +348,7 @@ public function attachFromString($name, $content, $mime = null)
348348
* @param string $name
349349
* @param string $path
350350
* @param string|null $mime
351-
* @return self
351+
* @return $this
352352
*/
353353
public function attachFromFile($name, $path, $mime = null)
354354
{
@@ -368,7 +368,7 @@ public function attachFromFile($name, $path, $mime = null)
368368

369369
/**
370370
* @param string $name
371-
* @return self
371+
* @return $this
372372
*/
373373
public function detach($name)
374374
{
@@ -384,7 +384,7 @@ public function detach($name)
384384
* @param string $id
385385
* @param string $content
386386
* @param string $mime
387-
* @return self
387+
* @return $this
388388
*/
389389
public function setHtmlContentFromString($id, $content, $mime = 'application/octet-stream')
390390
{
@@ -405,7 +405,7 @@ public function setHtmlContentFromString($id, $content, $mime = 'application/oct
405405
* @param string $id
406406
* @param string $path
407407
* @param string $mime
408-
* @return self
408+
* @return $this
409409
*/
410410
public function setHtmlContentFromFile($id, $path, $mime = 'application/octet-stream')
411411
{
@@ -425,7 +425,7 @@ public function setHtmlContentFromFile($id, $path, $mime = 'application/octet-st
425425

426426
/**
427427
* @param string $id
428-
* @return self
428+
* @return $this
429429
*/
430430
public function unsetBodyHtmlContent($id)
431431
{
@@ -440,7 +440,7 @@ public function unsetBodyHtmlContent($id)
440440
/**
441441
* @param string $header Header name.
442442
* @param string|null $value Header values.
443-
* @return self
443+
* @return $this
444444
*/
445445
public function setHeader($header, $value)
446446
{
@@ -475,7 +475,7 @@ public function getHeader($header)
475475

476476
/**
477477
* @param string $header Header name.
478-
* @return string|null Header values.
478+
* @return $this
479479
*/
480480
public function removeHeader($header)
481481
{
@@ -485,7 +485,7 @@ public function removeHeader($header)
485485
}
486486

487487
/**
488-
* @return string[] Recipients emails.
488+
* @inheritDoc
489489
*/
490490
public function getRecipients()
491491
{
@@ -501,7 +501,7 @@ public function getSubject()
501501
}
502502

503503
/**
504-
* @return string Rew string as email headers
504+
* @inheritDoc
505505
*/
506506
public function getHeadersRaw()
507507
{
@@ -540,6 +540,9 @@ public function getBodyRaw()
540540
return $info['data'];
541541
}
542542

543+
/**
544+
* @inheritDoc
545+
*/
543546
public function getPersonalMessages()
544547
{
545548
$messages = array();
@@ -572,9 +575,9 @@ public function serialize()
572575
/**
573576
* @inheritDoc
574577
*/
575-
public function unserialize($serialized)
578+
public function unserialize($data)
576579
{
577-
$raw = unserialize($serialized);
580+
$raw = unserialize($data);
578581
$empty = array(
579582
'id' => array(),
580583
'headers' => array(),
@@ -650,7 +653,7 @@ private function getMainInfo($onlyType)
650653
if (!is_null($this->text) && is_null($this->html)) {
651654
$result['type'] = 'text/plain; charset=UTF-8';
652655
if (!$onlyType) {
653-
$result['data'] = quoted_printable_encode($this->text);
656+
$result['data'] = $this->encodeBody($this->text);
654657
}
655658
return $result;
656659
}
@@ -671,7 +674,7 @@ private function getMainInfo($onlyType)
671674
$text .= 'Content-Type: text/plain; charset=UTF-8' . $eol;
672675
$text .= 'Content-Transfer-Encoding: quoted-printable' . $eol;
673676
$text .= $eol;
674-
$text .= quoted_printable_encode($this->text) . $eol;
677+
$text .= $this->encodeBody($this->text) . $eol;
675678

676679
$html = $eol;
677680
$html .= $this->encodeHeader('Content-Type', $htmlInfo['type']) . $eol;
@@ -698,7 +701,7 @@ private function getHtmlInfo($onlyType)
698701
if (is_null($this->html)) {
699702
return $result;
700703
}
701-
$raw = quoted_printable_encode($this->html);
704+
$raw = $this->encodeBody($this->html);
702705
if (empty($this->contents)) {
703706
$result['type'] = 'text/html; charset=UTF-8';
704707
} else {
@@ -749,16 +752,13 @@ private function randomString()
749752
{
750753
$start = 268435456;
751754
$finish = 4294967295;
752-
$rand = null;
753-
if (function_exists('random_int')) {
754-
try {
755-
/** @noinspection PhpElementIsNotAvailableInCurrentPhpVersionInspection */
756-
$rand = random_int($start, $finish);
757-
} catch (Exception $e) {
758-
$rand = null;
759-
}
755+
if (!function_exists('random_int')) {
756+
return dechex(rand($start, $finish));
760757
}
761-
if (!$rand) {
758+
759+
try {
760+
$rand = random_int($start, $finish);
761+
} catch (Exception $e) {
762762
$rand = rand($start, $finish);
763763
}
764764
return dechex($rand);
@@ -767,14 +767,14 @@ private function randomString()
767767
/**
768768
* @param string $header Header name.
769769
* @param string $value Header values.
770-
* @return self
770+
* @return void
771771
*/
772772
private function setAnyHeader($header, $value)
773773
{
774774
$header = $this->prepareHeaderName($header);
775775
$value = $this->prepareHeaderValue($value);
776776
if (!$header) {
777-
return $this;
777+
return;
778778
}
779779
if ($value) {
780780
$this->headers[$header] = $value;
@@ -783,26 +783,24 @@ private function setAnyHeader($header, $value)
783783
unset($this->headers[$header]);
784784
}
785785
}
786-
return $this;
787786
}
788787

789788
/**
790789
* @param string $header Header name.
791-
* @return string|null Header values.
790+
* @return void
792791
*/
793792
private function removeAnyHeader($header)
794793
{
795794
$header = $this->prepareHeaderName($header);
796795
if (array_key_exists($header, $this->headers)) {
797796
unset($this->headers[$header]);
798797
}
799-
return $this;
800798
}
801799

802800
/**
803801
* @param string $header
804802
* @param bool $removing
805-
* @return bool
803+
* @return void
806804
* @throws HeaderNotModifiedException
807805
*/
808806
private function touchHeader($header, $removing)
@@ -814,7 +812,6 @@ private function touchHeader($header, $removing)
814812
$method = is_array($this->protectedHeaders[$header]) ? $this->protectedHeaders[$header][$key] : null;
815813
throw new HeaderNotModifiedException($header, $method);
816814
}
817-
return true;
818815
}
819816

820817
/**
@@ -829,14 +826,13 @@ private function normalizeHeaderName($name)
829826
if ($name === 'message-id') {
830827
return 'Message-ID';
831828
}
832-
$name = preg_replace_callback(
829+
return preg_replace_callback(
833830
'/(^|-)[a-z]/ui',
834831
function ($match) {
835832
return strtoupper($match[0]);
836833
},
837834
$name
838835
);
839-
return $name;
840836
}
841837

842838
/**
@@ -931,8 +927,7 @@ private function prepareAttachmentName($name)
931927
*/
932928
private function prepareContentId($name)
933929
{
934-
$name = (string)$name;
935-
return $name;
930+
return (string)$name;
936931
}
937932

938933
/**
@@ -1004,6 +999,15 @@ private function encodeHeader($header, $value)
1004999
return $result;
10051000
}
10061001

1002+
/**
1003+
* @param string $data
1004+
* @return string
1005+
*/
1006+
private function encodeBody($data)
1007+
{
1008+
return quoted_printable_encode($data);
1009+
}
1010+
10071011
/**
10081012
* @param string $pathOrContent Path to file or Contents
10091013
* @return string

0 commit comments

Comments
 (0)