Skip to content
This repository was archived by the owner on Aug 16, 2022. It is now read-only.

Commit 145e3ab

Browse files
authored
Default attachment charset to null (#21)
* Default attachment charset to null Binary attachments should not contain a character set at all, or the mail client runs the risk of misinterpreting the data. For this reason, it probably safest for this library to assume there is no predefined character set and only add it if explicitly asked to. * Apply fixes from StyleCI
1 parent 59657ad commit 145e3ab

File tree

10 files changed

+66
-25
lines changed

10 files changed

+66
-25
lines changed

CHANGELOG.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
66
and this project attempts to follow [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
77

8+
## Unreleased Changes
9+
10+
### Added
11+
12+
* Created helper function on `Attachment` to get the RFC content type header.
13+
14+
### Changed
15+
16+
* Allowed null charset on attachments and changed the default to null.
17+
818
## 0.5.0 - 2018-02-25
919

1020
### Added
@@ -15,7 +25,7 @@ and this project attempts to follow [Semantic Versioning](http://semver.org/spec
1525
* Add embedded (inline) attachments to emails.
1626
* Added fluent `addText` and `addHtml` methods on `SimpleContent`
1727

18-
### Change
28+
### Changed
1929

2030
* Now using `Message` objects instead of string for `SimpleContent`'s text and HTML properties.
2131

src/Attachment.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ public function setContentType(string $contentType): Attachment;
4040
/**
4141
* Get the character set defined on the attachment.
4242
*
43-
* @return string
43+
* @return string|null
4444
*/
45-
public function getCharset(): string;
45+
public function getCharset(): ?string;
4646

4747
/**
4848
* Set the character set for the attachment.
@@ -85,6 +85,16 @@ public function getName(): string;
8585
*/
8686
public function setName(string $name): Attachment;
8787

88+
/**
89+
* Get a common representation of the RFC 822 formatted Content Type headers, including:
90+
* - content type
91+
* - name
92+
* - character set.
93+
*
94+
* @return string
95+
*/
96+
public function getRfc2822ContentType(): string;
97+
8898
/**
8999
* Must be implemented to support comparison.
90100
*

src/Attachment/AttachmentWithHeaders.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,13 @@
88

99
abstract class AttachmentWithHeaders implements Attachment
1010
{
11-
public const DEFAULT_CHARSET = 'utf-8';
12-
1311
/**
1412
* @var string|null
1513
*/
1614
protected $contentType;
1715

1816
/**
19-
* @var string
17+
* @var string|null
2018
*/
2119
protected $charset;
2220

@@ -64,7 +62,7 @@ public function setContentType(string $contentType): Attachment
6462
/**
6563
* {@inheritdoc}
6664
*/
67-
public function getCharset(): string
65+
public function getCharset(): ?string
6866
{
6967
return $this->charset;
7068
}
@@ -114,4 +112,21 @@ public function setName(string $name): Attachment
114112

115113
return $this;
116114
}
115+
116+
/**
117+
* {@inheritdoc}
118+
*/
119+
public function getRfc2822ContentType(): string
120+
{
121+
$parts = [
122+
$this->getContentType(),
123+
sprintf('name="%s"', $this->getName()),
124+
];
125+
126+
if ($this->getCharset() !== null) {
127+
$parts[] = sprintf('charset="%s"', $this->getCharset());
128+
}
129+
130+
return implode('; ', $parts);
131+
}
117132
}

src/Attachment/FileAttachment.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ class FileAttachment extends AttachmentWithHeaders
2424
* @param null|string $name If null, the class will determine a name for the attachment based on the file path.
2525
* @param null|string $contentId
2626
* @param null|string $contentType
27-
* @param string $charset
27+
* @param null|string $charset
2828
*/
2929
public function __construct(
3030
string $file,
3131
?string $name = null,
3232
?string $contentId = null,
3333
?string $contentType = null,
34-
string $charset = self::DEFAULT_CHARSET
34+
string $charset = null
3535
) {
3636
Validate::that()
3737
->isFile('file', $file)
@@ -51,7 +51,7 @@ public function __construct(
5151
* @param null|string $name If null, the class will determine a name for the attachment based on the file path.
5252
* @param null|string $contentId
5353
* @param null|string $contentType
54-
* @param string $charset
54+
* @param null|string $charset
5555
*
5656
* @return FileAttachment
5757
*/
@@ -60,7 +60,7 @@ public static function fromFile(
6060
?string $name = null,
6161
?string $contentId = null,
6262
?string $contentType = null,
63-
string $charset = self::DEFAULT_CHARSET
63+
string $charset = null
6464
): FileAttachment {
6565
return new self($file, $name, $contentId, $contentType, $charset);
6666
}

src/Attachment/ResourceAttachment.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ class ResourceAttachment extends AttachmentWithHeaders
2424
* @param null|string $name If null, the class will determine a name for the attachment based on the resource.
2525
* @param null|string $contentId
2626
* @param null|string $contentType
27-
* @param string $charset
27+
* @param null|string $charset
2828
*/
2929
public function __construct(
3030
$resource,
3131
?string $name = null,
3232
?string $contentId = null,
3333
?string $contentType = null,
34-
string $charset = self::DEFAULT_CHARSET
34+
string $charset = null
3535
) {
3636
Validate::that()
3737
->isStream('resource', $resource)
@@ -51,7 +51,7 @@ public function __construct(
5151
* @param null|string $name If null, the class will determine a name for the attachment based on the resource.
5252
* @param null|string $contentId
5353
* @param null|string $contentType
54-
* @param string $charset
54+
* @param null|string $charset
5555
*
5656
* @return ResourceAttachment
5757
*/
@@ -60,7 +60,7 @@ public static function fromResource(
6060
?string $name = null,
6161
?string $contentId = null,
6262
?string $contentType = null,
63-
string $charset = self::DEFAULT_CHARSET
63+
string $charset = null
6464
): ResourceAttachment {
6565
return new self($resource, $name, $contentId, $contentType, $charset);
6666
}

src/Attachment/UrlAttachment.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ class UrlAttachment extends AttachmentWithHeaders
2727
* @param null|string $name If null, the class will determine a name for the attachment based on the URL.
2828
* @param null|string $contentId
2929
* @param null|string $contentType
30-
* @param string $charset
30+
* @param null|string $charset
3131
*/
3232
public function __construct(
3333
string $url,
3434
?string $name = null,
3535
?string $contentId = null,
3636
?string $contentType = null,
37-
string $charset = self::DEFAULT_CHARSET
37+
string $charset = null
3838
) {
3939
Validate::that()
4040
->isUrl('url', $url)
@@ -54,7 +54,7 @@ public function __construct(
5454
* @param null|string $name If null, the class will determine a name for the attachment based on the URL.
5555
* @param null|string $contentId
5656
* @param null|string $contentType
57-
* @param string $charset
57+
* @param null|string $charset
5858
*
5959
* @return UrlAttachment
6060
*/
@@ -63,7 +63,7 @@ public static function fromUrl(
6363
?string $name = null,
6464
?string $contentId = null,
6565
?string $contentType = null,
66-
string $charset = self::DEFAULT_CHARSET
66+
string $charset = null
6767
): UrlAttachment {
6868
return new self($url, $name, $contentId, $contentType, $charset);
6969
}

src/Email.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ public function addEmbedded(Attachment ...$attachments): self
365365
}
366366

367367
/**
368-
* @return array
368+
* @return Attachment[]
369369
*/
370370
public function getEmbedded(): array
371371
{

tests/Attachment/FileAttachmentTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,13 @@ public function testHandlesLocalFile()
4040
$attachment = new FileAttachment(self::$file);
4141

4242
self::assertEquals('text/plain', $attachment->getContentType());
43-
self::assertEquals('utf-8', $attachment->getCharset());
43+
self::assertEquals(null, $attachment->getCharset());
4444
self::assertEquals(null, $attachment->getContentId());
4545
self::assertEquals('QXR0YWNobWVudCBmaWxl', $attachment->getBase64Content());
4646
self::assertEquals('Attachment file', $attachment->getContent());
4747
self::assertEquals('attachment_test.txt', $attachment->getName());
4848
self::assertEquals('/tmp/attachment_test.txt', $attachment->getFile());
49+
self::assertEquals('text/plain; name="attachment_test.txt"', $attachment->getRfc2822ContentType());
4950
self::assertEquals(
5051
'{"file":"\/tmp\/attachment_test.txt","name":"attachment_test.txt","contentId":null}',
5152
$attachment->__toString()
@@ -55,7 +56,7 @@ public function testHandlesLocalFile()
5556
/**
5657
* @testdox It should create an attachment using a file on the disk and set its headers.
5758
*/
58-
public function handlesLocalFileWithHeaders()
59+
public function testHandlesLocalFileWithHeaders()
5960
{
6061
$attachment = FileAttachment::fromFile(self::$file)
6162
->setContentType('text/json')
@@ -70,6 +71,7 @@ public function handlesLocalFileWithHeaders()
7071
self::assertEquals('Attachment file', $attachment->getContent());
7172
self::assertEquals('testfile.txt', $attachment->getName());
7273
self::assertEquals('/tmp/attachment_test.txt', $attachment->getFile());
74+
self::assertEquals('text/json; name="testfile.txt"; charset="utf-16"', $attachment->getRfc2822ContentType());
7375
self::assertEquals(
7476
'{"file":"\/tmp\/attachment_test.txt","name":"testfile.txt","contentId":"testid"}',
7577
$attachment->__toString()

tests/Attachment/ResourceAttachmentTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,13 @@ public function testHandlesLocalFile()
5959
$attachment = new ResourceAttachment($this->resource);
6060

6161
self::assertEquals('text/plain', $attachment->getContentType());
62-
self::assertEquals('utf-8', $attachment->getCharset());
62+
self::assertEquals(null, $attachment->getCharset());
6363
self::assertEquals(null, $attachment->getContentId());
6464
self::assertEquals('QXR0YWNobWVudCBmaWxl', $attachment->getBase64Content());
6565
self::assertEquals('Attachment file', $attachment->getContent());
6666
self::assertEquals('attachment test.txt', $attachment->getName());
6767
self::assertEquals($this->resource, $attachment->getResource());
68+
self::assertEquals('text/plain; name="attachment test.txt"', $attachment->getRfc2822ContentType());
6869
self::assertEquals(
6970
'{"uri":"\/tmp\/attachment test.txt","name":"attachment test.txt","contentId":null}',
7071
$attachment->__toString()
@@ -91,6 +92,7 @@ public function handlesLocalFileWithHeaders()
9192
self::assertEquals('Attachment file', $attachment->getContent());
9293
self::assertEquals('testfile.txt', $attachment->getName());
9394
self::assertEquals($this->resource, $attachment->getResource());
95+
self::assertEquals('text/json; name="testfile.txt"; charset="utf-16"', $attachment->getRfc2822ContentType());
9496
self::assertEquals(
9597
'{"uri":"\/tmp\/attachment test.txt","name":"testfile.txt","contentId":"testid"}',
9698
$attachment->__toString()
@@ -108,7 +110,7 @@ public function testHandlesRemoteFile()
108110
$attachment = new ResourceAttachment($this->resource);
109111

110112
self::assertEquals('text/plain', $attachment->getContentType());
111-
self::assertEquals('utf-8', $attachment->getCharset());
113+
self::assertEquals(null, $attachment->getCharset());
112114
self::assertEquals(null, $attachment->getContentId());
113115
self::assertEquals('QXR0YWNobWVudCBmaWxl', $attachment->getBase64Content());
114116
self::assertEquals('Attachment file', $attachment->getContent());

tests/Attachment/UrlAttachmentTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,13 @@ public function testHandlesRemoteFileWithDefaults()
5959
$attachment = new UrlAttachment('http://localhost:8777/test%20test.txt?withquery=1');
6060

6161
self::assertEquals('text/plain', $attachment->getContentType());
62-
self::assertEquals('utf-8', $attachment->getCharset());
62+
self::assertEquals(null, $attachment->getCharset());
6363
self::assertEquals(null, $attachment->getContentId());
6464
self::assertEquals('QXR0YWNobWVudCBmaWxl', $attachment->getBase64Content());
6565
self::assertEquals('Attachment file', $attachment->getContent());
6666
self::assertEquals('test test.txt', $attachment->getName());
6767
self::assertEquals('http://localhost:8777/test%20test.txt?withquery=1', $attachment->getUrl());
68+
self::assertEquals('text/plain; name="test test.txt"', $attachment->getRfc2822ContentType());
6869
self::assertEquals(
6970
'{"url":"http:\/\/localhost:8777\/test%20test.txt?withquery=1","name":"test test.txt","contentId":null}',
7071
$attachment->__toString()
@@ -89,6 +90,7 @@ public function testHandlesRemoteFileWithHeaders()
8990
self::assertEquals('Attachment file', $attachment->getContent());
9091
self::assertEquals('testfile.txt', $attachment->getName());
9192
self::assertEquals('http://localhost:8777/test%20test.txt?withquery=1', $attachment->getUrl());
93+
self::assertEquals('text/json; name="testfile.txt"; charset="utf-16"', $attachment->getRfc2822ContentType());
9294
self::assertEquals(
9395
'{"url":"http:\/\/localhost:8777\/test%20test.txt?withquery=1","name":"testfile.txt","contentId":"testid"}',
9496
$attachment->__toString()

0 commit comments

Comments
 (0)