When I try to send an email trough an api, i get this message:
Access to XMLHttpRequest at 'https://api.urosciric.com/mail' from origin 'https://urosciric.com' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
I've seen many solutions of which none are the same and none of them worked so far.
Angular code:
const httpOptions = {
headers: new HttpHeaders({
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*"
})
};
this.http.post('https://api.urosciric.com/mail',
{ firstName: this.FirstName, lastName: this.LastName, email: this.Email, phone: this.Phone, text: this.Text },
httpOptions)
.pipe(catchError(err => {
this.onMailSend(false);
return throwError(err);
})).subscribe(data => {
this.onMailSend(true);
return data;
});
Laravel (api) code:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Validator;
use App\Mail\General;
class MailController extends Controller
{
public function send(Request $request){
$firstName = $request -> input('firstName');
$lastName = $request -> input('lastName');
$email = $request -> input('email');
$text = $request -> input('text');
$phone = $request -> input('phone');
$to = "ciricbgd@gmail.com";
$subject = "[urosciric.com] email from ".$firstName." ".$lastName;
$txt = $text;
$headers = "From: mail@urosciric.com";
mail($to,$subject,$txt,$headers);
return response()->json('Mail sent. Thank you!', 201);
return response()->json('Mail not sent. Please try contacting me directly at ciricbgd@gmail.com',400);
}
}
When i try in development or prod mode, I get this error, but when I use postman or api testing software, everything works fine.