Overview
PHP mail() most often fails with one of two errors. This article explains how to find and fix each one:
- Sender domain policy errors
- Certificate errors
How do I 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.
How do I find the error?
A simple way to confirm if this is the case:
- Log in to your web server via FTP or SSH.
- Navigate to the /Maildir/new folder which is located in your user’s directory:
In this directory, you’ll see emails that have failed to send.
/home/username/Maildir/new - 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))
How do I fix 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 from Shared Hosting serversIf you've verified that you've configured your domain properly to follow Sender Domain Policy, but email messages still aren't being sent from the 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 do I 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.
How do I find 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
How do I fix 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.