![](/img/trans.png)
[英]Why will my html/css/php form only send me an email when the email address is valid? I want an email regardless
[英]Why I get html code when I send email in php?
嗨,我正在為我的項目在 php 中開發一個忘記密碼系統。 我想給我的用戶發送一封電子郵件,以便他們可以點擊鏈接來重置他們的密碼。 但是當我打開收件箱時,我得到的是 html 文本而不是鏈接。 我嘗試了其他方法,例如向消息添加標題,但它仍然是 html 代碼。 這是我的代碼:
<?php
$errors = [];
$user_id = "";
$db = mysqli_connect('localhost', 'root', 'mySQLpassword', 'registration');
if (isset($_POST['login_user'])) {
$user_id = mysqli_real_escape_string($db, $_POST['user_id']);
$password = mysqli_real_escape_string($db, $_POST['password']);
if (empty($user_id)) array_push($errors, "Username or Email is required");
if (empty($password)) array_push($errors, "Password is required");
if (count($errors) == 0) {
$password = md5($password);
$sql = "SELECT * FROM users WHERE username='$user_id' OR email='$user_id' AND password='$password'";
$results = mysqli_query($db, $sql);
if (mysqli_num_rows($results) == 1) {
$_SESSION['username'] = $user_id;
$_SESSION['success'] = "You are now logged in";
header('location: index.php');
}else {
array_push($errors, "Wrong credentials");
}
}
}
if (isset($_POST['reset-password'])) {
$email = mysqli_real_escape_string($db, $_POST['email']);
query = "SELECT email FROM users WHERE email='$email'";
$results = mysqli_query($db, $query);
if (empty($email)) {
array_push($errors, "Your email is required");
}else if(mysqli_num_rows($results) <= 0) {
array_push($errors, "Sorry, no user exists on our system with that email");
}
$token = bin2hex(random_bytes(50));
if (count($errors) == 0) {
$sql = "INSERT INTO password_reset(email, token) VALUES ('$email', '$token')";
$results = mysqli_query($db, $sql);
$to = $email;
$subject = "Reset your password";
$msg = "Hi there, click on this <a href=\"new_password.php?token=" . $token . "\">link</a> to reset your password on our site";
$msg = wordwrap($msg,70);
$headers = "From: info@gmail.com";
mail($to, $subject, $msg, $headers);
header('location: pending.php?email=' . $email);
}
}
if (isset($_POST['new_password'])) {
$new_pass = mysqli_real_escape_string($db, $_POST['new_pass']);
$new_pass_c = mysqli_real_escape_string($db, $_POST['new_pass_c']);
$token = $_SESSION['token'];
if (empty($new_pass) || empty($new_pass_c)) array_push($errors, "Password is required");
if ($new_pass !== $new_pass_c) array_push($errors, "Password do not match");
if (count($errors) == 0) {
$sql = "SELECT email FROM password_reset WHERE token='$token' LIMIT 1";
$results = mysqli_query($db, $sql);
$email = mysqli_fetch_assoc($results)['email'];
if ($email) {
$new_pass = md5($new_pass);
$sql = "UPDATE users SET password='$new_pass' WHERE email='$email'";
$results = mysqli_query($db, $sql);
header('location: index.php');
}
}
}
?>
任何幫助將被排除在外!
要發送 HTML 郵件,必須設置 Content-type 標頭。
選中mail
文檔中的“示例 #5 發送 HTML 電子郵件”。
示例(這里的 $headers 是一個數組):
$headers[] = 'Content-Type: text/html; charset=UTF-8';
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.