After send the mail in php resposence by json
In PHP, you can send a response to a mail request and return the response in JSON format. Here's an example:
<?php
// assume you have a mail function that sends the mail
function sendMail($to, $subject, $message) {
// your mail sending code here
//...
return array('status' => 'success', 'message' => 'Mail sent successfully');
}
// assume you have a JSON response function
function jsonResponse($data) {
header('Content-Type: application/json');
echo json_encode($data);
exit;
}
// send the mail and return the response in JSON
$to = '[email protected]';
$subject = 'Test Mail';
$message = 'This is a test mail';
$result = sendMail($to, $subject, $message);
if ($result['status'] == 'success') {
jsonResponse(array('message' => 'Mail sent successfully'));
} else {
jsonResponse(array('error' => 'Failed to send mail'));
}
?>
In this example, the sendMail
function sends the mail and returns an array with a status
key and a message
key. The jsonResponse
function takes this array as input, sets the Content-Type
header to application/json
, encodes the array to JSON using json_encode
, and exits the script.
When you call the script, it will send the mail and return the response in JSON format. For example, if the mail is sent successfully, the response will be:
{"message":"Mail sent successfully"}
If the mail fails to send, the response will be:
{"error":"Failed to send mail"}
You can then use this JSON response in your JavaScript code to handle the response accordingly.