Troubleshooting PHP mail()

 

Overview

The following explains how to troubleshoot and resolve two common PHP mail() errors:

  • Sender domain policy errors
  • Certificate errors

How to fix sender domain policy errors

If you’ve created a PHP mail form and find it’s not sending email, then it’s most often due to the FROM address the form uses in its headers.

Finding the error

A simple way to confirm if this is the case:

  1. Log in to your web server via FTP or SSH.
  2. Navigate to the /Maildir/new folder which is located in your user’s directory:
    /home/username/Maildir/new
    In this directory, you’ll see emails that have failed to send.
  3. Edit the emails via FTP or SSH.
    You may see the following error:
    Sender domain not allowed. Please read: https://dhurl.org/20b (in reply to end of DATA command))

Fixing the error

This happens because of the following reasons:

  • The outgoing spam prevention policies that were put into effect, as detailed here.
  • The FROM address you’re using is not an email address on the domain you’re sending from and/or it’s not hosted with DreamHost. For example, if you’re sending from a site called example.com, the FROM address in your PHP Mail form must be something like admin@example.com. Changing this to your email hosted with DreamHost allows the form to send correctly.

If DreamHost is not hosting your email, you must use SMTP authentication to send in your PHP mail form. If you enable SMTP authentication, you can use any email address you like as the FROM address.

Email messages aren't being sent on shared servers

If you've verified that you've configured your domain properly to follow Sender Domain Policy, but email messages still aren't being sent from a shared server, it's possible that the server has reached its hourly sending limit. To avoid this, make sure your site is configured to use SMTP for sending. See the following articles for more information:

How to fix certificate errors

In the past, it was possible to use mail.example.com to connect (where example.com is your actual domain name). Due to how modern mail clients handle security, this is now NOT recommended as it may throw connection errors. Please ensure you are only using your mail server name when connecting. This should either be imap.dreamhost.com or pop.dreamhost.com.

Certificate errors are another common issue that may occur when sending PHP mail(). The following shows how to find the error and a few steps to help you resolve it.

Finding the error

When sending PHP mail(), you may see the following error messages:

PHPMailer

Warning: stream_socket_enable_crypto(): Peer certificate CN=`*.mail.dreamhost.com' did not match expected CN=`mail.example.com'

PHP SMTP mail

Failed to connect to ssl://mail.example.com [SMTP: Failed to connect socket: fsockopen(): unable to connect to ssl://mail.example.com (Unknown error) (code: -1, response: )]

WordPress SMTP plugins

The SMTP debugging output is shown below:

2024-03-25 19:44:51	Connection: opening to ssl://mail.example.com, timeout=300, options=array (
                   	                  )
2024-03-25 19:44:53	SMTP ERROR: Failed to connect to server:  (0)
2024-03-25 19:44:53	SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

Fixing the error

This is due to the hostname being used. The certificate references mail.dreamhost.com, which is not the same as your domain. See this article for an explanation of this error.

The solution is to use your mail server name as the hostname. For example, in the PHP code of your mail form, you are using your domain as the hostname:

$mail_host = "mail.example.com";

Instead, use your DreamHost mail server name. Update your mail form code using an FTP client or SSH to the following:

$mail_host = "imap.dreamhost.com"; 

Changing the hostname fixes this error.

See also

Did this article answer your questions?

Article last updated PST.

Still not finding what you're looking for?