0

I have built a contact form using AngularjS for my frontend and PHP for the mail client that sends the email. I want to pass data from PHP to Angular, telling Angular whether or not the email sent. However, when I try to do that, I get a "Syntax error: Unexpected number".

My PHP:

<?php
        require_once "../vendors/PHPMailer-master/PHPMailerAutoload.php";

    print_r($_POST);
    $data = array();

    $sender_name = isset($_POST['sender_name']) ? $_POST['sender_name'] : '';
    $sender_email = isset($_POST['sender_email']) ? $_POST['sender_email'] : '';
    $sender_tel = isset($_POST['sender_tel']) ? $_POST['sender_tel'] : '';
    $sender_message = isset($_POST['sender_message']) ? $_POST['sender_message'] : '';

    $mail = new PHPMailer;

    //Enable SMTP debugging. 
    $mail->SMTPDebug = 3;                               
    //Set PHPMailer to use SMTP.
    $mail->isSMTP();            
    //Set SMTP host name                          
    $mail->Host = "smtp.gmail.com";
    //Set this to true if SMTP host requires authentication to send email
    $mail->SMTPAuth = true;                          
    //Provide username and password     
    $mail->Username = "*****@gmail.com";                 
    $mail->Password = "******";                           
    //If SMTP requires TLS encryption then set it
    $mail->SMTPSecure = "tls";                           
    //Set TCP port to connect to 
    $mail->Port = 587;                                   

    $mail->From = $sender_email;
    $mail->FromName = $sender_name;


    $mail->addAddress("[email protected]", "Blah");

    $mail->isHTML(true);

    $mail->Subject = "Message Sent from jcrageralternatives.com by: ".$sender_name;
    $mail->Body = "<p>Name: ".$sender_name."</p><p>Email Provided: ".$sender_email."</p><p>Phone Number Provided: ".$sender_tel."</p><p>Message: '".$sender_message."'</p>";
    $mail->AltBody = $sender_message;

    header('Content-Type: application/json');

    if(!$mail->send()) 
    {
        $data["success"] = false;
    } 
    else 
    {
        $data["success"] = true;
    }

    echo json_encode($data);
?>

And my controller:

$http({
        method  : 'POST',
        url     : '/php/contact.php',
        data    : $.param($scope.contactData),  // pass in data as strings
        headers : { 'Content-Type': 'application/x-www-form-urlencoded' }  // set the headers so angular passing info as form data (not request payload)
        }).then(function (response) {
             console.log(response.data);
        }
});

If I remove the header('Content-Type: application/json'); from the PHP file and still console.log(response.data), I get:

