简体   繁体   中英

500 error after PHP mail()

UPDATE I was able to get the hosting info from my client and I contacted support, apparently there's an issue with the hosts mail function at the moment and they are working on a resolution. Will wait to see if that's the cause of this problem and will report back.
END UPDATE

I am trying to set up a simple contact form that will send an email. I have the form action set to the below PHP file.

The email gets sent, but the user experience ends with a 500 error instead of sending the user to the confirmation page.

If I comment out the mail() part, then the form redirects the user to the confirmation page successfully, but of course no email gets sent.

The website is hosted on GoDaddy, and I don't have access to the hosting account, though I can try to get it if I need it.

Here's the PHP code:

<?php

$name = $_POST['name'];
$address = $_POST['address'];
$city = $_POST['CITY'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$email = $_POST['email'];
$howdidyouhear = $_POST['hear_about'];
$notifyshow = $_POST['notify_shows'];
$notifyonline = $_POST['notify_online'];
$interest_jewelry = $_POST['Interest_jewelry'];
$interest_prints = $_POST['interest_prints'];
$interest_folkart = $_POST['interest_folkart'];
$interest_indian = $_POST['interest_indian'];
$interest_closeouts = $_POST['interest_closeouts'];
$interest_other = $_POST['interest_other'];
$interest_other_text = $_POST['interest_other_text'];
$spamvalid = $_POST['validate'];
$honeypot = $_POST['website'];

//Spammer Handling
if ($honeypot!=null){echo 'You have been flagged as a spammer, please go away!'; exit;} 

if ($spamvalid != '357'){
    echo "
    <script>
        function goBack() {
            window.history.back()
        }
    </script>
    You didn't enter the correct number at the bottom of the form.  Please try again.<br><button onclick='goBack()'>Go Back</button>";
    exit;
}

//START EMAIL

//Body
$mailbody="Name: {$name}\n\nAddress: {$address}\n\nCity: {$city}\n\nState: {$state}\n\nZip: {$zip}\n\nEmail: {$email}\n\nHow did you hear about us?: {$howdidyouhear}\n\nWould you like to be notified when we will be doing a show in your area?: {$notifyshow}\n\nWould you like to receive email notifications of special sales and online events?: {$notifyonline}\n\nWhat brought you to mishuganah.com?: {$interest_jewelry} {$interest_prints} {$interest_folkart} {$interest_indian} {$interest_closeouts} {$interest_other}: {$interest_other_text}\n\n";

//Send Email
mail('matt.rodela@gmail.com','New submission from Mishuganah.com', $mailbody, "From:{$email}\r\n" );

header("Location: http://".$_SERVER["HTTP_HOST"]."/mailing_list/confirmation_page.htm");

?>

I am a relative novice with PHP, so please explain your solutions fully. Thanks!

Use phpMailer instead of php mail() function below you will find reasons not to use built in php mail function

In some cases, mails send via PHP mail() did not receive the recipients although it was send by WB without any error message. The most common reasons for that issue are listed below.

    wrong format of mail header or content (e.g. differences in line break between Windows/Unix)
    sendmail not installed or configured on your server (php.ini)
    the mail provider of the recipeint does not allow mails send by PHP mail(); common spam protection

Errors in the format of header or content can cause that mails are treated as SPAM. In the best case, such mails are transfered to the spam folder of your recipient inbox or send back to the sender. In the worst case, such mails are deleted without any comment. If sendmail is not installed or not configured, no mails can be send at all.

It is common practice by free mail provider such as GMX, to reject mails send via the PHP function mail(). Very often such mails are deleted without any information of the recipient.

So it turns out it was an issue on GoDaddy's end and it has been resolved. The form is working now. Apparently there was nothing wrong with the code.

Thanks for the suggestions folks, I learned some stuff (going to sanatize and filter my inputs now).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM