Troubleshooting PHP mail()

Sender domain policy

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 is using in its headers.

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. Open the emails in a text editor.
    You may see the following error:
    Sender domain not allowed. Please read: https://dhurl.org/20b (in reply to end of DATA command))

Why does this happen?

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:

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.

Error messages

When sending via PHP mail, you may see the following errors:

  • 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:465 [SMTP: Failed to connect socket: fsockopen(): unable to connect to ssl://mail.example.com:465 (Unknown error) (code: -1, response: )]
  • WordPress SMTP plugins
The SMTP debugging output is shown below:

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

Solution

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

The solution is to use your mail server's name as the hostname. In the code of your PHP mail form, you are using your domain as the mail server name. It may look something like this:

$mail_host = "mail.example.com";

Instead of using your domain name in the mail server variable, use your actual mail server name. View the following article for instructions on how to locate your mail server name.

Update your mail form code using an FTP client or SSH:

In your mail form, change your hostname to the following:

$mail_host = "imap.dreamhost.com"; 

Changing the hostname fixes this error. Your mail hostname may differ from the one above. View the following article for further details on how to locate your specific mail servername.

See also

Did this article answer your questions?

Article last updated PST.

Still not finding what you're looking for?