Skip to content

Commit 9a22031

Browse files
committed
headers encoding
1 parent b6a9f40 commit 9a22031

File tree

3 files changed

+53
-10
lines changed

3 files changed

+53
-10
lines changed

src/Message.php

+30-9
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,15 @@ final class Message
5050

5151
public function __construct($subject = null, $html = null, $plain = null)
5252
{
53+
$this->boundary = dechex(rand(0, 16777215));
5354
$this->setHeader("MIME-Version", "1.0");
55+
$this->setHeaderRaw("Content-Type", "multipart/mixed; boundary=\"r={$this->boundary}\"");
56+
$this->setHeader("Content-Transfer-Encoding", "7bit");
5457
$mailer = sprintf("ddrv/mailer-%s (https://github.com/ddrv/mailer)", Mailer::MAILER_VERSION);
5558
$this->setHeader("X-Mailer", $mailer);
5659
$this->setSubject($subject);
5760
$this->setHtmlBody($html);
5861
$this->setPlainBody($plain);
59-
$this->boundary = dechex(rand(0, 16777215));
6062
}
6163

6264
public function setSubject($subject)
@@ -143,15 +145,30 @@ public function getBcc()
143145
return $this->bcc;
144146
}
145147

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+
146157
public function getRecipients()
147158
{
148159
return array_keys(array_replace($this->to, $this->cc, $this->bcc));
149160
}
150161

151162
public function setHeader($header, $value)
163+
{
164+
$this->setHeaderRaw($header, $this->headerEncode($value));
165+
return $this;
166+
}
167+
168+
private function setHeaderRaw($header, $value)
152169
{
153170
$header = (string)$header;
154-
$value = str_replace("\r\n", "", (string)$value);
171+
//$value = str_replace(array("\r", "\n"), "", (string)$value);
155172
if ($value) {
156173
$this->headers[mb_strtolower($header)] = "$header: $value";
157174
} else {
@@ -226,8 +243,6 @@ public function getSubject()
226243

227244
public function getHeaders()
228245
{
229-
$this->setHeader("Content-Type", "multipart/mixed; boundary=\"r={$this->boundary}\"");
230-
$this->setHeader("Content-Transfer-Encoding", "7bit");
231246
$headers = array_values($this->headers);
232247
return $headers;
233248
}
@@ -359,26 +374,26 @@ private function addAddress($type, $email, $name)
359374

360375
private function replaceHeaderTo()
361376
{
362-
$this->setHeader("To", implode(", ", $this->to));
377+
$this->setHeaderRaw("To", implode(", ", $this->to));
363378
}
364379

365380
private function replaceHeaderCc()
366381
{
367-
$this->setHeader("Cc", implode(", ", $this->cc));
382+
$this->setHeaderRaw("Cc", implode(", ", $this->cc));
368383
}
369384

370385
private function replaceHeaderBcc()
371386
{
372-
$this->setHeader("Bcc", implode(", ", $this->bcc));
387+
$this->setHeaderRaw("Bcc", implode(", ", $this->bcc));
373388
}
374389

375390
private function getContact($email, $name = "")
376391
{
377392
$email = (string)$email;
378393
$name = preg_replace("/[^\pL\s\,\.\d]/ui", "", (string)$name);
379394
$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 ";
382397
return "$name<$email>";
383398
}
384399

@@ -414,4 +429,10 @@ public function unserialize($serialized)
414429
$this->$key = array_key_exists($key, $raw) ? $raw[$key] : $default;
415430
}
416431
}
432+
433+
private function headerEncode($value)
434+
{
435+
$value = mb_encode_mimeheader($value, "UTF-8", "B", "\r\n", 0);
436+
return $value;
437+
}
417438
}

src/Message/MessagePart.php

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Ddrv\Mailer\Message;
4+
5+
final class MessagePart
6+
{
7+
8+
private $content;
9+
10+
private $mime;
11+
12+
public function __construct($content, $mimeType)
13+
{
14+
$this->content = $content;
15+
$this->mime = $mimeType;
16+
}
17+
18+
public function getRaw()
19+
{
20+
21+
}
22+
}

tests/Support/Factory/MessageFactory.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class MessageFactory
1818

1919
public function __construct()
2020
{
21-
$this->faker = Factory::create();
21+
$this->faker = Factory::create("ru_RU");
2222
$this->from = sprintf("PHPUnit Test <%s+phpunit@localhost>", get_current_user());
2323
}
2424

0 commit comments

Comments
 (0)