Skip to content

Commit 74f7048

Browse files
committed
fix: don't throw exceptions, just log the error (#3703)
1 parent 4841f61 commit 74f7048

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

phpmyfaq/src/phpMyFAQ/Mail.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -273,19 +273,22 @@ private function setEmailTo(array &$target, string $targetAlias, string $address
273273
* @param string $targetAlias Alias Target alias.
274274
* @param string $address User e-mail address.
275275
* @param string|null $name Username (optional).
276-
* @throws Exception
277276
* @return bool True if successful, false otherwise.
278277
*/
279278
private function addEmailTo(array &$target, string $targetAlias, string $address, ?string $name = null): bool
280279
{
281280
// Check
282281
if (!self::validateEmail($address)) {
283-
throw new Exception('"' . $address . '" is not a valid email address!');
282+
$this->configuration->getLogger()->error('"' . $address . '" is not a valid email address!');
283+
return false;
284284
}
285285

286286
// Don't allow duplicated addresses
287287
if (array_key_exists($address, $target)) {
288-
throw new Exception('"' . $address . '" has been already added in ' . $targetAlias . '!');
288+
$this->configuration->getLogger()->error(
289+
'"' . $address . '" has been already added in ' . $targetAlias . '!'
290+
);
291+
return false;
289292
}
290293

291294
if (isset($name)) {
@@ -302,14 +305,15 @@ private function addEmailTo(array &$target, string $targetAlias, string $address
302305

303306
// Add the email address into the target array
304307
$target[$address] = $name;
305-
// On Windows, when using PHP built-in mail drop any name, just use the e-mail address
308+
// On Windows, when using PHP built-in mail drops any name, just use the e-mail address
306309
if ('WIN' !== strtoupper(substr(PHP_OS, 0, 3))) {
307310
return true;
308311
}
309312
if ('built-in' != $this->agent) {
310313
return true;
311314
}
312315
$target[$address] = null;
316+
313317
return true;
314318
}
315319

@@ -330,6 +334,7 @@ public static function validateEmail(string $address): bool
330334
if ($address !== str_replace($unsafe, '', $address)) {
331335
return false;
332336
}
337+
333338
return (bool) filter_var($address, FILTER_VALIDATE_EMAIL);
334339
}
335340

tests/phpMyFAQ/MailTest.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ public function testSetFromWithValidAddress(): void
5454

5555
public function testSetFromWithInvalidAddress(): void
5656
{
57-
$this->expectException(Exception::class);
58-
$this->mail->setFrom('invalid-email');
57+
$this->assertFalse($this->mail->setFrom('invalid-email'));
5958
}
6059

6160
public function testValidateEmailWithValidAddress(): void
@@ -99,8 +98,7 @@ public function testAddCcWithValidAddress(): void
9998

10099
public function testAddCcWithInvalidAddress(): void
101100
{
102-
$this->expectException(Exception::class);
103-
$this->mail->addCc('invalid-email');
101+
$this->assertFalse($this->mail->addCc('invalid-email'));
104102
}
105103

106104
/**
@@ -114,8 +112,7 @@ public function testAddToWithValidAddress(): void
114112

115113
public function testAddToWithInvalidAddress(): void
116114
{
117-
$this->expectException(Exception::class);
118-
$this->mail->addTo('invalid-email');
115+
$this->assertFalse($this->mail->addTo('invalid-email'));
119116
}
120117

121118
public function testGetDateWithValidTimestamp(): void
@@ -205,8 +202,7 @@ public function testSetReplyToWithValidAddress(): void
205202

206203
public function testSetReplyToWithInvalidAddress(): void
207204
{
208-
$this->expectException(Exception::class);
209-
$this->mail->setReplyTo('invalid-email');
205+
$this->assertFalse($this->mail->setReplyTo('invalid-email'));
210206
}
211207

212208
public function testSafeEmailWithSafeEmailEnabled(): void

0 commit comments

Comments
 (0)