1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Genkgo\Mail\Mime; |
5
|
|
|
|
6
|
|
|
use Genkgo\Mail\Header\ContentDisposition; |
7
|
|
|
use Genkgo\Mail\Header\ContentTransferEncoding; |
8
|
|
|
use Genkgo\Mail\Header\ContentType; |
9
|
|
|
use Genkgo\Mail\Header\HeaderValue; |
10
|
|
|
use Genkgo\Mail\HeaderInterface; |
11
|
|
|
use Genkgo\Mail\Stream\Base64EncodedStream; |
12
|
|
|
use Genkgo\Mail\StreamInterface; |
13
|
|
|
|
14
|
|
|
final class FileAttachment implements PartInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var PartInterface |
18
|
|
|
*/ |
19
|
|
|
private $decoratedPart; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param string $filename |
23
|
|
|
* @param ContentType $contentType |
24
|
|
|
* @param string $attachmentName |
25
|
|
|
*/ |
26
|
21 |
|
public function __construct(string $filename, ContentType $contentType, string $attachmentName = '') |
27
|
|
|
{ |
28
|
21 |
|
if (!\file_exists($filename) || !\is_file($filename)) { |
29
|
2 |
|
throw new \InvalidArgumentException('Attachment does not exists'); |
30
|
|
|
} |
31
|
|
|
|
32
|
19 |
|
if ($attachmentName === '') { |
33
|
19 |
|
$attachmentName = \basename($filename); |
34
|
|
|
} |
35
|
|
|
|
36
|
19 |
|
$resource = \fopen($filename, 'r'); |
37
|
19 |
|
if ($resource === false) { |
38
|
|
|
throw new \UnexpectedValueException('Cannot open file ' . $filename . ' for reading'); |
39
|
|
|
} |
40
|
|
|
|
41
|
19 |
|
$this->decoratedPart = (new GenericPart()) |
42
|
19 |
|
->withBody(new Base64EncodedStream($resource)) |
43
|
19 |
|
->withHeader($contentType) |
44
|
19 |
|
->withHeader(new ContentTransferEncoding('base64')) |
45
|
19 |
|
->withHeader(ContentDisposition::newAttachment($attachmentName)); |
46
|
19 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param string $filename |
50
|
|
|
* @param string $attachmentName |
51
|
|
|
* @return FileAttachment |
52
|
|
|
*/ |
53
|
1 |
|
public static function fromUnknownFileType(string $filename, string $attachmentName = ''): FileAttachment |
54
|
|
|
{ |
55
|
1 |
|
$fileInfo = new \finfo(FILEINFO_MIME); |
56
|
1 |
|
$mime = $fileInfo->file($filename); |
57
|
1 |
|
if ($mime === false) { |
58
|
|
|
return new self( |
59
|
|
|
$filename, |
60
|
|
|
new ContentType('application/octet-stream'), |
61
|
|
|
$attachmentName |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
1 |
|
$headerValue = HeaderValue::fromString($mime); |
66
|
|
|
|
67
|
|
|
try { |
68
|
1 |
|
$charset = $headerValue->getParameter('charset')->getValue(); |
69
|
1 |
|
if ($charset === 'binary') { |
70
|
|
|
$contentType = new ContentType($headerValue->getRaw()); |
71
|
|
|
} else { |
72
|
1 |
|
$contentType = new ContentType($headerValue->getRaw(), $charset); |
73
|
|
|
} |
74
|
|
|
} catch (\UnexpectedValueException $e) { |
75
|
|
|
$contentType = new ContentType($headerValue->getRaw()); |
76
|
|
|
} |
77
|
|
|
|
78
|
1 |
|
return new self( |
79
|
1 |
|
$filename, |
80
|
|
|
$contentType, |
81
|
|
|
$attachmentName |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return iterable<HeaderInterface> |
|
|
|
|
87
|
|
|
*/ |
88
|
2 |
|
public function getHeaders(): iterable |
89
|
|
|
{ |
90
|
2 |
|
return $this->decoratedPart->getHeaders(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param string $name |
95
|
|
|
* @return bool |
96
|
|
|
*/ |
97
|
8 |
|
public function hasHeader(string $name): bool |
98
|
|
|
{ |
99
|
8 |
|
return $this->decoratedPart->hasHeader($name); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param string $name |
104
|
|
|
* @return HeaderInterface |
105
|
|
|
*/ |
106
|
9 |
|
public function getHeader(string $name): HeaderInterface |
107
|
|
|
{ |
108
|
9 |
|
return $this->decoratedPart->getHeader($name); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param HeaderInterface $header |
113
|
|
|
* @return PartInterface |
114
|
|
|
*/ |
115
|
4 |
|
public function withHeader(HeaderInterface $header): PartInterface |
116
|
|
|
{ |
117
|
4 |
|
if (\strtolower((string)$header->getName()) === 'content-disposition') { |
118
|
2 |
|
throw new \InvalidArgumentException('Cannot modify content disposition for file attachment'); |
119
|
|
|
} |
120
|
|
|
|
121
|
2 |
|
$clone = clone $this; |
122
|
2 |
|
$clone->decoratedPart = $this->decoratedPart->withHeader($header); |
123
|
2 |
|
return $clone; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param string $name |
128
|
|
|
* @return PartInterface |
129
|
|
|
*/ |
130
|
4 |
|
public function withoutHeader(string $name): PartInterface |
131
|
|
|
{ |
132
|
4 |
|
if (\strtolower($name) === 'content-disposition') { |
133
|
2 |
|
throw new \InvalidArgumentException('Cannot modify content disposition for file attachment'); |
134
|
|
|
} |
135
|
|
|
|
136
|
2 |
|
$clone = clone $this; |
137
|
2 |
|
$clone->decoratedPart = $this->decoratedPart->withoutHeader($name); |
138
|
2 |
|
return $clone; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param StreamInterface $body |
143
|
|
|
* @return PartInterface |
144
|
|
|
*/ |
145
|
2 |
|
public function withBody(StreamInterface $body): PartInterface |
146
|
|
|
{ |
147
|
2 |
|
throw new \RuntimeException('Cannot modify body of FileAttachment'); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @return StreamInterface |
152
|
|
|
*/ |
153
|
8 |
|
public function getBody(): StreamInterface |
154
|
|
|
{ |
155
|
8 |
|
return $this->decoratedPart->getBody(); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.