You should not be using the PHP mail function within your site as mail being sent from the server can be faulty at reaching the intended email address. In fact I’ve recently found out that there are many web hosts that in fact disable this function entirely (on shared hosting situations).
You should be using SMTP Authentication when sending mail from your site, because this way your mail isn’t sent from your web server, but rather through your email server. This method of authentication overcomes many issues sites have with emails getting through. I won’t elaborate further as the point of this post is to supply you with code to test whether or not your web host has disabled the PHP mail() function.
Be sure to replace the $from and $to variables below.
Bonus tip: A big gotcha if you’re using the PHPMailer script on your web host to send mail via SMTP authentication (which is a good idea rather than using the default PHP mail() function) is the port number you’ll be connecting to your mail server with. All of my hosts help files, config files, mail settings pages and scripts no where mention the port you should be using for this. What fun.
In my case I needed to use port 587 instead of any of the listed mail client settings. After searching I found on Mailgun’s blog that port 587 exists because:
This is the default mail submission port. When a mail client or server is submitting an email to be routed by a proper mail server, it should always use this port.
So this “unlisted-by-my-provider port” is for when computers (servers) are talking to other servers. As opposed to mail clients talking to servers. So watch for that.
WordPress tip: I’ve used Postman SMTP Mailer and Email Log with great success. This plugin in makes it so my contact form sends mail via SMTP. In fact, it’s wizard driven interface was first what tipped me off to the use of port 587.