Search code examples
javascriptphpemailhttp-status-code-500

Getting 500 error when trying to call PHP function via AJAX


I am trying to send trigger an email when a user completes a webform.

However I am getting the following error

enter image description here

Appreciate any advise on how to resolve this error.

JS file that will call the php function

$(function() {
  $('.error').hide();
  $(".request_demo").click(function() {

  // validate and process form here
  $('.error').hide();
  var email = $("#email").val();
  if (email == "") {
    $("#email_error").show();
    $("#email").focus();
    return false;
  }

  var email = $("#email").val();

  var dataString = '<p><strong>Email: </strong> ' + email + '</p>';
  $.ajax({
   type: "POST",
   url: "/script/send.php",
   data: { data: dataString, senderAddress: email },
   success: function() {
     // show a success message to your visitors
     // clear input field values

     $("#email").val('');    
   }
  });
  return false;  
 });
});

PHP file

<?php
  if( isset($_POST) ){

    $postData = $_POST;
    $mailgun = sendMailgun($postData);

    if($mailgun) {
     echo "Great success.";
    } else {
      echo "Mailgun did not connect properly.";
    }
 }

 function sendMailgun($data) {

   $api_key = 'xxxxxxx1234';
   $api_domain = 'our_domain';
   $send_to = 'some@email.com';

   // sumbission data
   $ipaddress = $_SERVER['REMOTE_ADDR'];
   $date = date('d/m/Y');
   $time = date('H:i:s');

   // form data
   $postcontent = $data['data'];
   $reply = $data['senderAddress'];  

   $messageBody = "<p>You have received a new message from the contact form on your website.</p>
         {$postcontent}
          <p>This message was sent from the IP Address: {$ipaddress} on {$date} at {$time}</p>";

   $config = array();
   $config['api_key'] = $api_key;
   $config['api_url'] = 'https://api.mailgun.net/v2/'.$api_domain.'/messages';

   $message = array();
   $message['from'] = $reply;
   $message['to'] = $send_to;
   $message['h:Reply-To'] = $reply;
   $message['subject'] = "New Message from your Mailgun Contact Form";
   $message['html'] = $messageBody;

   $curl = curl_init();

   curl_setopt($curl, CURLOPT_URL, $config['api_url']);
   curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
   curl_setopt($curl, CURLOPT_USERPWD, "api:{$config['api_key']}");
   curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
   curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
   curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
   curl_setopt($curl, CURLOPT_POST, true); 
   curl_setopt($curl, CURLOPT_POSTFIELDS,$message);

   $result = curl_exec($curl);
   curl_close($curl);
   return $result;
 }
?>

Solution

  • I am not sure this will solve your problem or not, but this may help you.

    1. Please check what DATA TYPE is your server expecting. In your ajax request you haven't mentioned any Content-Type. As this is a POST request, jQuery will consider form data as default Content-Type. As I can see you are providing a JS Object as data, by default it will be converted to a query string before sending the request to the server.

    2. If your server is expecting a json payload in the request, then you need to do JSON.stringify() you payload to convert the JS object into a JSON object. ie. data : JSON.stringify({ data: dataString, senderAddress: email })