SMTP server response: 530 5.7.0 Must issue a STARTTLS command first

SMTP server response: 530 5.7.0 Must issue a STARTTLS command first

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

Stefan Bogdanescu

Founder & Senior Architect

SMTP server response: 530 5.7.0 Must issue a STARTTLS command first

When you encounter the error 530 5.7.0 Must issue a STARTTLS command first while attempting to send mail via PHP's mail() function, the root cause is rarely a configuration file setting like php.ini. The server is explicitly rejecting the connection because it has not yet established a secure channel using TLS before accepting credentials or sending data.

The error message itself provides the instruction: you must issue a STARTTLS command first. In the context of Gmail and many other modern SMTP providers, this is non-negotiable. They require an encrypted connection to verify that the client identity matches the user attempting to authenticate. Without this encryption layer, the server treats any subsequent login attempt as a security risk and aborts the session immediately.

Your current approach relies entirely on PHP's built-in mail() function. This function is notoriously opaque regarding the underlying transport mechanism. When you set ini_set("SMTP", "smtp.gmail.com"), you are instructing PHP to use the SMTP extension if a local MTA (Mail Transfer Agent) like Postfix or Sendmail is configured to relay mail through that address. However, the built-in PHP SMTP client often struggles with modern security requirements out of the box. It may not support TLS negotiation correctly, or it might be attempting to connect on port 25 without explicitly upgrading the connection to TLS.

To resolve this, you need to move away from the mail() function for sending emails over Gmail's SMTP relay. The most robust solution is to use PHPMailer with its built-in SMTP transport class. This library handles the TCP connection, the HELO/EHLO handshake, and crucially, the STARTTLS command correctly according to the RFC specifications.

Here is how you should structure the sending logic:

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';
    $mail->SMTPAuth   = true;
    $mail->Username   = 'your-email@gmail.com';
    $mail->Password   = 'your-app-specific-password';
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // This forces the STARTTLS command
    $mail->Port       = 587;

    // Recipients
    $mail->setFrom('your-email@gmail.com', 'Sender Name');
    $mail->addAddress($recipientEmail);

    // Content
    $mail->isHTML(true);
    $mail->Subject = 'Test Subject';
    $mail->Body    = 'Hello, this is a test email.';

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

The critical line here is $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;. This explicitly tells the library to initiate a TLS session after connecting to port 587 but before sending any authentication data. If you omit this or set it to PHPMailer::ENCRYPTION_SMTPS, the behavior changes significantly, and port 465 is required instead. For Gmail, STARTTLS on port 587 is the standard requirement for API access.

If you are unable to integrate PHPMailer due to legacy constraints, you might consider using a dedicated SMTP extension or configuring a local MTA like Postfix to handle the relay. In that scenario, you would need to ensure your php.ini settings point to the local MTA rather than the remote Gmail address directly:

[smtp]
; Point PHP to the local MTA which is configured to talk to Gmail securely
SMTP = localhost
smtp_port = 25
; Or use a specific port if your local MTA configures it differently

However, even with this setup, the local MTA must be configured in /etc/postfix/main.cf (or equivalent) to relay through Gmail's SMTP servers using TLS. This involves setting relayhost = [smtp.gmail.com]:587, enabling smtp_use_tls = yes, and ensuring tls_CA_file is set to a trusted certificate authority bundle.

It is important to note that simply adding ini_set("SMTP", ...) in your script does not enable STARTTLS on its own. The protocol negotiation is a multi-step handshake that requires the underlying SMTP library to support it. Furthermore, if you are using OAuth2 for authentication, the implementation becomes even more complex, as Gmail has deprecated plain text passwords in favor of App Passwords or OAuth tokens, which must be handled within the library's authentication logic rather than via basic string parameters.

Ultimately, the error stems from a mismatch between the client's security capabilities and the server's requirements. By switching to a modern SMTP library that explicitly negotiates the TLS upgrade, you align your application with Gmail's security policies and eliminate the 530 rejection.

Our network