[英]Nested function with phpmailer is not working
I have a this script, which is used for a forgot password procedure inside an Ionic app.我有一个这个脚本,用于在 Ionic 应用程序中忘记密码程序。 Firstly, I check if the entered e-mail address in the input field is already registered.
首先,我检查在输入字段中输入的电子邮件地址是否已经注册。 If not, it echo "no e-mail registered", if yes, the user should get an e-mail.
如果不是,则回显“没有注册电子邮件”,如果是,则用户应该收到一封电子邮件。
I tested the php mailer function without the first function, which check if the user is already registered.我在没有第一个函数的情况下测试了 php mailer 函数,它检查用户是否已经注册。 This works smoothly and the user get the e-mail.
这很顺利,用户会收到电子邮件。 Then I build the forgot function to check the e-mail in MySQL DB.
然后我构建了忘记功能来检查 MySQL DB 中的电子邮件。 So i nested my send function inside the forgot function, but its not working.
所以我将我的发送函数嵌套在忘记函数中,但它不起作用。 I tried the forgot function without the nested send function by replace it with a simple string.
我通过用一个简单的字符串替换它来尝试没有嵌套发送函数的忘记函数。 This works.
这有效。 So, I suppose there is something missing in order to send the email if email address exist in MySQL DB.
因此,我想如果 MySQL 数据库中存在电子邮件地址,则缺少某些内容才能发送电子邮件。
Here is the code这是代码
<?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}';
}
}
?>
In the forget()
function, you are defining a send()
function.在
forget()
函数中,您定义了一个send()
函数。 This function is never called so the mail will never be sent.永远不会调用此函数,因此永远不会发送邮件。 you have to declare your send function outside forget.
你必须在忘记之外声明你的发送函数。 Then, call the send() function where needed :
然后,在需要的地方调用 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}';
}
}
As mentionned in my comment, you are using a deprecated library for mysql and you should consider updating your code with mysqli
or PDO
.正如我在评论中提到的,您正在使用一个已弃用的 mysql 库,您应该考虑使用
mysqli
或PDO
更新您的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.