Sending email with PHP from an SMTP server

Sending email with PHP from an SMTP server

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Sending Email with PHP from an SMTP Server

The error SMTP server response: 530 SMTP authentication is required is a definitive indicator of the current state of email delivery in modern environments. It signals that the receiving mail server has rejected your connection because you are attempting to use the raw mail() function without providing valid credentials, or worse, using it with an old configuration that expects an open relay.

In the past, developers often assumed that setting up a local sendmail service via php.ini was sufficient for sending emails from any PHP application. The snippet you provided demonstrates exactly why this approach fails today:

$from = "someonelse@example.com";
$headers = "From:" . $from;
echo mail ("borutflis1@gmail.com" ,"testmailfunction" , "Oj",$headers);

While the code above is syntactically correct for PHP, it relies on the system's mail transfer agent (MTA) to deliver the message. By default, most web server distributions (like Apache or Nginx) do not include a functioning MTA like Postfix or Exim. Even if you configure SMTP = localhost and smtp_port = 25 in your php.ini, you are telling PHP to attempt to connect to a local SMTP daemon that likely doesn't exist or isn't listening on port 25. Furthermore, the configuration file snippet you shared reveals another critical issue: sendmail_from. This directive only controls the envelope sender address if you are using the system's mail queue; it does not authenticate with an external SMTP server like Gmail or a corporate Exchange instance.

The error code 530 specifically means the remote server requires authentication. You cannot simply "find" an SMTP server that accepts unauthenticated connections for sending mail, especially to major providers like Gmail (borutflis1@gmail.com). Open relays are effectively non-existent in the public internet due to spam filtering mechanisms (like Spamhaus). Attempting to send from a generic example.com domain without verified DNS records (SPF, DKIM, DMARC) will result in your emails landing in spam folders or being rejected entirely by modern gateways.

To resolve this, you must shift away from the legacy mail() function for authenticated SMTP scenarios and use PHP's built-in phpmailer library or a raw SMTP class wrapper. The most robust approach involves instantiating an SMTP connection directly within your script. This allows you to explicitly handle the handshake, authentication, and header injection required by modern servers.

Here is how you would refactor the logic to authenticate with an external server:

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'vendor/autoload.php';

$mail = new PHPMailer(true);

try {
    // Server settings
    $mail->isSMTP();
    $mail->Host       = 'smtp.gmail.com'; // Or your specific provider
    $mail->SMTPAuth   = true;             // Authentication is mandatory
    $mail->Username   = 'your_real_email@gmail.com'; 
    $mail->Password   = 'your_app_password'; // Never use main account password
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
    $mail->Port       = 587;

    // Recipients and Content
    $mail->setFrom('someonelse@example.com', 'Sender Name');
    $mail->addAddress('borutflis1@gmail.com', 'Recipient');
    
    $mail->Subject = 'testmailfunction';
    $mail->Body    = 'Oj';

    $mail->send();
    echo 'Message sent!';
} catch (Exception $e) {
    echo "Send failed: {$mail->ErrorInfo}";
}

This implementation addresses the core issue: it establishes a secure channel and provides the credentials the server demands. The Host variable must match the specific SMTP endpoint provided by your chosen email service provider. If you are using Gmail, for instance, you must ensure that "Less secure apps" is disabled in security settings (a deprecated feature) or, preferably, generate an "App Password" to use with this authentication flow.

If you do not have access to a custom domain or verified DNS records for example.com, sending from that address will likely still trigger spam filters even if the technical connection succeeds. The receiving server checks the SPF record of the envelope sender (someonelse@example.com) against the IP address connecting via SMTP. If these do not match, the email is flagged as spoofed.

For production applications where you need a reliable email stack without managing your own mail server infrastructure, using a dedicated API service (like SendGrid, Mailgun, or Amazon SES) is often superior. These services handle the complex DNS configuration and authentication behind the scenes, returning data that can be easily integrated into your Laravel application or any PHP backend via their respective SDKs.

Our network