2016-01-16 02:34:28 Connection: opening to smtp.gmail.com:587, timeout=300, options=array (
                                      )
    2016-01-16 02:34:28 Connection: opened
    2016-01-16 02:34:29 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP ry1sm18220246pab.30 - gsmtp
    2016-01-16 02:34:29 CLIENT -> SERVER: EHLO localhost
    2016-01-16 02:34:29 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [73.15.255.61]
                                      250-SIZE 35882577
                                      250-8BITMIME
                                      250-STARTTLS
                                      250-ENHANCEDSTATUSCODES
                                      250-PIPELINING
                                      250-CHUNKING
                                      250 SMTPUTF8
    2016-01-16 02:34:29 CLIENT -> SERVER: STARTTLS
    2016-01-16 02:34:29 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
    2016-01-16 02:34:29 CLIENT -> SERVER: EHLO localhost
    2016-01-16 02:34:29 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [73.15.255.61]
                                      250-SIZE 35882577
                                      250-8BITMIME
                                      250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH
                                      250-ENHANCEDSTATUSCODES
                                      250-PIPELINING
                                      250-CHUNKING
                                      250 SMTPUTF8
    2016-01-16 02:34:29 CLIENT -> SERVER: AUTH LOGIN
    2016-01-16 02:34:29 SERVER -> CLIENT: 334 VXNlcm5hbWU6
    2016-01-16 02:34:29 CLIENT -> SERVER: dmliaHUxMjAxQGdtYWlsLmNvbQ==
    2016-01-16 02:34:29 SERVER -> CLIENT: 334 UGFzc3dvcmQ6
    2016-01-16 02:34:29 CLIENT -> SERVER: Q0BycGVEMWVt
    2016-01-16 02:34:29 SERVER -> CLIENT: 235 2.7.0 Accepted
    2016-01-16 02:34:29 CLIENT -> SERVER: MAIL FROM:<v@v>
    2016-01-16 02:34:29 SERVER -> CLIENT: 250 2.1.0 OK ry1sm18220246pab.30 - gsmtp
    2016-01-16 02:34:29 CLIENT -> SERVER: RCPT TO:<****@gmail.com>
    2016-01-16 02:34:29 SERVER -> CLIENT: 250 2.1.5 OK ry1sm18220246pab.30 - gsmtp
    2016-01-16 02:34:29 CLIENT -> SERVER: DATA
    2016-01-16 02:34:29 SERVER -> CLIENT: 354  Go ahead ry1sm18220246pab.30 - gsmtp
    2016-01-16 02:34:29 CLIENT -> SERVER: Date: Sat, 16 Jan 2016 02:34:28 +0000
    2016-01-16 02:34:29 CLIENT -> SERVER: To: **** **** <****@gmail.com>
    2016-01-16 02:34:29 CLIENT -> SERVER: From: v <v@v>
    2016-01-16 02:34:29 CLIENT -> SERVER: Subject: Message Sent from jcrageralternatives.com by: v
    2016-01-16 02:34:29 CLIENT -> SERVER: Message-ID: <421aa50e45d9e33b9b7c41918d99af59@localhost>
    2016-01-16 02:34:29 CLIENT -> SERVER: X-Mailer: PHPMailer 5.2.14 (https://github.com/PHPMailer/PHPMailer)
    2016-01-16 02:34:29 CLIENT -> SERVER: MIME-Version: 1.0
    2016-01-16 02:34:29 CLIENT -> SERVER: Content-Type: multipart/alternative;
    2016-01-16 02:34:29 CLIENT -> SERVER:   boundary="b1_421aa50e45d9e33b9b7c41918d99af59"
    2016-01-16 02:34:29 CLIENT -> SERVER: Content-Transfer-Encoding: 8bit
    2016-01-16 02:34:29 CLIENT -> SERVER:
    2016-01-16 02:34:29 CLIENT -> SERVER: This is a multi-part message in MIME format.
    2016-01-16 02:34:29 CLIENT -> SERVER:
    2016-01-16 02:34:29 CLIENT -> SERVER: --b1_421aa50e45d9e33b9b7c41918d99af59
    2016-01-16 02:34:29 CLIENT -> SERVER: Content-Type: text/plain; charset=us-ascii
    2016-01-16 02:34:29 CLIENT -> SERVER:
    2016-01-16 02:34:29 CLIENT -> SERVER: as;lkdfjas;ldkf
    2016-01-16 02:34:29 CLIENT -> SERVER:
    2016-01-16 02:34:29 CLIENT -> SERVER:
    2016-01-16 02:34:29 CLIENT -> SERVER: --b1_421aa50e45d9e33b9b7c41918d99af59
    2016-01-16 02:34:29 CLIENT -> SERVER: Content-Type: text/html; charset=us-ascii
    2016-01-16 02:34:29 CLIENT -> SERVER:
    2016-01-16 02:34:29 CLIENT -> SERVER: <p>Name: v</p><p>Email Provided: v@v</p><p>Phone Number Provided: 1234567891</p><p>Message: 'as;lkdfjas;ldkf'</p>
    2016-01-16 02:34:29 CLIENT -> SERVER:
    2016-01-16 02:34:29 CLIENT -> SERVER:
    2016-01-16 02:34:29 CLIENT -> SERVER:
    2016-01-16 02:34:29 CLIENT -> SERVER: --b1_421aa50e45d9e33b9b7c41918d99af59--
    2016-01-16 02:34:29 CLIENT -> SERVER:
    2016-01-16 02:34:29 CLIENT -> SERVER: .
    2016-01-16 02:34:30 SERVER -> CLIENT: 250 2.0.0 OK 1452911670 ry1sm18220246pab.30 - gsmtp
    2016-01-16 02:34:30 CLIENT -> SERVER: QUIT
    2016-01-16 02:34:30 SERVER -> CLIENT: 221 2.0.0 closing connection ry1sm18220246pab.30 - gsmtp
    2016-01-16 02:34:30 Connection: closed
    {"success":true}

The first location of the Unexpected Number error, after Objects.parse (native) is at fromJSON from the AngularJS library. Where is the unexpected number and what can I do to fix this so I can read response.data["success"] ?

2
  • 1
    Disable SMTP-Debugging so it doesn't print out all that stuff, maybe?! Commented Jan 16, 2016 at 8:02
  • @tkausl legitimately feel so dumb right now lol, thank you! Commented Jan 16, 2016 at 8:16

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.