I am using PHPMailer to send mails but its from my localhost in xampp. When I test my API in Postman, it takes too long to process the request but renders response status 200 but no JSON response returned. Below are my codes.
public function actionSendMail() {
//Getting request from frontend
$request = file_get_contents('php://input');
//Decoding input into an array
$input = json_decode($request, true);
//Validating request
if (is_null($input)) {
$response = json_encode(['error' => 'Bad Input']);
die($response);
} else {
//mail parameters
$to = $input['to'];
$subject = $input['subject'];
$body = $input['body'];
$headers = $input['headers'];
//Sending mail
if($result = $this->sendMail($to, $subject, $body, $headers) === true) {
$response = json_encode(['success' => true]);
echo $response;
} else {
$response = json_encode(['error' => 'Mail Not Sent']);
die($response);
}
}
}
private function sendMail ($to, $subject, $body, $headers) {
//Configurating PHP Mailer
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = 'secure.emailsrvr.com';
$mail->Port = 995;
$mail->SMTPAuth = true;
$mail->Username = 'example@example.com';
$mail->Password = '****';
$mail->SMTPSecure = 'ssl';
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->SetFrom('REDACTED@example.com');
$mail->AddReplyTo($headers);
$mail->Subject = $subject;
$mail->MsgHTML($body);
$mail->AddAddress($to, "");
if(!$mail->Send())
return $mail->ErrorInfo;
return true;
}
Thanks for your time.