簡體   English   中英

phpmailer 的嵌套函數不起作用

[英]Nested function with phpmailer is not working

我有一個這個腳本,用於在 Ionic 應用程序中忘記密碼程序。 首先,我檢查在輸入字段中輸入的電子郵件地址是否已經注冊。 如果不是,則回顯“沒有注冊電子郵件”,如果是,則用戶應該收到一封電子郵件。

我在沒有第一個函數的情況下測試了 php mailer 函數,它檢查用戶是否已經注冊。 這很順利,用戶會收到電子郵件。 然后我構建了忘記功能來檢查 MySQL DB 中的電子郵件。 所以我將我的發送函數嵌套在忘記函數中,但它不起作用。 我通過用一個簡單的字符串替換它來嘗試沒有嵌套發送函數的忘記函數。 這有效。 因此,我想如果 MySQL 數據庫中存在電子郵件地址,則缺少某些內容才能發送電子郵件。

這是代碼

<?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}';
    }
}

?>

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 庫,您應該考慮使用mysqliPDO更新您的代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM