I'm building an API and on the aspect of sending a mail. I'm getting mail not sent error. In my API, the front-end sends a JSON request consisting of the mail parameters.
{
"to":"gideonappoh@gmail.com",
"subject":"Testing Reviewer's Page",
"body": "Hello Gideon",
"headers":"oksana.v@scopicsoftware.com"
}
I then decode them and pass them through a PHPMail method in my controller. But its not working, and I can't find whats wrong. Can someone help me. These 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(!$this->sendMail($to, $subject, $body, $headers)) {
$response = json_encode(['error' => 'Mail Not Sent']);
die($response);
} else {
$response = json_encode(['success' => true]);
echo $response;
}
}
}
private function sendMail ($to, $subject, $body, $headers) {
//Configurating PHP Mailer
$mail = new PHPMailer();
$mail->Host = 'stmp.emailsrvr.com';
$mail->Username = 'gideon.a@scopicsoftware.com';
$mail->Password = '*******';
$mail->Mailer = 'stmp';
$mail->Port = 25;
$mail->SMTPAuth = true;
//Sending mail
$mail->SetFrom($headers);
$mail->Subject = $subject;
$mail->IsHTML(true);
$mail->Body = $body;
$mail->AddAddress($to, "");
return $mail->Send();
}
I have read many articles on configuring xampp and I think I should work but still not working. Always getting the response { "error":"Mail not sent" }
Thanks for your help.