php network ai_generated true

Warning: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set()

ID: php/mail-relay-denied

Also available as: JSON · Markdown
87%Fix Rate
89%Confidence
60Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
83 active

Root Cause

Mail relay denied errors occur when PHP's mail() function or SMTP library cannot connect to or authenticate with the mail server. Common causes include missing SMTP configuration, blocked port 25, or the mail server rejecting relay from unauthorized senders.

generic

Workarounds

  1. 92% success Use a library like PHPMailer or Symfony Mailer with authenticated SMTP
    Install PHPMailer: composer require phpmailer/phpmailer. Configure it with authenticated SMTP: set Host, Port (587 for TLS), SMTPAuth = true, Username and Password. This bypasses the mail() function entirely and provides proper SMTP authentication.
  2. 90% success Use a transactional email service (SendGrid, Mailgun, SES) via their API
    Sign up for a transactional email service and use their PHP SDK or HTTP API. For example, with Symfony Mailer: composer require symfony/mailer symfony/amazon-mailer and configure MAILER_DSN=ses+smtp://ACCESS_KEY:SECRET@default?region=us-east-1.

Dead Ends

Common approaches that don't work:

  1. Using PHP's built-in mail() function for production email delivery 75% fail

    The mail() function relies on the system's sendmail binary and local SMTP configuration. It provides no authentication, no TLS, poor deliverability, and emails frequently end up in spam folders or are rejected entirely.

  2. Opening port 25 on cloud instances to send mail directly 80% fail

    Most cloud providers (AWS, GCP, Azure) block outbound port 25 by default. Even if unblocked, sending email directly from application servers results in poor deliverability due to missing SPF/DKIM/DMARC records.

Error Chain

Leads to:
Preceded by:
Frequently confused with: