@@ -141,15 +141,15 @@ public function getSender(): array
141
141
*/
142
142
public function getTo (): array
143
143
{
144
- return \array_map ([ $ this , ' parseContact ' ], $ this ->getHeader ('To ' ));
144
+ return $ this -> normalizeAddressList ( $ this ->getHeader ('To ' ));
145
145
}
146
146
147
147
/**
148
148
* @return Contact[]
149
149
*/
150
150
public function getCc (): array
151
151
{
152
- return \array_map ([ $ this , ' parseContact ' ], $ this ->getHeader ('Cc ' ));
152
+ return $ this -> normalizeAddressList ( $ this ->getHeader ('Cc ' ));
153
153
}
154
154
155
155
/**
@@ -160,15 +160,15 @@ public function getCc(): array
160
160
*/
161
161
public function getBcc (): array
162
162
{
163
- return \array_map ([ $ this , ' parseContact ' ], $ this ->protocol ['BCC ' ] ?? []);
163
+ return $ this -> normalizeAddressList ( $ this ->protocol ['BCC ' ] ?? []);
164
164
}
165
165
166
166
/**
167
167
* @return Contact[]
168
168
*/
169
169
public function getReplyTo (): array
170
170
{
171
- return \array_map ([ $ this , ' parseContact ' ], $ this ->getHeader ('Reply-To ' ));
171
+ return $ this -> normalizeAddressList ( $ this ->getHeader ('Reply-To ' ));
172
172
}
173
173
174
174
public function getSubject (): string
@@ -189,10 +189,43 @@ public function getMessage(MessageFormat $type): ?Field
189
189
190
190
private function parseContact (string $ line ): Contact
191
191
{
192
- if (\preg_match ('/^\s*(?<name>.*)\s*<(?<email>.*)>\s*$/ ' , $ line , $ matches ) === 1 ) {
193
- return new Contact ($ matches ['name ' ] ?: null , $ matches ['email ' ] ?: null );
192
+ if (\preg_match ('/^\s*+(?<name>.*?)\s*<(?<email>.*)>\s*$/ ' , $ line , $ matches ) === 1 ) {
193
+ $ name = match (true ) {
194
+ \preg_match ('/^".*?"$/ ' , $ matches ['name ' ]) === 1 => \str_replace ('\\" ' , '" ' , \substr ($ matches ['name ' ], 1 , -1 )),
195
+ $ matches ['name ' ] === '' => null ,
196
+ default => $ matches ['name ' ],
197
+ };
198
+
199
+ return new Contact (
200
+ $ name ,
201
+ $ matches ['email ' ] === '' ? null : \trim ($ matches ['email ' ]),
202
+ );
194
203
}
195
204
196
205
return new Contact (null , $ line );
197
206
}
207
+
208
+ /**
209
+ * @return array<Contact>
210
+ */
211
+ private function parseDestinationAddress (string $ line ): array
212
+ {
213
+ // if this is a group recipient
214
+ if (\preg_match ('/^[^"]+:(.*);$/ ' , $ line , $ matches ) === 1 ) {
215
+ $ line = $ matches [1 ];
216
+ }
217
+
218
+ $ emailList = \array_map ('trim ' , \explode (', ' , $ line ));
219
+ return \array_map ([$ this , 'parseContact ' ], $ emailList );
220
+ }
221
+
222
+ /**
223
+ * @return array<Contact>
224
+ */
225
+ private function normalizeAddressList (array $ param ): array
226
+ {
227
+ return \array_merge (
228
+ ...\array_map ([$ this , 'parseDestinationAddress ' ], $ param ),
229
+ );
230
+ }
198
231
}
0 commit comments