can't send email to addresses at my own domain

can't send email to addresses at my own domain

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

Stefan Bogdanescu

Founder & Senior Architect

can't send email to addresses at my own domain

When you use PHP's built-in mail() function, it is a deceptively simple wrapper around the operating system's SMTP stack. Sending an email to an external provider like Gmail usually works instantly because the target server has no reason to reject your message based on your sender reputation or domain configuration. However, when the recipient address belongs to your own domain (me@mydomain.com), the behavior changes entirely due to how DNS and mail delivery logic interact with local hosts.

The immediate suspicion is often that the PHP script is broken or that the mail() function is failing silently. While checking error logs in the web server or PHP configuration is a valid first step, the root cause in this specific scenario is almost always network topology and DNS resolution rather than application code.

When you send an email to me@gmail.com, your mail transfer agent (MTA), which could be Postfix, Exim, or even a system default like Sendmail, queries the DNS for the MX record of gmail.com. It successfully connects to Google's servers and delivers the message. The loopback logic is bypassed because the destination is external.

Conversely, when you send an email to me@mydomain.com, the MTA attempts to resolve the domain name first. If your server is configured as a mail server for that domain, the system might attempt to deliver the message locally before sending it out over the network. This triggers a specific failure mode: the message lands in a local mailbox (often /var/mail/ or /var/spool/mail) rather than being processed by a queue daemon like Postfix, or it gets dropped immediately because the local delivery agent cannot find a valid user entry for that address.

In many shared hosting environments or simple LAMP stacks, the mail() function relies on the system's default MTA. If you have not explicitly configured Postfix to relay external mail and handle internal delivery correctly, sending mail to your own domain often results in a "local delivery failed" error. The operating system might think it is trying to deliver to itself via a local socket but lacks the necessary user mapping or queue configuration to process the message as if it arrived from an SMTP client.

To fix this without installing complex software like Postfix, you must understand that PHP's mail() function does not inherently bypass local delivery rules. The most robust solution for a developer maintaining a simple script is to ensure the domain's MX records are set up correctly and that your MTA is configured to handle local aliases properly. If you cannot modify the system-wide mail configuration, you should switch to a library that handles SMTP directly, such as PHPMailer or Laravel's Mail facade. These libraries connect explicitly to an SMTP server (like smtp.gmail.com or your own Postfix instance) and bypass the local mail() function entirely, forcing the message out of your network regardless of where the destination address resides.

If you are using Laravel, the framework provides a robust mail driver system. By configuring the environment to use smtp or ses, you gain visibility into delivery failures via logs rather than relying on the opaque success/fail return value of mail(). This approach decouples your application from the idiosyncrasies of the local OS mail queue, ensuring that messages to me@mydomain.com are treated with the same rigor as messages to external providers.

Ultimately, the issue is not a bug in PHP but a quirk of how legacy system calls interact with modern DNS and local delivery agents. Until you have a fully configured MTA that understands your local domain's aliasing rules, relying on mail() for internal addresses is a fragile pattern that requires direct SMTP connections to guarantee reliability.

Our network