如何在本地主机内发送电子邮件


How do I send emails within the local host?

我在尝试向用户发送新的随机密码时遇到了此错误。

警告:mysql_num_rows() 期望参数 1 是资源、对象 在第 30 行的 C:''xampp''htdocs''FYP_v4''doForgotPassword.php 中给出

来自doForgotPassword

<?php
include 'dbFunctions.php';
include 'navigationBar.php';
function createRandomPassword() {
    $chars = "ABCDEFGHJKLMNOPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz023456789";
    $i = 0;
    $pass = '';
    while ($i <= 8) {
        $num = mt_rand(0, 58);
        $tmp = substr($chars, $num, 1);
        $pass = $pass . $tmp;
        $i++;
    }
    return $pass;
}
if (isset($_POST)) {
    $username = $_POST['username'];
    $newPassword = createRandomPassword();
    $updateQuery = "UPDATE student_profile SET password = SHA1('" . $newPassword . "') WHERE student_id = '" . $username . "'";
    $updated = mysqli_query($link, $updateQuery) or die(mysqli_error($link));
    $email = filter_input(INPUT_GET, 'email');
    $emailQuery = "SELECT * FROM student_profile WHERE email = '$email'";
    $emailResult = mysqli_query($link, $emailQuery) or die(mysqli_error($link));
    if (mysql_num_rows($emailResult) == 1) {
        if ($updated) {
            $rows = mysql_fetch_array($emailResult);
            $to = $rows['email'];
            $subject = "NAPFA Test - New Password";
            $message = "Your new password is $newPassword";
            //$headers = 'From: rpfyp2001@gmail.com';
           // $emailSent = mail($to, $subject, $message, $headers);
            //CODE FOR SENDING EMAIL
            $headers  = 'MIME-Version: 1.0' . "'r'n";
            $headers .= 'Content-type: text/html; charset=iso-8859-1' . "'r'n";
            $headers .= 'From: rpfyp2001<rpfyp2001@gmail.com>'. "'r'n"; 
            $emailSent = mail($to,$subject,$message,$headers);
            if ($emailSent) {
                $statusMessage = "The email has been sent.<br />";
                $statusMessage .= "<a href='login.php'>Home</a>";
            }
        } else {
            $statusMessage = "Process failed. Please try again";
            $statusMessage .= "<a href='forgotPassword.php'>Forgot Password</a>";
        }
    } else {
        $statusMessage = "Please fill up Forgot Password form <a href='forgotPassword.php'>here</a>";
    }
} else {
    if ($_POST ['username'] != "") {
        echo "Invalid User.";
    }
}
?>
<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Republic Polytechnic | NAPFA: Forgot Password </title>
    </head>
    <body>
        <p>
            <?php
            echo $statusMessage;
            ?>
        </p>
    </body>
</html>

错误消息与主题标题不对应。

致命错误由第 30 行引起:

if (mysql_num_rows($emailResult) = 1) {

你在那里用过mysql_num_rows,但在你用mysqli之前。您必须在此处使用mysqli_num_rows并与两个==进行比较(=分配,==比较):

if (mysqli_num_rows($emailResult) == 1) {
//       ^^                       ^^
我不知道

您是否还有其他错误,但是您提到的错误是由

if (mysql_num_rows($emailResult) = 1) {

应该是==.=用于赋值,不能为函数赋值。使用==进行比较。

正如黑豹提到的,它应该是mysqli_num_rows

 [Install] the "fake sendmail for windows". If you are not using XAMPP you can download it here: http://glob.com.au/sendmail/sendmail.zip
[Modify] the php.ini file to use it (commented out the other lines):
[mail function]
; For Win32 only.
; SMTP = smtp.gmail.com
; smtp_port = 25
; For Win32 only.
; sendmail_from = <e-mail username>@gmail.com
; For Unix only. You may supply arguments as well (default: "sendmail -t -i").
sendmail_path = "C:'xampp'sendmail'sendmail.exe -t"

(ignore the "Unix only" bit, since we actually are using sendmail)

You then have to configure the "sendmail.ini" file in the directory where sendmail was installed:
[sendmail]
smtp_server=smtp.gmail.com
smtp_port=25
error_logfile=error.log
debug_logfile=debug.log
auth_username=<username>
auth_password=<password>
force_sender=<e-mail username>@gmail.com