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.