0

I'm trying to figure out why I'm getting 500 (Internal Server Error) and my error function called. Is there any glaring error in the code

// $nameMap is a map of input names to table cells in the email
// e.g. <input name="email" value="somedude@gmail.com"/> gets made into the row <tr><td><b>Email Address:<b></td><td>somedude@gmail.com</td></tr>
// by the mapping 'email' => 'Email Address' in the array 
$nameMap = array( 'name' => 'Name', 'email' => 'Email Address', 'phone' => 'Phone Number', 'comments' => 'Comments');

// https://css-tricks.com/sending-nice-html-email-with-php/
$headers = "MIME-Version: 1.0\r\nContent-Type: text/html; charset=ISO-8859-1\r\n"; 
function contact ( )
{
    global $nameMap, $headers;

    $info = array('validName' => (strlen(trim($_POST['name'])) > 0 ? true : false), // name good as long as its not empty
                  'validEmail' => (preg_match("!^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$!",trim($_POST['email'])) == 1 ? true : false), // http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address
                  'validPhoneNumber' => (preg_match("!^(?:(?:\+?1\s*(?:[.-]\s*)?)?(?:\(\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\s*\)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s*(?:[.-]\s*)?)?([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})\s*(?:[.-]\s*)?([0-9]{4})(?:\s*(?:#|x\.?|ext\.?|extension)\s*(\d+))?$!",trim($_POST['phone'])) == 1 ? true : false) // http://stackoverflow.com/questions/123559/a-comprehensive-regex-for-phone-number-validation
                  );
    // see http://stackoverflow.com/questions/482377/php-regex-delimiter-whats-the-point about why "!" is around the regexes

    if (!$info['validName'] || !$info['validEmail'] || !$info['validPhoneNumber']) // if invalid name, email or phone number
    {
        $info['sentEmail'] = false;
    }
    else // inputs contained valid values
    {
        // contstruct email body
        $emailMsg = '<html><body><h3>Someone submitted a contact form ...</h3><table>';
        foreach ( $_POST as $key => $value ) if (array_key_exists($key, $nameMap)) $emailMsg .= '<tr><td><b>' . $nameMap[$key] . ':</b></td><td>' . $value . '</td></tr>';
        $emailMsg .= '</table></body></html>';

        // attempt to send email and set 'sentEmail' key accordingly
        $info['sentEmail'] = email("myemail@gmail.com","A Comment Was Submitted",$emailMsg,$headers) ? true : false;

    }

    echo json_encode($info);    
    die();
}

which is called by

                    $.ajax({
                        url: ajaxurl,
                        type: 'POST',
                        data: formdata,
                        async: false,
                        success: function (info) {
                            info = JSON.parse(info);
                            console.log(info); // TEST
                            if (info.sentEmail) // means info={validName:true,validEmail:true,validPhoneNumber:true,sentEmail:true}
                            {
                                $('.contact-form h1').text('Thank You!').css('text-align','center');    
                                $('.contact-form input').css('visibility', 'hidden'); 
                                $('.contact-form .contact-submit-btn').css('visibility', 'hidden');                   
                            }
                            else // means info has a false value for validName, validEmail or validPhoneNumber
                            {
                                if (!info.validName)
                                {
                                    $('.contact-form .error-message').text('You must enter a name');    
                                }
                                else if (!info.validEmail)
                                {
                                    $('.contact-form .error-message').text('You must enter a valid email address'); 
                                }
                                else if (!info.validPhoneNumber)
                                {
                                    $('.contact-form .error-message').text('You must enter a phone number in form XXX-XXX-XXXX');
                                }
                            }
                        },
                        error: function () {
                            $('.contact-form .error-message').text('Oops! An unexpected error occurred.');    
                        },
                        cache: false,
                        contentType: false,
                        processData: false
                    }); 
  • A 500 error is a generic "There is an error" type of error. If you want the specifics, read the error log. On debian systems (ubuntu, most common) you can find the error log at `/var/log/apache2/error.log` – Jonathan Kuhn Aug 19 '15 at 17:51
  • So I can safely assume that my code is fine and the problem is something with the server? –  Aug 19 '15 at 17:56
  • No. Look at the error log and you should see the specific error. A 500 error is thrown for any error, php, server, configuration...etc. The log will show you what the issue is whether it is with php or something else. – Jonathan Kuhn Aug 19 '15 at 18:07
  • @JonathanKuhn I finally figured out the problem! I was trying to call `email(...)`, not `mail(...)`. D'oh! –  Aug 19 '15 at 18:43

0 Answers0