title: Sending a web form email using Mandrill's PHP API and jQuery .ajax
body: <p>I've been trying to send a simple email from a web form using Mandril. I can get an email to send using mandrill's PHP example from their api docs.</p>
  
  <p>However when I try to pass in a json object through jQuery's .ajax function Mandrill is giving me this error:</p>
  
  <pre><code>Fatal error:  Function name must be a string in /home4/kknopper/public_html/mandrill.php on line 5
  </code></pre>
  
  <p>I'm new to working with .ajax and I feel like this could be a really simple fix.
  Any help would be appreciated!</p>
  
  <p><strong>js:</strong></p>
  
  <pre><code>//This all runs when the user submits the form
  
  var email = $("#email").val(); // get email field value
  var name = $("#fname").val(); // get name field value
  var format = "- ";
  var fname = format + name;
  var message = $("#message").val(); // get message field value
  message = message + "\n\n"+fname;
  
  
  function log(obj) {
    console.log(JSON.stringify(obj));
  }
  
  var params = {
      "message": {
          "from_email": email,
          "to":[{"email":"email@gmail.com"}],
          "subject": "Web Contact Form Response",
          "text": message
      }
  };
  
  console.log(params);
  
  var formReset = function() {
    $("#fname").val(''); // reset field after successful submission
    $("#email").val(''); // reset field after successful submission
    $("#message").val(''); // reset field after successful submission
    $("form input").attr("class","ng-pristine ng-invalid ng-invalid-required");
    $("form textarea").attr("class","ng-pristine ng-invalid ng-invalid-required");
    $("form button").attr("disabled","disabled");
  };
  
  $.ajax(
  {
      type: "POST",
      url: "mandrill.php",
      data: JSON.stringify(params)
  })
  .done(function(response) {
    alert(response);
  
      console.log(response);
       // show success message
      formReset(); // reset field after successful submission
  })
  .fail(function(response) {
          alert('Error sending message.');
              return false; // prevent page refresh
  })
  
  .success(function(data) {
      console.log(data);
      console.log(data[0].status);
      alert('Your message has been sent. Thank you!');
  
      if (!data.success) {
        // if not successful, bind errors to error variables
          // $scope.errorName = data.errors.name;
          // $scope.errorSuperhero = data.errors.superheroAlias;
      } else {
        // if successful, bind success message to message
          $scope.message = data.message;
      }
  });
  </code></pre>
  
  <p><strong>mandrill.php:</strong></p>
  
  <pre><code>&lt;?php
  require_once 'mandrill-api-php/src/Mandrill.php';
  try {
    $mandrill = new Mandrill('my api key');
    $input = json_encode($_POST('params'));
  
    //If I pass in this message variable the email sends.
    // $message = array(
  //     'text' =&gt; $input.message.text,
  //     'subject' =&gt; 'Web Contact Form',
  //     'from_email' =&gt; $_POST["params.message."],
  //     'from_name' =&gt; 'Example Name',
  //     'to' =&gt; array(
  //         array(
  //             'email' =&gt; 'email@gmail.com',
  //             'name' =&gt; 'Recipient Name',
  //             'type' =&gt; 'to'
  //         )
  //     )     
  // );
  $async = false;
  
  $result = $mandrill-&gt;messages-&gt;send($input, $async);
  print_r($result);
  
  } catch(Mandrill_Error $e) {
  // Mandrill errors are thrown as exceptions
  echo 'A mandrill error occurred: ' . get_class($e) . ' - ' . $e-&gt;getMessage();
  throw $e;
  }
  ?&gt;
  </code></pre>