1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Notifier\Channel\Email; |
6
|
|
|
|
7
|
|
|
class EmailMessage |
8
|
|
|
{ |
9
|
|
|
/** @var array array */ |
10
|
|
|
protected $headers = []; |
11
|
|
|
|
12
|
|
|
/** @var string */ |
13
|
|
|
protected $subject = ''; |
14
|
|
|
|
15
|
|
|
/** @var string */ |
16
|
|
|
protected $body = ''; |
17
|
|
|
|
18
|
1 |
|
public function getHeaders(): array |
19
|
|
|
{ |
20
|
1 |
|
return $this->headers; |
21
|
|
|
} |
22
|
|
|
|
23
|
5 |
|
public function from(string $email, string $name = null) |
24
|
|
|
{ |
25
|
5 |
|
$this->headers['From'][] = $this->createAddress($email, $name); |
26
|
|
|
|
27
|
5 |
|
return $this; |
28
|
|
|
} |
29
|
|
|
|
30
|
1 |
|
public function getFrom(): array |
31
|
|
|
{ |
32
|
1 |
|
return $this->headers['From'] ?? []; |
33
|
|
|
} |
34
|
|
|
|
35
|
1 |
|
public function sender(string $email, string $name = null) |
36
|
|
|
{ |
37
|
1 |
|
$this->headers['Sender'] = $this->createAddress($email, $name); |
38
|
|
|
|
39
|
1 |
|
return $this; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function getSender(): string |
43
|
|
|
{ |
44
|
|
|
return $this->headers['Sender'] ?? ''; |
45
|
|
|
} |
46
|
|
|
|
47
|
1 |
|
public function replyTo(string $email, string $name = null) |
48
|
|
|
{ |
49
|
1 |
|
$this->headers['Reply-To'][] = $this->createAddress($email, $name); |
50
|
|
|
|
51
|
1 |
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function getReplyTo(): array |
55
|
|
|
{ |
56
|
|
|
return $this->headers['Reply-To'] ?? []; |
57
|
|
|
} |
58
|
|
|
|
59
|
4 |
|
public function to(string $email, string $name = null) |
60
|
|
|
{ |
61
|
4 |
|
$this->headers['To'][] = $this->createAddress($email, $name); |
62
|
|
|
|
63
|
4 |
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
4 |
|
public function getTo(): array |
67
|
|
|
{ |
68
|
4 |
|
return $this->headers['To'] ?? []; |
69
|
|
|
} |
70
|
|
|
|
71
|
1 |
|
public function cc(string $email, string $name = null) |
72
|
|
|
{ |
73
|
1 |
|
$this->headers['Cc'][] = $this->createAddress($email, $name); |
74
|
|
|
|
75
|
1 |
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function getCc(): array |
79
|
|
|
{ |
80
|
|
|
return $this->headers['Cc'] ?? []; |
81
|
|
|
} |
82
|
|
|
|
83
|
1 |
|
public function bcc(string $email, string $name = null) |
84
|
|
|
{ |
85
|
1 |
|
$this->headers['Bcc'][] = $this->createAddress($email, $name); |
86
|
|
|
|
87
|
1 |
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function getBcc(): array |
91
|
|
|
{ |
92
|
|
|
return $this->headers['Bcc'] ?? []; |
93
|
|
|
} |
94
|
|
|
|
95
|
5 |
|
public function subject(string $subject) |
96
|
|
|
{ |
97
|
5 |
|
$this->subject = $subject; |
98
|
|
|
|
99
|
5 |
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
1 |
|
public function getSubject(): string |
103
|
|
|
{ |
104
|
1 |
|
return $this->subject; |
105
|
|
|
} |
106
|
|
|
|
107
|
1 |
|
public function textBody(string $textBody) |
108
|
|
|
{ |
109
|
1 |
|
$this->body = $textBody; |
110
|
|
|
|
111
|
1 |
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
4 |
|
public function htmlBody(string $htmlBody) |
115
|
|
|
{ |
116
|
4 |
|
$this->body = $htmlBody; |
117
|
4 |
|
$this->headers['MIME-Version'] = '1.0'; |
118
|
4 |
|
$this->headers['Content-type'] = 'text/html; charset=utf-8'; |
119
|
|
|
|
120
|
4 |
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
2 |
|
public function getBody(): string |
124
|
|
|
{ |
125
|
2 |
|
return $this->body; |
126
|
|
|
} |
127
|
|
|
|
128
|
5 |
|
private function createAddress(string $email, ?string $name): string |
129
|
|
|
{ |
130
|
5 |
|
return (null !== $name) ? $name . ' <' . $email . '>' : $email; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|