cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Go to solution

Contact form email in PHP to send email on CPanel account fails

I've been trying for much of the day to send the results of a contact form over email with PHP from one of my GoDaddy sites. I'm using the PHP mail() function as is recommended by GoDaddy for a CPanel created PHP site. It returns success (true) with no errors, but I never receive the emailed contact form. I've searched online here and on stackoverflow but I can't seem to find any solution.

 

Can anyone share a minimum PHP script to send one email that works?

 

Things I've tried so far include:

* Sending email to and from only the same domain as the web site

* Reviewing all the mail() parameters

* Tested and verified that getting the contact form variables from the POST are working

 

Here's my code so far:

 

$fromEmail = "admin@email.com"; // replace with my domain
    $toEmail = "me@email.com";      // replace with my domain

    $name = $_POST["name"];
    $email = $_POST["email"];
    $phone = $_POST["phone"];
    $subject = $_POST["subject"];
    $message = $_POST["message"];

    $emailMessage = array(
        "Hello I'm testing this email service because it doesn't seem to be working right now. Date: ".date(DATE_RFC2822),
        "Name: ".$name,
        "Email: ".$email,
        "Phone: ".$phone,
        "Subject: ".$subject,
        "Message: ".$message
    );
    $emailMessage = implode("\r\n", $emailMessage);

    $headers = array("From: ".$fromEmail,
        "Reply-To: ".$fromEmail,
        "X-Mailer: PHP/" . PHP_VERSION
    );
    $headers = implode("\r\n", $headers);

    $output = mail($toEmail, "Contact Form test message from website", $emailMessage, $headers, "-f".$fromEmail);

    var_dump($output);
How can I get this to work?
14 REPLIES 14
Helper IV Helper IV
Helper IV
Solution

Re: Contact form email in PHP to send email on CPanel account fails

I'd recommend going even more bare bones than that for testing purposes. Create a file called test.php with the following:

 

<?php
   $to = “your@emailaddress.here“; // <– replace with your address here
   $subject = “Test mail”;
   $message = “Hello! This is a simple test email message.”;
   $from = “sender@emailaddress.here“;
   $headers = “From:” . $from;
   mail($to,$subject,$message,$headers);
   echo “Mail Sent.”;
?>

 

Upload it to your site and then call it directly through a web browser.  If it works, use it as the foundation for your own script. If it doesn't, then it could be an issue with GoDaddy not supporting php Mail so you'll need to start looking for some alternative approaches.

 

Good luck with this.

 

Cheers!

Doc

 

Need some help with WordPress? Check out Site Doctor 911
Worry-Free WordPress Support - So You Can Focus On The Important Stuff

Re: Contact form email in PHP to send email on CPanel account fails

Thanks for replying SiteDoctor. I was able to get more debugging by using PHPMailer from github. It showed the connections to both smtp.gmail.com:587 and to relay-hosting.secureserver.net:25 are being firewall blocked by GoDaddy. The only option is to send to localhost:25. That works, but unfortunately the localhost SMTP server knows nothing about the users that I created on GoDaddy in WORKSPACE for the domain even though I can send email from other email providers to those users into WORKSPACE. And the CPanel default configuration for emails to unknown addresses is to blackhole them. Once I set CPanel to instead show an error, those errors started appearing in the SMTP traces from PHPMailer. 

 

This is not getting me any further because the local SMTP server on the GoDaddy server is not able to forward any email anywhere outside of the local host 😞

 

I've been on chat with GoDaddy support asking for help for the last hour and a half.

Moderator
Moderator

Re: Contact form email in PHP to send email on CPanel account fails

Hi @duncanandrew.

When sending email from a cPanel account, you can only send using localhost.
You can adjust your email/MX settings on your cPanel account to deliver mail using a remote server instead of the local server. This would allow you to send email from your cPanel account to your Workspace accounts. See the second section of this article:
https://www.godaddy.com/help/changing-your-email-destination-with-cpanel-shared-hosting-12380
^Gary

Gary - GoDaddy | Community Moderator
24/7 Support | Check System Status

Re: Contact form email in PHP to send email on CPanel account fails

Thank you Gary! I set Remote Mail Exchanger on the MX Entry screen in CPanel and I can get email delivered to the WORKSPACE email account now. This should allow me to get email back to my own account for the contact form.

 

Thanks so much for replying. With most email connectivity blocked on GoDaddy this is really hard to debug.

