简体   繁体   中英

Nested function with phpmailer is not working

I have a this script, which is used for a forgot password procedure inside an Ionic app. 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. This works smoothly and the user get the e-mail. Then I build the forgot function to check the e-mail in 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.

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. 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 :

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM