0

I'm a bit of a novice when it comes to web development, but I'm trying to make a website using Angular CLI and for the contact page there needs to be a form to send an email directly to the company. I've made the form and everything works fine for that. All the routing is done and works. However, whenever I try to do an http POST to the php file, i get this 404 not found:

zone.js:2019 POST http://localhost:4200/src/assets/email.php 404 (Not Found)

I've seen TONS of other questions on here related to this topic but never found any that were solved. I know for a fact that the URL is correct because when I go to that URL, the php file will automatically download to my computer. So clearly it's there, but I must have gone wrong somewhere. Can anyone help me out? Here's the function that does the HTTP POST (which is in a service located in emailform.service.ts):

sendEmail(request: EmailRequest): Observable<EmailRequest> | any {
console.log("REQUEST : \n" + "FROM : " +  request.email + ",\nNAME : " +  request.name +", \n" 
+ "MSG : " +  request.message + 
",\nTO :  " + request.toaddr);

return this.http.post(this.emailPathPHP, request)
  .map(response => {
    console.log('Sending email was successfull', response);
    return response;
  })
  .catch(error => {
    console.log('Sending email got error', error);
    return Observable.throw(error)
  })

}

And here is the PHP file (made with help from a tutorial):

<?php
header('Content-Type: application/json');
    $errors='';

if(empty($errors)) {
    $postdata = file_get_contents("php://input", true);
    $request = json_decode($postdata);

    $from_email = $request->email;
    $message = $request->message;
    $from_name = $request->name;
    $to_email = $request->toaddr;

    $contact="<p><strong>Name:</strong> $from_name</p>
                    <p><strong>Email:</strong>$from_email</p>";
    $content = "<p>$message</p>"

    $website = "advanced-teknoleds";
    $email_subject = "$website: Email from $from_name.";

    $email_body = '<html><body>';
    $email_body .= "$contact $content";
    $email_body .= '</body></html>';

    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
    $headers .= "From: $from_email\n";
    $headers .= "Reply-To: $from_email";

    mail($to_email, $email_subject, $email_body, $headers);

    $response_array['status'] = 'success';
    $response_array['from'] = $from_email;

    echo json_encode($response_array);
    echo json_encode($from_email);
    header($response_array);
    return $from_email;
}
else {
    $response_array['status'] = 'error';
    echo json_encode($response_array);
    header('Location: /error.html');
}

?>

Any help would be greatly appreciated!! Thanks.

  • The fact that it will download means that PHP is being ran at all. Check https://stackoverflow.com/questions/18422140/apache-is-downloading-php-files-instead-of-displaying-them – apokryfos Jun 23 '17 at 16:18
  • Usually, stuff like this is a server thing. When the site gets served and you used dynamic links, it might not work the same in all environments. This is why a lot of languages use stuff like Url.action("controllerName", "action") and that generates a path on before page load that will work based on the location it is calling from. This is php and I am not too familiar with that but I would check the link being created / sent-out for the post – Christian4423 Jun 23 '17 at 16:18
  • @apokryfos I think you meant "PHP is *NOT* being ran at all" - right? – Scott C Wilson Jun 23 '17 at 16:20
  • Yes forgot the most important word of the sentence. – apokryfos Jun 23 '17 at 16:53
  • I've checked the URL and it's all correct. Is there something I need to configure so that the PHP script can be run? I'm running this locally using ng-serve, but I'm a bit confused as to what exactly that means for my PHP script – Daniel Gomm Jun 23 '17 at 16:55

0 Answers0