WordPress Quick Tip: Fix Form Mail Not Being Delivered

I ran into a problem recently where emails from from submissions on a client’s site were not being delivered.

The site is hosted on Godaddy, the mail is hosted elsewhere, the DNS is on Godaddy but in another account.

The emails had been working fine up until a few months ago (the client only just noticed a couple weeks ago, though).

Since it had been working then stopped, I assumed the client had changed something about their email or about their DNS and that broke it.

DNS: I got access to edit their zone file and updated the SPF (sender policy framework) record to include the site’s IP address and Godaddy’s relay server. I also added the DKIM record suggested in the site’s cPanel.

Still no love.

I checked the site’s cPanel to make sure that “Remote mail exchanger” was selected in the email routing settings.

Still no love.

CPanel also suggested editing the PTR record, but Godaddy doesn’t allow users to do that. Their help doc says they automatically set it to the correct thing.

I wondered if it was possible that at some point in October (when the emails stopped being delivered), something had changed at Godaddy and the PTR record needed to change to but didn’t…. or something.

I chatted with Godaddy support about it for a bit online and that went nowhere. Finally the rep said that since the server is a VPS, I would have to call and speak to an “expert” because the chat personnel “we have limited access to Gen3 VPS.”

I’m really too busy for long phone calls (which often end up being pointless), so I decided to keep investigating on my own.

Then, I woke up in the middle of the night and I remembered another site on the exact same serve that had had this same problem. The next day looked at what I did there.

This “note to self” from cobbled code is what had gotten me close to a fix on that site.

Here’s what I ended up doing:

The email needs to be sent from an address on the same domain as the web site. Even though my forms were set to do that, when the email was actually sent, I could see in the site’s WHM email reports that the “from” was actually ip-address-blah-blah-domain@blah-blah-ipaddress.secureserver.net.

So the PHP mailer was not using the from address I specify in WordPress and or Gravity Forms, but an address for Godaddy’s relay server.

I made a “mail fixer” php file and included it into my functions.php:

/* 
   this forces wordpress to always use the same from address
   you don't necessarily have to do this
 */
add_filter( 'wp_mail_from', 'theme_name_mail_from' );
function theme_name_mail_from( $email ) {
    return "name@your-domain.com";
}

/* 
  thanks to https://cobbledco.de/set-envelope-from-header-for-wp-mail/
  for the following 
*/
add_action( 'phpmailer_init', 'theme_name_add_phpmailer_setfrom' );
/**
 * Add setFrom for hosts that insist on making life hard.
 *
 * @param array $phpmailer The PHPMailer instance, passed by reference.
 */
function theme_name_add_phpmailer_setfrom( $phpmailer ) {
	
	$phpmailer->setFrom(
		'name@your-domain.com', // From email address.
		'Your Web Site' // From name.
	);
}

Why?

One thing I’m not sure about is the “why.” Why did this suddenly become necessary? Was it because Godaddy changed something?

I don’t usually enable comments, but since I’m still curious about the why, I will enable them for a limited time on this post. Note that I will moderate the comments relentlessly.

Feel free to share any relevant insights in the comments.