Send form detail to mail ID in angular 4

If you want to send you form detail to your mail ID

–contact.component.ts

onSubmit(){
    this.data = this.contactForm.value;
    this.http.post('http://pkanvi.com/send.php', this.data)
      .subscribe(
        (res:Response) => {
          console.log(res.json());
        },
        err => {
          console.log("Error occured");
        }
      );
  }

–send.php

<?php
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
header('Content-type: application/json');
header("Access-Control-Allow-Origin: *");
header('Access-Control-Allow-Headers: X-Requested-With, content-type, access-control-allow-origin, access-control-allow-methods, access-control-allow-headers');

echo json_encode($request);

$to = 'test.abc@gmail.com';
$subject = 'PK Anvi - Query';
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From:PK Anvi <'.$request->mail.'>' . "\r\n";
$message = '<table>';
$message .= '<tr>';
$message .= '<td>Name:</td>'.'<td>'.$request->name.'</td>';
$message .= '</tr>';
$message .= '<tr>';
$message .= '<td>Email ID:</td>'.'<td>'.$request->email.'</td>';
$message .= '</tr>';
$message .= '<tr>';
$message .= '<td>Phone No:</td>'.'<td>'.$request->phone.'</td>';
$message .= '</tr>';
$message .= '<tr>';
$message .= '<td>Message:</td>'.'<td>'.$request->comment.'</td>';
$message .= '</tr>';

if(mail($to, $subject, $message, $headers)){
echo "Your Query Successfully Sent, we will get back to you seen.";
}?>

Leave a Reply

Your email address will not be published. Required fields are marked *