【发布时间】:2020-11-27 17:10:31
【问题描述】:
我目前正在使用 vue-cli 构建一个没有真正后端的小型网站。 由于我在 php(以及一般的后端)方面是一个真正的土豆,我想知道我在这个网站上的联系表格有多安全,如果它不够安全该怎么办。
总而言之,我正在从我的 vue 应用程序对我服务器上的 mail.php 文件进行 fetch() 调用。 此 mail.php 文件使用 PHPMailer 将电子邮件从我的域的一个电子邮件地址发送到我的另一个地址。 到目前为止,它运行良好。
这是我与发送邮件相关的 JS:
sendMail () {
this.sendRequest()
.then(r => {
if (r.mailSent === 'ok') {
// mail sent callback
}
})
},
async sendRequest () {
const response = await fetch('./mail.php', {
method: 'post',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify({
subject: 'Message de ' + this.firstNameInput + ' ' + this.lastNameInput + ' via mysite.com',
body: '<html>' +
'<body>' +
'<p>De : ' + this.firstNameInput + ' ' + this.lastNameInput + '</p>' +
'<p>Email : ' + this.mailInput + '</p>' +
'<p>Message :<br>' + this.messageInput.replace(/\n/g, '<br>') + '</p>' +
'</body>' +
'</html>',
altBody: 'De : ' + this.firstNameInput + ' ' + this.lastNameInput + ' /// Email : ' + this.mailInput + ' /// Message :' + this.messageInput
})
})
return response.json()
}
这是我的 mail.php 文件的内容:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require './PHPMailer/src/Exception.php';
require './PHPMailer/src/PHPMailer.php';
require './PHPMailer/src/SMTP.php';
header('Content-Type: application/json');
include './mailSettings.php'; // contains $site, $mailFrom, $mailTo, $senderName and $recipientName
if(@isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER'] == $site) {
$contentType = isset($_SERVER["CONTENT_TYPE"]) ?trim($_SERVER["CONTENT_TYPE"]) : '';
if ($contentType === "application/json") {
$content = stripslashes(trim(file_get_contents("php://input")));
$decoded = json_decode($content, true);
if(!is_array($decoded)) {
echo '{"status":"error"}';
} else {
$mail = new PHPMailer(TRUE);
try {
$mail->setFrom($mailFrom, $senderName);
$mail->addAddress($mailTo, $recipientName);
$mail->Subject = $decoded['subject'];
$mail->isHTML(TRUE);
$mail->Body = $decoded['body'];
$mail->AltBody = $decoded['altBody'];
$mail->send();
}
catch (Exception $e) {
echo $e->errorMessage();
}
catch (\Exception $e) {
echo $e->getMessage();
}
echo '{ "mailSent": "ok"}';
}
} else {
echo '{"status":"error"}';
}
} else {
echo '{"status":"no call accepted from this adress"}';
}
?>
我猜我还有一些事情要做以确保安全,但你们能帮我找出原因吗?
【问题讨论】:
标签: php vue.js email security phpmailer