how to send email using angular and php script

Viewed 120

Hi I am new to angular and I am trying to send a mail after the response of payment gateway. i am using a services to perform this operation its return error Please help!!!

This is mt

Component.ts

file function where i am send data to service

    sendMail() {

    this.submitted = true;
    this.name = this.getOrderDetails.shipping.name.full_name;
    //console.log(this.name);
    this.email = this.getOrderDetails.payee.email_address;
    this.amount = this.getOrderDetails.amount.value;
    this.address = this.getOrderDetails.shipping.address.address_line_1;
    let infoDetails = {
      name: this.name,
      email: this.email,
      amount: this.amount,
      address: this.address
    }

    this._invoice.sendInvoice(infoDetails)
      .pipe(takeUntil(this.unsubsribe$))
      .subscribe((response: Response) => {
        console.log(response);
        return this.responseMessage = "Thanks for the message! We will get back to you soon!";
      }, (error) => {
        return this.responseMessage = "Oops! An error occurred... Reload the page and try again.";
      });
  }

this is my

service.ts

import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs';
///import { Observable } from 'rxjs/Observable';

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

  constructor(private _http: HttpClient) { }

  sendInvoice(params: any): Observable<any> {
    return this._http.post('http://localhost/php/invoice-mail.php', params);
  }
}

This is my PHP SCRIPT to send the mail

<?php
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, X-Requested-With");


//$post_data = file_get_contents("php://input");
// $decodedText = html_entity_decode($post_data);
// var_dump($decodedText);
//$data = json_decode($post_data);

$response = new stdClass();
$response = file_get_contents('php://input');
$decodedText = html_entity_decode($response);
$myArray = json_decode('[' . $decodedText . ']', true);
//var_dump($response);
//die();

//$response_array = array();

if (isset($_POST["address"])) {
    $name = $_POST['name'];        // Sender's name
    $email = $_POST['email'];      // Sender's email address
    $phone  = $_POST['amount'];     // Sender's phone number
    $address = $_POST['address'];  // Sender's message
    $headers = 'From:' . $name . "\r\n";

    $to = 'moreashish2097@gmail.com';     // Recipient's email address
    $subject = 'Message from ' . $email . "\r\n"; // Message title

    $body = " From: $name \n E-Mail: $email \n Amount: $amount= \n Address: $address";

    // init error message
    $errmsg = '';
    // Check if name has been entered
    if (isset($_POST['name']) && $_POST['name'] == '') {
        $errmsg .= '<p>Please enter your name.</p>';
    }
    // Check if email has been entered and is valid
    if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
        $errmsg .= '<p>Please enter a valid email address.</p>';
    }
    //Check if phone number has been entered
    if (isset($_POST['phone']) && $_POST['phone'] == '') {
        $errmsg .= '<p>Please enter your phone number.</p>';
    }

    //Check if message has been entered
    if (isset($_POST['message']) && $_POST['message'] == '') {
        $errmsg .= '<p>Please enter your message.</p>';
    }

    /* Check Google captch validation */
    // if( isset( $_POST['g-recaptcha-response'] ) ){
    //  $error_message = validation_google_captcha( $_POST['g-recaptcha-response'] );
    //  if($error_message!=''){
    //      $errmsg .= $error_message;
    //  }
    // }    

    $result = '';
    // If there are no errors, send the email

    if (!$errmsg) {
        if (mail($to, $subject, $body, $headers)) {
            $result = '<div class="alert alert-success">Thank you for contacting us. Your message has been successfully sent. We will contact you very soon!</div>';
        } else {
            $result = '<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
        }
    } else {
        $result = '<div class="alert alert-danger">' . $errmsg . '</div>';
    }
    echo $result;
}

can anyone tell what is wrong with the code..

0 Answers
Related