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

时间:2015-01-06 09:23:25

标签: php html html-email

尝试向用户发送新的随机密码时,我一直面临此错误。

  

警告: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>

3 个答案:

答案 0 :(得分:1)

我不知道你是否还有其他错误,但你提到的错误是由

引起的
if (mysql_num_rows($emailResult) = 1) {

应该是===用于赋值,您无法为函数赋值。使用==进行比较。

panther mentioned应为mysqli_num_rows

答案 1 :(得分:1)

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

致命错误是由第30行引起的:

if (mysql_num_rows($emailResult) = 1) {

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

if (mysqli_num_rows($emailResult) == 1) {
//       ^^                       ^^

答案 2 :(得分:0)

 [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