title: How to execute a PHP email script using AJAX? [Current code is not working]
body: <p>After browsing stackoverflow and multiple other sites for a solution to my problem I have come up with some AJAX and a PHP email script using SwiftMailer.</p>
  
  <p>As far as I am aware, my AJAX is not running hence the PHP script is never executed.</p>
  
  <p>My question: Am I using AJAX correctly to execute my script? Additionally, is there a way to achieve my desired result?</p>
  
  <p>Context: I am working on a school coursework project based around a schedule/rota site for families. I have pretty much reached the standards for a first build and decided to add a feature to send an email when a job is scheduled.</p>
  
  <p>Email Script (domain has been censored):</p>
  
  <pre><code>&lt;?php
  
  $setBy = $_GET['setBy'];
  $jobEmail = $_GET['jobEmail'];
  $subject = $_GET['emailSub'];
  $content = $_GET['emailContent'];
  
  $headers = 'From: MyRota@*****' . "\r\n" .
          'Reply-To: MyRota@*****' . "\r\n" .
          'X-Mailer: PHP/' . phpversion();
  
  
  require_once '../swiftmailer/lib/swift_required.php';
  $transport = Swift_MailTransport::newInstance();
  // Create the message
  $message = Swift_Message::newInstance();
  $message-&gt;setTo(array(
      $jobEmail =&gt; "MyFamilyRota User",
  ));
  $message-&gt;setSubject($subject);
  $message-&gt;setBody($emailContent);
  $message-&gt;setFrom("MyRota@*****", "MyFamilyRota");
  $mailer = Swift_Mailer::newInstance($transport);
  $mailer-&gt;send($message);
  </code></pre>
  
  <p>The emailing part works perfectly but causes a page to run slowly when it is located on the actually scheduling .php due to email sending taking a long period of time. Hence my desire to push the email features to an alternative script that will handle it without slowing down a user's experience.</p>
  
  <p>My AJAX/PHP (does not include the whole file as the issue is with the following section):</p>
  
  <pre><code>$emailContent = "Assigned Job: $jobName\nJob Description: $jobDesc\nAssigned By: $setBy\nStart Time: $time_start\nEnd Time: $time_end\n";
      $emailContent = wordwrap($emailContent, $words = 70);
      $subject = "New Job Assigned: $jobName!";
      ?&gt;
      &lt;script type="text/javascript" src="jquery-3.1.0.js"&gt;&lt;/script&gt;
      &lt;script type="text/javascript"&gt;
          function sendEmail() {
              $.ajax({
                  method: 'get',
                  url: "job_email.php",
                  data: {
                      'setBy': &lt;?php $setBy ?&gt;,
                      'useremail': &lt;?php $emailRow[0] ?&gt;,
                      'emailContent': &lt;?php $emailContent ?&gt;,
                      'emailSub': &lt;?php $subject ?&gt;
                  },
                  success: function () {
                      alert("done");
                  }});
          }
      &lt;/script&gt;
      &lt;?php
      echo "&lt;script type='text/javascript'&gt;sendEmail();&lt;/script&gt;";
  </code></pre>
  
  <p>Is this the correct way of executing a php script with AJAX or have I done something completely wrong?</p>
  
  <p>(Please excuse any inefficient code or bad practice. I am relatively new to PHP and web development so I understand that some sections of my code may be sloppy.)</p>
  
  <p>EDIT - form sending the data:</p>
  
  <pre><code>&lt;form id='login' action='jobAssigner.php' method='post' accept-charset='UTF-8'&gt;
      &lt;fieldset&gt;
          &lt;legend&gt;Assign Job To &lt;?php echo $_SESSION['assignee_name'] ?&gt;&lt;/legend&gt;
          &lt;input type='hidden' name='submitted' id='submitted' value='1'/&gt;
          &lt;label for='jobSelect'&gt;Job Selection:&lt;/label&gt;
          &lt;?php echo $select; ?&gt;
          &lt;br /&gt;
          &lt;label for='time_start'&gt;Job Start:&lt;/label&gt;
          &lt;input type='datetime-local' name='time_start' id='time_start' value=''/&gt;
          &lt;br /&gt;
          &lt;label for='time_end'&gt;Job End:&lt;/label&gt;
          &lt;input type='datetime-local' name='time_end' id='time_end' value=''/&gt;
          &lt;br /&gt;
          &lt;input type='submit' name='assign' id='assign' value='Assign!' /&gt;
      &lt;/fieldset&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>This should all be working as jobs are being added to the database and appearing under an upcoming job list.</p>
  
  <p>UPDATE:</p>
  
  <p>After fixing a few errors with my code, I've reached a new error but the AJAX function is running now.</p>
  
  <p>The AJAX function returns with an error: POST http://******.com/home/job_email.php 500 (Internal Server Error)send @ jquery-3.1.0.js:9392ajax @ jquery-3.1.0.js:8999sendEmail @ jobAssigner.php:4(anonymous function) @ jobAssigner.php:18</p>
  
  <p>NEW AJAX:</p>
  
  <pre><code>function sendEmail() {
              $.ajax({
                  method: 'POST',
                  url: "job_email.php",
                  data: {
                      'setBy': '&lt;?php echo $newSetBy ?&gt;',
                      'useremail': '&lt;?php echo $emailRow[0] ?&gt;',
                      'emailContent': '&lt;?php echo $emailContent ?&gt;',
                      'emailSub': '&lt;?php echo $subject ?&gt;'
                  },
                  success: function () {
                      alert("done");
                  }});
          }
      &lt;/script&gt;
      &lt;?php
      echo "&lt;script type='text/javascript'&gt;sendEmail();&lt;/script&gt;";
  </code></pre>
  
  <p>UPDATE:</p>
  
  <p>Thank you to everyone for their contributions! :) The 500 error was a result of some variable name changes during the process of the original question. It is all fixed now.</p>