I am not 100% sure that I can give you an exact answer, but I think that there are some common problems / confusions / misunderstandings that you encounter, and understanding them can help you move in the right direction.
First of all, you should now ignore angular and only consider PHP (which you tried to do). angular is basically red-herring: if you can get your PHP script to send emails without using Angular, then it will be easy to get angular to send emails using the PHP endpoint. The trick is that your script is currently accepting its input through the POST body using JSON, so if you just load it into your browser, nothing will happen. Instead, you can test this endpoint directly with things like curl. If you have curl installed, you can do something like this from the command line:
curl -d '{"email":" johndoe@example.com ", "message": "Hi", "name": "Conor Mancone"}' 'http://example.com/email.php'
The d flag indicates message data (and implicit flags for the POST request). If you do not have localization, you can use the online curl or set up a postman. These are tools you could start learning now.
This will allow you to debug your PHP endpoint more efficiently. Most importantly, you can see the exit directly. The next step is to copy and paste the output into something like jsonlint . It looks like your PHP endpoint is not returning JSON properly, and this will allow you to understand this part. Most importantly, you can ignore angular and find out why you are not sending emails. On this note, go to PHP and talk about some common problems in your code that may or may not cause problems, but certainly don't help your case:
$errors = ''; if(empty($errors)){ // send email } else { // return error }
This first part is pretty obvious to those who are looking at your code for the first time. You make $errors empty, and then all your email sending logic ends up in an if (empty($errors)) state. Lose the $errors variable. Lose this if-else. Never leave code in your application that doesn't actually do anything. It just gives you more options for introducing errors for no reason.
Also, this is a minor point, but you are not doing input validation. If JSON is sent to the endpoint, you are missing data that your script will crash. Someone may put HTML in a message, which can be unpleasant at best or dangerous at worst.
You also have a bunch of errors at the end of your script:
echo json_encode($response_array); echo json_encode($from_email); header($response_array); return $from_email;
You output $response_array as JSON to the browser, then you run json_encode on a line ( $from_email ) that will not even form a valid JSON, and the combination of them will definitely not be valid JSON. You should have only one echo json_encode , otherwise the result will not be valid JSON and you will get a parsing error in your angular interface.
Next, you pass $response_array to the php header function. It definitely does nothing for you. header expects a string, not an array, and is used to set HTTP / HTTP key pairs in an HTTP response. I cannot imagine that you want to set any data in your $response_array values โโas HTTP headers, and even if you want it, you cannot do this by passing $response_array . Therefore, be sure to kill this line. PHP ignores it anyway.
Similarly, there is no reason to return anything. Returning from a file executed by an HTTP request will have no effect. As a rule, you should not go back beyond functions (and this is not a function). Although I don't think this line causes any errors, it also does nothing. If he does nothing, delete it. To be clear, I'm talking about this line: return $from_email; .
All of this script should consist of reading in the mail data, sending an email, and then repeating a single json_encode call. Anything else that ends up with invalid JSON that your external application cannot read. Again, use curl (or other similar tools) to directly invoke your PHP script and debug it. That way you can see what it outputs and verify that it returns the correct JSON.
Email
Now, the main problem: the lack of sending email. You definitely have invalid email headers. Each mail header must end with \r\n . You start well, but your last two header lines do not end properly. This may be enough to sink attempts to send e-mail. PHP, which is built into the mail function, does not notify you very well of errors, so you might be better off using a more robust email program. This will give you the best feedback if you are mistaken in setting up your email. Common in PHP is this guy:
https://github.com/PHPMailer/PHPMailer
I would start by fixing the email headers and see if that helps you. Otherwise, you may have to try out the actual email program. The reason is that the next most common reason email doesn't work is due to modern spam mitigation efforts. The PHP mail function will send email directly from the server itself. Many modern email systems (especially gmail) will automatically reject such messages, unless the domain name you are sending is correctly configured at the DNS level. However, you send from arbitrary email addresses (the contents of the variable $from_email , which comes from the user). Many email providers these days will automatically refuse such emails, and PHP will not know what happened and will not give you any instructions.
Instead, send from a fixed address that you control. Either follow $from_email in the email, or set it as the answer. It is best to use the actual email address and authenticate using SMTP. Gmail actually works great for this. You should find some examples of using the above PHPMailer with gmail to send directly from your gmail address. This minimizes the likelihood of rejection of your emails as spam, and also gives you additional feedback (if it is in your sent field, but does not appear, then it was rejected as spam).
Sending email today is complicated. This is not as simple as calling the mail function.