Skip to content

Commit 478edcd

Browse files
committed
Fix GH-7875: mails are sent even if failure to log throws exception
We explicitly check for an exception after the logging attempt, and bail out in that case. Co-authored-by: Tim Düsterhus <timwolla@googlemail.com> Closes GH-7878.
1 parent ee8f9d7 commit 478edcd

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ PHP NEWS
1616
- Sockets:
1717
. Fixed ext/sockets build on Haiku. (David Carlier)
1818

19+
- Standard:
20+
. Fixed bug GH-7875 (mails are sent even if failure to log throws exception).
21+
(cmb)
22+
1923
20 Jan 2022, PHP 8.0.15
2024

2125
- Core:

ext/standard/mail.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,10 @@ PHPAPI int php_mail(const char *to, const char *subject, const char *message, co
473473
efree(logline);
474474
}
475475

476+
if (EG(exception)) {
477+
MAIL_RET(0);
478+
}
479+
476480
if (PG(mail_x_header)) {
477481
const char *tmp = zend_get_executed_filename();
478482
zend_string *f;

ext/standard/tests/mail/gh7875.phpt

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
--TEST--
2+
GH-7875 (mails are sent even if failure to log throws exception)
3+
--INI--
4+
sendmail_path={MAIL:{PWD}/gh7875.mail.out}
5+
mail.log={PWD}/gh7875.mail.log
6+
--FILE--
7+
<?php
8+
function exception_error_handler($severity, $message, $file, $line) {
9+
if (!(error_reporting() & $severity)) {
10+
return;
11+
}
12+
throw new ErrorException($message, 0, $severity, $file, $line);
13+
}
14+
set_error_handler("exception_error_handler");
15+
16+
touch(__DIR__ . "/gh7875.mail.log");
17+
chmod(__DIR__ . "/gh7875.mail.log", 0444);
18+
19+
try {
20+
mail('recipient@example.com', 'Subject', 'Body', []);
21+
echo 'Not Reached';
22+
} catch (\Exception $e) {
23+
echo $e->getMessage(), PHP_EOL;
24+
var_dump(file_exists(__DIR__ . "/gh7875.mail.out"));
25+
}
26+
?>
27+
--CLEAN--
28+
<?php
29+
@chmod(__DIR__ . "/gh7875.mail.log", 0644);
30+
@unlink(__DIR__ . "/gh7875.mail.log");
31+
@unlink(__DIR__ . "/gh7875.mail.out");
32+
?>
33+
--EXPECTF--
34+
mail(%s): Failed to open stream: Permission denied
35+
bool(false)

0 commit comments

Comments
 (0)