我正在构建API以及发送邮件方面。我收到的邮件没有发送错误。在我的API中,前端发送一个由邮件参数组成的JSON请求。
{
"to":"gideonappoh@gmail.com",
"subject":"Testing Reviewer's Page",
"body": "Hello Gideon",
"headers":"oksana.v@scopicsoftware.com"
}
然后我解码它们并通过我的控制器中的PHPMail方法传递它们。但它不起作用,我找不到什么错误。有人能帮我吗。这些是我的代码。
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)) {
$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->Host = 'stmp.emailsrvr.com';
$mail->SMTPAuth = true;
$mail->Username = 'gideon.a@scopicsoftware.com';
$mail->Password = 'n010grc7dsx';
$mail->Mailer = 'stmp';
$mail->SMTPDebug = 2;
$mail->Port = 25;
//Sending mail
$mail->SetFrom($headers);
$mail->Subject = $subject;
$mail->IsHTML(true);
$mail->MsgHTML($body);
$mail->AddAddress($to);
if(!$mail->Send())
return $mail->ErrorInfo;
return true;
}
我已经阅读了许多关于配置xampp的文章,我想我应该工作但仍然无法正常工作。始终获得回复{ "error":"Mail not sent" }
感谢您的帮助。