0

I am trying to run a contact form using Angular 8. I have found a solution here, but after adapting the code to mine, the console gives me an error 500. I'm a bit stuck here. What do I still have to do? what is wrong? thanks for advance.

This is my last version code: https://github.com/BiggsBottor/Portfolio.git

but these two files are that I'm trying to run:

contact.service.ts

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Contact } from '../models/contact';


@Injectable({
  providedIn: 'root'
})
export class ContactService {

  baseUrl = 'http://localhost/portfolio/api';
  // baseUrl = '../../../api';
  contact: Contact;

  constructor(private http: HttpClient) { }

  sendEmail() {
    console.log(this.contact);
    this.http.post(`${this.baseUrl}/email.php`, this.contact)
                  .subscribe(resp => console.log(resp), resp => console.log(resp));
  }
}

email.php

<?php 

switch($_SERVER['REQUEST_METHOD']){
case("OPTIONS"): //Allow preflighting to take place.
    header("Access-Control-Allow-Origin: *");
    header("Access-Control-Allow-Methods: POST");
    header("Access-Control-Allow-Headers: content-type");
    exit;
case("POST"): //Send the email;
    header("Access-Control-Allow-Origin: *");

    $json = file_get_contents('php://input');

    $params = json_decode($json);

    $name = $params->name;
    $email = $params->email;
    $phone = $params->phone;
    $subject = $params->subject;
    $message = $params->message;

    // the structure of the email itself
    $to = 'my-personal-email@gmail.com';

    $subject = "Portfolio Contact Form with topic: $subject";

    $body = "From:\n\nName: $name";
    $body .= "Email: $email\n\n";
    $body .= "Phone: $phone\n\n";
    $body .= "Message:\n$message";

    $header = "From: $name <$email>";

    if (!mail($to, $subject, $body, $header)) { 
        http_response_code(500); 
    }

    break;
default: //Reject any non POST or OPTIONS requests.
    header("Allow: POST", true, 405);
    exit;
}

?>
  • ERROR 500 is a general error everything can cause that problem, but yours is not a 500 error it’s showing that error because of you are asking to show error 500, Try to return real ERROR instead of http_response_code(500);! or read your error_log file to see what causing the problem. Enable error reporting in php –  Feb 19 '20 at 11:52
  • @Dlk the PHP error log will probably not show anything because the most likely explanation is that the 500 error was code-generated using http_response_code(), not the result of an exception. And it's sensible to return a 500 error instead of the real error details, because leaking error details can be security issue. Error details, if any, should be logged to a file elsewhere on the server. – ADyson Feb 19 '20 at 13:00
  • The root cause of this is most likely to be that `mail()` returned false (leading to the `http_response_code(500);` command, which generates the 500 error. I'm not sure this is the most helpful thing the code could do, because the server didn't actually crash, it simply failed to send the email for some reason. It might be better to return a user-friendly error message string instead. Meanwhile, to debug why the email sending failed, here's a good general guide to debugging email problems in PHP: https://stackoverflow.com/a/24644450/5947043 . – ADyson Feb 19 '20 at 13:02
  • If it turns out the http_response_code() command isn't the root of the 500 error (you can test easily, by commenting it out for a moment and re-running your code), then it likely means that the PHP code is crashing somewhere. You need to find out where, so first thing to do would be to enable error logging (if it's not already enabled) - see https://stackify.com/php-error-logs-guide/ for a guide, and then re-test, and examine the logs afterwards. Also see http://www.phpknowhow.com/basics/basic-debugging/ which has a simple guide to general debugging with PHP. – ADyson Feb 19 '20 at 13:04

0 Answers0