2026-07-15

php.ini, sendmail configuration to send an email using a php script

Stefan Bogdanescu

Stefan Bogdanescu

Founder & Senior Architect

php.ini, sendmail configuration to send an email using a php script

Sending Emails from PHP: Decoding php.ini, Sendmail, and SMTP Configuration

Sending emails programmatically is a fundamental task in web development, but configuring the underlying system settings—like php.ini and mail transfer agents (MTAs)—can often be more complex than it seems. When developers try to bridge the gap between simple PHP functions and complex external services like Gmail's SMTP server, they frequently encounter silent failures where the script runs without error, but no email is ever delivered.

This post will walk you through why your current setup isn't working, explain the role of system configurations versus application-level settings, and guide you toward a reliable method for sending emails in PHP.

The Pitfall of Mixing mail() and SMTP Configuration

You are attempting to use the basic PHP mail() function while simultaneously trying to configure an external SMTP server (like Gmail) via php.ini directives. This is where the confusion arises.

The core issue is that the standard PHP mail() function relies entirely on the operating system's local Mail Transfer Agent (MTA), such as Sendmail or Postfix, to handle the actual delivery. These systems are typically configured for local delivery or simple relaying, not direct, authenticated SMTP communication required by services like Gmail.

When you set directives in php.ini related to SMTP or Sendmail, you are configuring how PHP interacts with the local mail system. If your environment requires authentication (which most modern providers do), simply setting these variables in php.ini is insufficient. The script executes successfully because PHP found a mail handler, but the message never leaves the local machine properly authenticated to the remote server.

Understanding Your Environment and Configuration

Let's break down what you provided:

  1. PHP Script:

    mail('sugar.donkey@gmail.com','Helo','This is a test','From:salt@goodness.com');
    

    This function delegates the task to the OS mail utility.

  2. php.ini Configuration: The configuration you listed under [mail function] attempts to modify how PHP interacts with the system mail setup. For modern SMTP relaying, this method is often bypassed in favor of using dedicated libraries that handle the SMTP protocol directly.

  3. External SMTP Config: Your settings (smtp_server, smtp_port, auth_username, etc.) are meant for an application layer (like a custom script or framework) to initiate an SMTP connection, not typically for the basic PHP mail() function.

The reason you get no error but no email is that the system mail setup doesn't know how to handle the authentication handshake required by Gmail; it just sends the request locally and fails silently at the network level.

The Developer's Solution: Use Dedicated SMTP Libraries

For reliable, authenticated email delivery, especially when using services like Gmail, SendGrid, or Mailgun, the professional standard is to bypass the system-level mail() function entirely and use a dedicated library that handles the full SMTP protocol handshake.

The most popular and robust solution in the PHP ecosystem is PHPMailer. PHPMailer allows your script to connect directly to the SMTP server (e.g., smtp.gmail.com), provide the necessary username and password for authentication, and manage the entire message transmission process correctly.

Here is a conceptual example using PHPMailer, which provides much greater control than relying on system mail settings:

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;

require 'vendor/autoload.php'; // Ensure you have installed PHPMailer via Composer

$mail = new PHPMailer(true);

try {
    // Server settings (using Gmail example)
    $mail->isSMTP();                                            // Send using SMTP
    $mail->Host       = 'smtp.gmail.com';                      // Specify main and backup SMTP servers
    $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
    $mail->Username   = 'sugar.donkey@gmail.com';               // Your Gmail address
    $mail->Password   = '[MYPASSWORDHERE]';                     // Your Gmail password (use App Passwords)
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;         // Use TLS encryption
    $mail->Port       = 587;                                    // Standard port for STARTTLS

    // Recipients
    $mail->setFrom('salt@goodness.com', 'Goodness Sender');
    $mail->addAddress('recipient@example.com');

    // Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Test Email from PHP';
    $mail->Body    = 'This is a test message sent securely via SMTP.';
    $mail->send();
    echo 'Message has been sent successfully';

} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
?>

Conclusion

While tinkering with php.ini and Sendmail configurations is valuable for deep system administration, it often introduces brittle dependencies when trying to achieve modern email delivery via external SMTP services. For robust application development, especially when dealing with external authentication (like connecting to Gmail), relying on established libraries like PHPMailer provides a secure, reliable, and portable solution. By shifting from system-level mail handling to protocol-level SMTP implementation, you ensure your emails are delivered correctly every time, regardless of the underlying operating system configuration. If you are building complex applications, focusing on modular, tested solutions is key, much like how modern frameworks encourage best practices, similar to the structure found in projects on laravelcompany.com.

Note: Blog content is currently available in English.

Tags:

Enhance your marketing setup with your own email marketing platform.

Join the growing number of SaaS platforms using Laravel Mail to offer email marketing solutions to their customers.