1

I don't know is the problem, this is my angular script to send a form data to php script for email. I have tried to access code sample from the github and here but none seems to work out for me. I have the following error each time I send over but it works perfectly on postman.

Error: Access to XMLHttpRequest at 'http://127.0.0.1/mediacrow/mymail/mailer_php.php/' from origin 'http://127.0.0.1:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: The 'Access-Control-Allow-Origin' header contains multiple values '*, *',

Angular: contact.component.ts

send() {
    console.log(this.contactForm.value);
   this.http.post(`http://127.0.0.1/mediacrow/mymail/mailer_php.php/`,
                {
                   data: this.contactForm.value,
                   headers:
                     {
                      'Content-Type': 'application/x-www-form-urlencoded'
                      }
             })
    .subscribe((resp) => {
    this.snackBar.open(
      'Thank you for your submission!',
      '', {duration: 4000}
    );
   }, (err) => {
    this.snackBar.open(
      'This was an error with your submission!',
      '', {duration: 4000}
    );
   });

  }

This is my email script

<?php

// Headers 
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json');
//add more for creating  a post 
header('Access-Control-Allow-Methods: POST');
header('Access-Control-Allow-Headers: Access-Control-Allow-headers,
Content-Type, Access-Control-Allow-Methods');
$errors = '';

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

    $from_email = $request->email;
    $message = $request->message;
    $phone  = $request->phone;
    $platform = $request->platform;
    $browser = $request->browser;
    $name = $request->firstname.' '.$request->lastname;
    $from_name = $name;
    $response_array = array();


    $to_email = "adex@gmail.com";

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

    $content = "<p><strong>Phone Number: </strong>$phone</p><p><strong>Browser: </strong>$browser</p>
    <p><strong>Platform: </strong>$platform</p><p><strong>Message: </strong>$message</p>";

    $website = "M&M";
    $email_subject = "Contact Form";

    $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 );

} else {

    $response_array['status'] = 'error';
    echo json_encode($response_array);
}
?>

I will be so much grateful as I don't know what could have been the problem because it works perfectly on postman

Payne
  • 543
  • 2
  • 12
  • 32

1 Answers1

0

The OPTION request is sent before when your POST request is sent. But in your PHP code, you didn't allow OPTION method.

It's not PHP code, but I think this link(CORS JSON php No 'Access-Control-Allow-Origin' header is present on the requested resource) will be helpful for you.

JW Kim
  • 171
  • 7
  • Thank you so much but where do I put the function as it wasn't mentioned protected void Application_BeginRequest() { if (Request.Headers.AllKeys.Contains("Origin", StringComparer.OrdinalIgnoreCase) && Request.HttpMethod == "OPTIONS") { Response.End(); } } – Payne Mar 13 '19 at 00:44
  • Through [this comment](https://stackoverflow.com/a/4045448/10931353), you'll find out where you should put `OPTION` method. – JW Kim Mar 13 '19 at 00:57