我有一个这个脚本,用于Ionic app中的忘记密码程序。首先,我检查输入字段中输入的电子邮件地址是否已经注册。如果没有,它回显“没有注册电子邮件”,如果是,用户应该收到一封电子邮件。
我在没有第一个功能的情况下测试了php邮件程序功能,检查用户是否已注册。这很顺利,用户可以收到电子邮件。然后我构建了忘记函数来检查MySQL DB中的电子邮件。所以我在忘记函数中嵌套了我的发送函数,但它不起作用。我尝试了没有嵌套发送函数的忘记函数,用一个简单的字符串替换它。这有效。因此,我认为如果MySQL DB中存在电子邮件地址,则发送电子邮件时会丢失一些内容。
这是代码
<?php
include 'dbconn.php';
require 'mailer/PHPMailerAutoload.php';
if (isset($_SERVER['HTTP_ORIGIN'])) {
header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Max-Age: 86400'); // cache for 1 day
}
// Access-Control headers are received during OPTIONS requests
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
exit(0);
}
$postdata = file_get_contents("php://input");
if (isset($postdata)) {
$request = json_decode($postdata);
forgot($request);
}
function forgot($request)
{
$email = $request->email;
$query = mysql_query("SELECT * FROM users WHERE email = '$request->email'") or die(mysql_error());
if (mysql_num_rows($query) > 0) {
$row = mysql_fetch_array($query) or die(mysql_error());
if ($row["email"] == $request->email) {
function send($parent_request)
{
//$email = $request->email;
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = '*****.net'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'info@*****.com'; // SMTP username
$mail->Password = '*****'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
$mail->setFrom('info@****.com', 'Team');
$mail->addAddress($email, 'Joe User');
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Passwort Reset';
$mail->Body = 'Hallo lieber User <br> Hier kannst du dein Passwort für die varfinz.ch App zurückstellen. <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if ($mail->send()) {
echo '{"result":1}';
} else {
echo '{"result":0}';
}
}
} else {
echo '{"result":0}';
}
}
else {
echo '{"result":0}';
}
}
?>
答案 0 :(得分:1)
在forget()
函数中,您正在定义send()
函数。永远不会调用此函数,因此永远不会发送邮件。你必须在外面声明你的发送功能。
然后,在需要的地方调用send()函数:
function send($email)
{
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = '*****.net'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'info@*****.com'; // SMTP username
$mail->Password = '*****'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
$mail->setFrom('info@****.com', 'Team');
$mail->addAddress($email, 'Joe User');
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Passwort Reset';
$mail->Body = 'Hallo lieber User <br> Hier kannst du dein Password für die varfinz.ch App zurückstellen. <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if ($mail->send()) {
echo '{"result":1}';
} else {
echo '{"result":0}';
}
}
function forgot($request)
{
$email = $request->email;
$query = mysql_query("SELECT * FROM users WHERE email = '$email'") or die(mysql_error());
if (mysql_num_rows($query) > 0) {
$row = mysql_fetch_array($query) or die(mysql_error());
if ($row["email"] == $email) {
send($email);
} else {
echo '{"result":0}';
}
}
else {
echo '{"result":0}';
}
}
正如我在评论中提到的,您使用的是不推荐使用的mysql库,您应该考虑使用mysqli
或PDO
更新代码。