gpt4 book ai didi

PHP SMTP 邮件脚本不起作用

转载 作者:行者123 更新时间:2023-11-29 06:42:42 25 4
gpt4 key购买 nike

我正在编写一个 php smtp 邮件脚本,以便在用户丢失密码时向他们发送邮件。但是它不起作用。

处理器总是返回:

Message could not be sent...

我已经用 Gmail smtp 尝试过,得到了相同的结果。

你能帮忙吗?有两个文件:表单和处理器。

下面是代码:

表格:

<html>
<head>
<meta name = 'viewport' content = 'width = device-width, initial-scale = 1.0, maximum-scale = 4.0, user-scalable = yes'>
<link rel = 'stylesheet' href = 'style.css' type = 'text/css'>
<title>Reset Password</title>
</head>
<?php
$db = new PDO('mysql:host=host;dbname=db;charset=utf8', 'user', 'pass');
$sql = $db->query('select id from posts order by id desc');
$row = $sql->fetch(PDO::FETCH_ASSOC);
echo "<a href = 'browse.php?page=".$row['id']."'>Browse</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href = 'index.php'>Home</a>";
echo "<hr>";
echo '<form name = "forgot-pass" action = "reset-pass-notif.php" method = "post">
<label><em>Input your registered email address</em></label><br>
<input type = "text" name = "email"><br>
<em>Enter Image Text:</em><img src="captcha.php" /><br />
<input name="captcha" type="text" /><br>
<input type = "submit" name = "submit" value = "Send Reset-Password Link">
</form>
<hr>
<a href = "browse.php?page='.$row["id"].'">Browse</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href = "index.php">Home</a>';
?>

</html>

处理器:

<html>
<head>
<meta name = 'viewport' content = 'width = device-width, initial-scale = 1.0, maximum-scale = 4.0, user-scalable = yes'>
<link rel = 'stylesheet' href = 'style.css' type = 'text/css'>
<title>Reset Password Notification</title>
</head>
<?php
error_reporting(E_ALL);
session_start();
$db = new PDO('mysql:host=host;dbname=db;charset=utf8', 'user', 'pass');
$sql0 = $db->query('select id from posts order by id desc limit 1');
$row0 = $sql0->fetch(PDO::FETCH_ASSOC);
echo '<a href = "browse.php?page='.$row0['id'].'">Browse</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href = "index.php">Home</a>';
echo '<hr>';
$email = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);
$sql = $db->prepare('select * from users where email = :email');
$sql->bindParam(':email', $email);
$sql->execute();
$sql->setFetchMode(PDO::FETCH_ASSOC);
$row = $sql->fetch();
$username = $row['username'];
$user_check = $row['user_hash'];
$subject = 'Reset your Password';
$link = '<a href = "http://example.com/reset-pass-land.php?username='.$row['username'].'&user_check='.$row['user_hash'].'">http://bquotes.xyz/reset-pass-land.php?username='.$row['username'].'&user_check='.$row['user_hash'].'</a>';
$message = 'Reset your password here: '.$link.' If you cannot click on the link, copy it and paste in your browser address bar';
$sql = $db->prepare('select email from users where email = :email');
$sql->bindParam(':email', $email);
$sql->execute();
$sql->setFetchMode(PDO::FETCH_ASSOC);
$row = $sql->fetch();

//require_once ('class.phpmailer.php');
require 'PHPMailerAutoload.php';

$mail = new PHPMailer;

$mail->SMTPDebug = 3; // Enable verbose debug output

$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'mail.domain.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user@domain.com'; // SMTP username
$mail->Password = 'pass'; // SMTP password
//$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('user@domain.com', 'BQuotes Webmaster');
//$mail->addAddress($email); // Add a recipient
/*$mail->addAddress('ellen@example.com'); // Name is optional
$mail->addReplyTo('info@example.com', 'Information');
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');

$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); */ // Optional name
$mail->isHTML(true); // Set email format to HTML

$mail->Subject = $subject;
$mail->Body = $message;
//$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

/*if(!$mail->send()) {
//echo '<em>Message could not be sent. Mailer Error:</em> ', "<em>", $mail->ErrorInfo, "</em>";
echo '<span class = "red"><em>Message could not be sent. You have one or more invalid fields.</em></span>';
} else {
echo '<span class = "green"><em>Message has been sent. Please check your Spam folders if not in Inbox by 10 minutes.</em></span>';
}*/

if (($email)&&($username)&&!empty($_POST['captcha'])&&($_POST['captcha']==$_SESSION['code'])&&($mail->send())){
echo '<span class = "green"><em>Message has been sent. Please check your Spam folders if not in Inbox by 10 minutes.</em></span>';
}

else {echo '<span class = "red"><em>Message could not be sent. You have one or more invalid fields.</em></span>';
}

echo "<hr>";
echo '<a href = "browse.php?page='.$row0['id'].'">Browse</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href = "index.php">Home</a>';
?>
</html>

谢谢!

最佳答案

PHPMailer 的正确设置:

<?php

$mail = new PHPMailer;
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Username = "user@domain.com";
$mail->Password = "xxxx";
$mail->SMTPSecure = "ssl";
$mail->Host = "smtp.domain.com";
$mail->Port = "465";
$mail->setFrom('from@domain.com', 'John Doe');
$mail->addReplyTo('from@domain.com', 'John Doe');
$mail->AddAddress("mailto@otherdomain.com", "Jane Smith");
$mail->CharSet = 'UTF-8';
$mail->IsHTML(true);
$mail->Subject = 'Bla bla or from DB';
$mail->Body = 'Here come the content...';
if (!$mail->send()) {
echo 'Error: ' . $mail->ErrorInfo;
exit;
}

?>

关于PHP SMTP 邮件脚本不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50887278/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com