PHP: mail() function with runtime ini_set() for SMTP and SMTP_PORT not working on Linux
The behavior of PHP's mail() function differs fundamentally between Windows environments like WAMP and Linux servers, primarily due to how each platform handles the underlying Mail Transfer Agent (MTA). On Windows, the path is often "No Value" in phpinfo, meaning PHP relies on a local MTA or simply passes arguments to a command that might not strictly require one. However, on a dedicated Linux server, the default configuration sets sendmail_path to /usr/sbin/sendmail -t -i. This command expects specific arguments that do not align with the way ini_set('SMTP', ...) and ini_set('smtp_port', ...) function in modern PHP versions.
When you run ini_set('SMTP', 'myserver') and ini_set('smtp_port', 25), you are instructing PHP to use a specific SMTP command for sending mail. This approach was more common in older PHP versions (pre-5.5). In those versions, if the sendmail_path is configured, PHP would attempt to spawn the sendmail binary and pass the SMTP parameters as arguments. On Linux, this often fails because /usr/sbin/sendmail does not accept -s smtp://myserver:25 as a direct argument in the same way; it expects an envelope sender or recipient list based on standard Unix mail conventions.
Consequently, even though mail() returns true, the email is never delivered. The function successfully hands off the message to the system's MTA (sendmail), but that MTA cannot interpret the SMTP host and port settings you provided via ini_set. It attempts to send the message using its default local queue configuration, which likely lacks the necessary permissions or network access to relay your specific messages without an explicit SASL authentication or a proper virtual mail domain setup.
The most reliable solution is to bypass the legacy sendmail wrapper entirely and use PHP's built-in SMTP extension directly. This requires changing your approach from relying on configuration variables to explicitly instantiating an SMTP client. This method works identically on Windows and Linux because it doesn't depend on the system's default sendmail_path.
To implement this, you need to enable the phpmailer library or use the native PHPMailer class via Composer. Here is a robust pattern using PHPMailer that replaces your current mail() call:
composer require phpmailer/phpmailer
In your code, replace the original block with this implementation:
require 'vendor/autoload.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
$mail = new PHPMailer(true);
try {
// Server settings
$mail->isSMTP();
$mail->Host = 'myserver';
$mail->SMTPAuth = true; // Usually required on Linux servers
$mail->Username = 'your_smtp_user'; // Required for most modern servers
$mail->Password = 'your_smtp_pass';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // or ENCRYPTION_SSL
$mail->Port = 587; // Standard port for SMTP with TLS
// Recipients
$mail->setFrom('no-reply@mydomain.com', 'No Reply');
$mail->addAddress($email, '');
// Content
$mail->isHTML(true);
$mail->Subject = 'Confirmation For Request';
$mail->Body = '<html>
<body>
<p>Hi '.$firstname.' '.$lastname.'</p>
<p>We recieved below details from you. Please use given Request/Ticket ID for future follow up:</p>
<p>Your Request/Ticket ID: <b>'.$ticketID.'</b></p>
<p>Thanks,<br>'.$team.' Team.</p>
</body>
</html>';
$mail->send();
// Email sent successfully
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
This approach eliminates the ambiguity of sendmail_path and ensures consistent behavior across operating systems. It also forces you to handle authentication explicitly, which is a best practice for production environments. If you must stick to the system's sendmail, you would need to configure Postfix or Exim to act as an SMTP relay that accepts connections on port 25 from your application user, but this adds significant complexity regarding firewall rules and domain permissions.
For modern applications, especially those running on shared hosting or dedicated Linux servers, relying on the SMTP ini settings with a generic sendmail path is a legacy pattern that often breaks in production. Switching to an explicit SMTP library ensures you have control over encryption, authentication, and error handling, preventing silent failures where emails are simply not delivered despite returning true.