Php 如何在本地主机内发送电子邮件?
我在尝试向用户发送新的随机密码时遇到了这个错误 警告:mysql_num_rows()要求参数1为资源、对象 在第30行的C:\xampp\htdocs\FYP\u v4\doForgotPassword.php中给出 从doForgotPassword:Php 如何在本地主机内发送电子邮件?,php,html,html-email,Php,Html,Html Email,我在尝试向用户发送新的随机密码时遇到了这个错误 警告:mysql_num_rows()要求参数1为资源、对象 在第30行的C:\xampp\htdocs\FYP\u v4\doForgotPassword.php中给出 从doForgotPassword: <?php include 'dbFunctions.php'; include 'navigationBar.php'; function createRandomPassword() { $chars = "ABCDEFG
<?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>
我不知道您是否还有其他错误,但您提到的错误是由
if (mysql_num_rows($emailResult) = 1) {
它应该是==
。=
用于赋值,不能将值赋值给函数。使用==
进行比较
而且应该是mysqli_num_rows
错误消息与主题标题不对应
第30行导致致命错误:
if (mysql_num_rows($emailResult) = 1) {
您在那里使用了mysql\u num\u rows
,但在使用mysqli
之前。您必须在此处使用mysqli\u num\u行
,并与两个=
(=
赋值,=
比较):
[安装]假windows sendmail。如果您未使用XAMPP,可以在此处下载:http://glob.com.au/sendmail/sendmail.zip
[修改]php.ini文件以使用它(注释掉其他行):
[邮件功能]
; 仅适用于Win32。
; SMTP=SMTP.gmail.com
; smtp_端口=25
; 仅适用于Win32。
; sendmail_from=@gmail.com
; 仅适用于Unix。您也可以提供参数(默认值:“sendmail-t-i”)。
sendmail\u path=“C:\xampp\sendmail\sendmail.exe-t”
(忽略“仅Unix”位,因为我们实际上使用的是sendmail)
然后,您必须在安装sendmail的目录中配置“sendmail.ini”文件:
[发送邮件]
smtp_server=smtp.gmail.com
smtp_端口=25
error\u logfile=error.log
debug_logfile=debug.log
验证用户名=
验证密码=
force_sender=@gmail.com
您需要一封邮件server@MatejŽvan嗨,我正在使用gmail SMTP服务器。有人告诉我这应该行得通,但这不适合我。请提供建议。是的,我认为这应该有效。检查下面的答案以了解您的错误。解决此问题后,我发布了另一个问题:[单击此处寻求解决方案][1][1]:
if (mysqli_num_rows($emailResult) == 1) {
// ^^ ^^
[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