How to execute php email script using AJAX? [Current code does not work]

After browsing stackoverflow and several other sites to solve my problem, I came up with an AJAX and PHP email script using SwiftMailer.

As far as I know, my AJAX is not working, so a PHP script is never executed.

My question is: am I using AJAX correctly to execute my script? In addition, is there a way to achieve the desired result?

Context: I am working on a school timetable-based school / company project for families. I pretty much reached the standards for the first build and decided to add an email sending feature when the job is scheduled.

Email script (domain was censored):

<?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->setTo(array(
    $jobEmail => "MyFamilyRota User",
));
$message->setSubject($subject);
$message->setBody($emailContent);
$message->setFrom("MyRota@*****", "MyFamilyRota");
$mailer = Swift_Mailer::newInstance($transport);
$mailer->send($message);

, , .php - . script, , .

My AJAX/PHP ( , ):

$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!";
    ?>
    <script type="text/javascript" src="jquery-3.1.0.js"></script>
    <script type="text/javascript">
        function sendEmail() {
            $.ajax({
                method: 'get',
                url: "job_email.php",
                data: {
                    'setBy': <?php $setBy ?>,
                    'useremail': <?php $emailRow[0] ?>,
                    'emailContent': <?php $emailContent ?>,
                    'emailSub': <?php $subject ?>
                },
                success: function () {
                    alert("done");
                }});
        }
    </script>
    <?php
    echo "<script type='text/javascript'>sendEmail();</script>";

php script AJAX - ?

( . PHP -, , .)

EDIT - :

<form id='login' action='jobAssigner.php' method='post' accept-charset='UTF-8'>
    <fieldset>
        <legend>Assign Job To <?php echo $_SESSION['assignee_name'] ?></legend>
        <input type='hidden' name='submitted' id='submitted' value='1'/>
        <label for='jobSelect'>Job Selection:</label>
        <?php echo $select; ?>
        <br />
        <label for='time_start'>Job Start:</label>
        <input type='datetime-local' name='time_start' id='time_start' value=''/>
        <br />
        <label for='time_end'>Job End:</label>
        <input type='datetime-local' name='time_end' id='time_end' value=''/>
        <br />
        <input type='submit' name='assign' id='assign' value='Assign!' />
    </fieldset>
</form>

, .

UPDATE:

, AJAX.

AJAX : POST http://******.com/home/job_email.php 500 ( ) send @jquery-3.1.0.js: 9392ajax @jquery- 3.1.0.js: 8999sendEmail @jobAssigner.php: 4 ( ) @jobAssigner.php: 18

NEW AJAX:

function sendEmail() {
            $.ajax({
                method: 'POST',
                url: "job_email.php",
                data: {
                    'setBy': '<?php echo $newSetBy ?>',
                    'useremail': '<?php echo $emailRow[0] ?>',
                    'emailContent': '<?php echo $emailContent ?>',
                    'emailSub': '<?php echo $subject ?>'
                },
                success: function () {
                    alert("done");
                }});
        }
    </script>
    <?php
    echo "<script type='text/javascript'>sendEmail();</script>";

UPDATE:

!:) 500 . .

+4
1

AJAX PHP, , PHP .

, AJAX :

<script type="text/javascript">
        function sendEmail() {
            $.ajax({
                method: 'get',
                url: "job_email.php",
                data: {
                    'setBy': ,
                    'useremail': ,
                    'emailContent': ,
                    'emailSub': 
                },
                success: function () {
                    alert("done");
                }});
        }
    </script>

, - . PHP Javascript <?php $setBy ?> .., - echo .

<?php echo $setBy ?>
+2

Source: https://habr.com/ru/post/1652534/


All Articles