Re: Contact form email in PHP to send email on CPanel account fails

This has been an issue I haven't been able to solve for a while, even with calling GoDaddy support. THANK YOU!!!

Re: Contact form email in PHP to send email on CPanel account fails

@SiteDoctor,

As a non-coder, can you explain this solution in amateur terms?

 

I also have a PHP form which seems to operate perfectly except the resultant email is never received. The target email account is also hosted on GoDaddy and has been tested as working. Your suggested solution seems to involve a non-GoDaddy mail server if I understand correctly. I'm just trying to send form results to a GoDaddy hosted email account.

Helper IV Helper IV
Helper IV

Re: Contact form email in PHP to send email on CPanel account fails

@JJimenez- I will give it a try.

 

If I understand what @duncanandrew and @GaryA are saying, GoDaddy is set up in such a way that email sent from cPanel's default local mail server cannot be received by GoDaddy's Workspace email accounts. I'm guessing it is something that GoDaddy has set up for security or anti-spam reasons.

When you have a cPanel account you can choose whether is sent through the cPanel mail server (localhost) or through a remote email server, like Gmail for instance. That's what they are talking about when they talk about changing the MX (Mail Exchange) record for the domain - switching from using the localhost server to a remote server. The remote email server will then be able to 'see' the Workspace email server and send email to it, and the Workspace email server will be able to receive that email.

In short, if you are sending email from a php form inside a cPanel account to a GoDaddy Workspace email address, you cannot send it from A to B. You have to switch the MX record of cPanel so you can send it from A to C to B, with C being a remote email server.

I hope that helps explain it a bit better - it is quite a technical concept!

 

Cheers

Doc

 

 

 

Need some help with WordPress? Check out Site Doctor 911
Worry-Free WordPress Support - So You Can Focus On The Important Stuff

Re: Contact form email in PHP to send email on CPanel account fails

OK thanks, but still not clear on this:

 

If the site and domain are hosted by GoDaddy, and the target email of the PHP form is the same domain on which the form resides, is this possible or not? If I can't use the GoDaddy server to send to my own GoDaddy hosted domain/email, exactly *what* server am I supposed to use? What "remote server" is being referred to?

New
Solution

Re: Contact form email in PHP to send email on CPanel account fails

While I can't speak to some of the tech-ease previously mentioned, I can tell you that I had this issue...and I, too, was trying to use the GoDaddy server to send to my own GoDaddy hosted domain/email based on data submitted from a contact form...and this helped me stumble across the solution for my situation.  I went into cPanel, selected MX Entry, picked this domain (both the website with the form and the hosted email are associated with it), and noticed that neither Local, nor Remote, was selected for my domain.  I selected Local and then Change.  A few minutes later, I retested my form and it worked as expected.  Even better, nothing else broke!  🙂  Best of luck, @JJimenez!

Re: Contact form email in PHP to send email on CPanel account fails

I'm using formmail.pl on all of my cpanel shared server websites for contact us forms such as

xynames.com/contact.html

 

and it works fine.

 

But now I'm trying to integrate invisible recaptcha into the forms and I'm not sure how to do it yet.

Re: Contact form email in PHP to send email on CPanel account fails

My best advice is to verify that your MX records are set correctly and then be patient.  It took almost 48 for me to see the results of changing ours and get our forms working.  

Re: Contact form email in PHP to send email on CPanel account fails

I just want to add that I was facing the same issue. I have a Office 365 email account linked to my domain and I wasn't able to send email to it. From what I have understood, the local mail server of cPanel cannot send emails to external servers so in the cPanel Email -> Mx Entry section, I changed the email routing from local to remote. It says for remote that "Configure server to not accept mail locally and send email to lowest MX record".  An MX record is a record in your DNS records that mapped an alias hostname to the name of a mail server. Since I have an Office 365 email account, there is a MX record mapping my domain to my Office 365 mail server. So that record is use to reroute emails send to my domain to that mail server, will deliver it to my 365 email. Hope that helps somebody.

Highlighted

Re: Contact form email in PHP to send email on CPanel account fails

I tried that simple mailform. It shows me "Mail sent". But I don't receive any email on my gmail.com address. What could be the issue? 

Re: Contact form email in PHP to send email on CPanel account fails

Their support knows NOTHING!!!! I was on the phone with them twice. My default Joomla contact form goes to never never land and they says it works. Something is wrong with Joomla's coding.