1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PhpEmail\Attachment; |
6
|
|
|
|
7
|
|
|
use PhpEmail\Attachment; |
8
|
|
|
use PhpEmail\Validate; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Attach a file by URL. This requires that the `allow_url_fopen` be enabled. |
12
|
|
|
*/ |
13
|
|
|
class UrlAttachment extends AttachmentWithHeaders |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
private $url; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string|null |
22
|
|
|
*/ |
23
|
|
|
private $content = null; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param string $url |
27
|
|
|
* @param null|string $name If null, the class will determine a name for the attachment based on the URL. |
28
|
|
|
* @param null|string $contentId |
29
|
|
|
* @param null|string $contentType |
30
|
|
|
* @param null|string $charset |
31
|
|
|
*/ |
32
|
2 |
|
public function __construct( |
33
|
|
|
string $url, |
34
|
|
|
?string $name = null, |
35
|
|
|
?string $contentId = null, |
36
|
|
|
?string $contentType = null, |
37
|
|
|
string $charset = null |
38
|
|
|
) { |
39
|
2 |
|
Validate::that() |
40
|
2 |
|
->isUrl('url', $url) |
41
|
2 |
|
->now(); |
42
|
|
|
|
43
|
2 |
|
$this->url = $url; |
44
|
2 |
|
$this->name = $name ?: urldecode(basename(parse_url($url, PHP_URL_PATH))); |
45
|
2 |
|
$this->contentId = $contentId; |
46
|
2 |
|
$this->contentType = $contentType; |
47
|
2 |
|
$this->charset = $charset; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* A static constructor for the UrlAttachment constructor. |
52
|
|
|
* |
53
|
|
|
* @param string $url |
54
|
|
|
* @param null|string $name If null, the class will determine a name for the attachment based on the URL. |
55
|
|
|
* @param null|string $contentId |
56
|
|
|
* @param null|string $contentType |
57
|
|
|
* @param null|string $charset |
58
|
|
|
* |
59
|
|
|
* @return UrlAttachment |
60
|
|
|
*/ |
61
|
1 |
|
public static function fromUrl( |
62
|
|
|
string $url, |
63
|
|
|
?string $name = null, |
64
|
|
|
?string $contentId = null, |
65
|
|
|
?string $contentType = null, |
66
|
|
|
string $charset = null |
67
|
|
|
): UrlAttachment { |
68
|
1 |
|
return new self($url, $name, $contentId, $contentType, $charset); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
2 |
|
public function getUrl(): string |
75
|
|
|
{ |
76
|
2 |
|
return $this->url; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* {@inheritdoc} |
81
|
|
|
*/ |
82
|
2 |
|
public function getContent(): string |
83
|
|
|
{ |
84
|
2 |
|
if ($this->content === null) { |
85
|
2 |
|
$this->content = file_get_contents($this->url); |
86
|
|
|
} |
87
|
|
|
|
88
|
2 |
|
return $this->content; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* {@inheritdoc} |
93
|
|
|
*/ |
94
|
2 |
|
public function getBase64Content(): string |
95
|
|
|
{ |
96
|
2 |
|
return base64_encode($this->getContent()); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
2 |
|
public function __toString(): string |
103
|
|
|
{ |
104
|
2 |
|
return json_encode([ |
105
|
2 |
|
'url' => $this->url, |
106
|
2 |
|
'name' => $this->name, |
107
|
2 |
|
'contentId' => $this->contentId, |
108
|
|
|
]); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* {@inheritdoc} |
113
|
|
|
*/ |
114
|
1 |
|
protected function determineContentType(): string |
115
|
|
|
{ |
116
|
1 |
|
$contentType = get_headers($this->url, 1)['Content-Type'] ?? null; |
117
|
|
|
|
118
|
1 |
|
if (!$contentType) { |
119
|
|
|
return 'application/octet-stream'; |
120
|
|
|
} else { |
121
|
1 |
|
return explode(';', $contentType)[0]; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|