Skip to content

mail: fix exit code handling of sendmail cmd and add logging #18786

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 31 additions & 5 deletions ext/standard/mail.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
#include "ext/date/php_date.h"
#include "zend_smart_str.h"

#ifdef HAVE_SYS_WAIT_H
#include <sys/wait.h>
#endif

#ifdef HAVE_SYSEXITS_H
# include <sysexits.h>
#endif
Expand Down Expand Up @@ -562,6 +566,7 @@ PHPAPI bool php_mail(const char *to, const char *subject, const char *message, c
}

if (sendmail) {
int ret;
#ifndef PHP_WIN32
if (EACCES == errno) {
php_error_docref(NULL, E_WARNING, "Permission denied: unable to execute shell to run mail delivery binary '%s'", sendmail_path);
Expand All @@ -582,26 +587,47 @@ PHPAPI bool php_mail(const char *to, const char *subject, const char *message, c
fprintf(sendmail, "%s%s", hdr, line_sep);
}
fprintf(sendmail, "%s%s%s", line_sep, message, line_sep);
int ret = pclose(sendmail);
#ifdef PHP_WIN32
ret = pclose(sendmail);

#if PHP_SIGCHILD
if (sig_handler) {
signal(SIGCHLD, sig_handler);
}
#endif

#ifdef PHP_WIN32
if (ret == -1)
#else
int wstatus = pclose(sendmail);
#if PHP_SIGCHILD
if (sig_handler) {
signal(SIGCHLD, sig_handler);
}
#endif
/* Determine the wait(2) exit status */
if (wstatus == -1) {
php_error_docref(NULL, E_WARNING, "Sendmail pclose failed %d (%s)", errno, strerror(errno));
MAIL_RET(false);
} else if (WIFSIGNALED(wstatus)) {
php_error_docref(NULL, E_WARNING, "Sendmail killed by signal %d (%s)", WTERMSIG(wstatus), strsignal(WTERMSIG(wstatus)));
MAIL_RET(false);
} else {
if (WIFEXITED(wstatus)) {
ret = WEXITSTATUS(wstatus);
} else {
php_error_docref(NULL, E_WARNING, "Sendmail did not exit");
MAIL_RET(false);
}
}
#endif

#if defined(EX_TEMPFAIL)
if ((ret != EX_OK)&&(ret != EX_TEMPFAIL))
#elif defined(EX_OK)
if (ret != EX_OK)
#else
if (ret != 0)
#endif
#endif
{
php_error_docref(NULL, E_WARNING, "Sendmail exited with non-zero exit code %d", ret);
MAIL_RET(false);
} else {
MAIL_RET(true);
Expand Down
3 changes: 2 additions & 1 deletion ext/standard/tests/mail/gh10990.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ $from = '[email protected]';
$headers = ['From' => &$from];
var_dump(mail('[email protected]', 'Test', 'Test', $headers));
?>
--EXPECT--
--EXPECTF--
Warning: mail(): Sendmail exited with non-zero exit code 127 in %sgh10990.php on line %d
bool(false)
4 changes: 3 additions & 1 deletion ext/standard/tests/mail/mail_basic5.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ $message = 'A Message';
echo "-- failure --\n";
var_dump( mail($to, $subject, $message) );
?>
--EXPECT--
--EXPECTF--
*** Testing mail() : basic functionality ***
-- failure --

Warning: mail(): Sendmail exited with non-zero exit code 1 in %smail_basic5.php on line %d
bool(false)
4 changes: 3 additions & 1 deletion ext/standard/tests/mail/mail_variation1.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ $subject = 'Test Subject';
$message = 'A Message';
var_dump( mail($to, $subject, $message) );
?>
--EXPECT--
--EXPECTF--
*** Testing mail() : variation ***

Warning: mail(): Sendmail exited with non-zero exit code 127 in %smail_variation1.php on line %d
bool(false)
22 changes: 22 additions & 0 deletions ext/standard/tests/mail/mail_variation3.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
--TEST--
Test mail() function : variation sendmail temp fail
--INI--
sendmail_path=exit 75
--SKIPIF--
<?php
if (substr(PHP_OS, 0, 3) == "WIN")
die("skip Won't run on Windows");
?>
--FILE--
<?php
echo "*** Testing mail() : variation ***\n";

// Initialise all required variables
$to = '[email protected]';
$subject = 'Test Subject';
$message = 'A Message';
var_dump(mail($to, $subject, $message));
?>
--EXPECT--
*** Testing mail() : variation ***
bool(true)
24 changes: 24 additions & 0 deletions ext/standard/tests/mail/mail_variation4.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
--TEST--
Test mail() function : variation sigterm
--INI--
sendmail_path="kill \$\$"
--SKIPIF--
<?php
if (substr(PHP_OS, 0, 3) == "WIN")
die("skip Won't run on Windows");
?>
--FILE--
<?php
echo "*** Testing mail() : variation ***\n";

// Initialise all required variables
$to = '[email protected]';
$subject = 'Test Subject';
$message = 'A Message';
var_dump(mail($to, $subject, $message));
?>
--EXPECTF--
*** Testing mail() : variation ***

Warning: mail(): Sendmail killed by signal %d (%s) in %smail_variation4.php on line %d
bool(false)
24 changes: 24 additions & 0 deletions ext/standard/tests/mail/mail_variation5.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
--TEST--
Test mail() function : variation non-zero exit
--INI--
sendmail_path="exit 123"
--SKIPIF--
<?php
if (substr(PHP_OS, 0, 3) == "WIN")
die("skip Won't run on Windows");
?>
--FILE--
<?php
echo "*** Testing mail() : variation ***\n";

// Initialise all required variables
$to = '[email protected]';
$subject = 'Test Subject';
$message = 'A Message';
var_dump(mail($to, $subject, $message));
?>
--EXPECTF--
*** Testing mail() : variation ***

Warning: mail(): Sendmail exited with non-zero exit code 123 in %smail_variation5.php on line %d
bool(false)