【问题标题】:SMTP failed to connect through phpmailer. Given the code belowSMTP 无法通过 phpmailer 连接。鉴于下面的代码
【发布时间】:2018-05-04 13:42:42
【问题描述】:
    <?php
if(!empty($_POST["forgot-password"])){
$conn = mysqli_connect("localhost", "root", "12345", "omar_wacoa");
$condition = "";
if(!empty($_POST["user-login-name"])) 
$condition = " ku_id = '" . $_POST["user-login-name"] . "'";
if(!empty($_POST["user-email"])) {
if(!empty($condition)) {
$condition = " and ";
}
$condition = " email = '" . $_POST["user-email"] . "'";
}
if(!empty($condition)) {
$condition = " where " . $condition;
}
$sql = "Select * from user " . $condition;
$result = mysqli_query($conn,$sql);
$user = mysqli_fetch_array($result);
if(!empty($user)) {
echo "hello";
require_once("forgot-password-recovery-mail.php");
} else {
$error_message = 'No User Found';
}
    }
?>
<link href="demo-style.css" rel="stylesheet" type="text/css">
<script>
function validate_forgot() {
    if((document.getElementById("user-login-name").value == "") && (document.getElementById("user-email").value == "")) {
        document.getElementById("validation-message").innerHTML = "Login name or Email is required!"
        return false;
    }
    return true
}
</script>
<form name="frmForgot" id="frmForgot" method="post" onSubmit="return validate_forgot();">
<h1>Forgot Password?</h1>
<?php if(!empty($success_message)) { ?>
<div class="success_message"><?php echo $success_message; ?></div>
<?php } ?>
<div id="validation-message">
<?php if(!empty($error_message)) { ?>
<?php echo $error_message; ?>
<?php } ?>
</div>
<div class="field-group">
<div><label for="username">Username</label></div>
<div><input type="text" name="user-login-name" id="user-login-name" class="input-field"> Or</div>
</div>  
<div class="field-group">
<div><label for="email">Email</label></div>
<div><input type="text" name="user-email" id="user-email" class="input-field"></div>
</div>
<div class="field-group">
<div><input type="submit" name="forgot-password" id="forgot-password" value="Submit" class="form-submit-button"></div>
    </div>  
</form>

邮件配置.php

<?php
define("PROJECT_HOME","http://localhost/wacoa_f");
define("PORT","587"); // port number
define("MAIL_USERNAME", "user@gmail.com"); // smtp usernmae
define("MAIL_PASSWORD", "ffdg"); // smtp password
define("MAIL_HOST", "smtp.gmail.com"); // smtp host
define("MAILER", "smtp");
define("SENDER_NAME", "Admin");
define("SERDER_EMAIL", "admin@admin.com");
?>

忘记密码恢复邮件.php

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer/vendor/autoload.php';
$mail = new PHPMailer();
//require( 'PHPMailer/PHPMailerAutoload.php' );
require_once("mail_configuration.php");
$body = "<div>" . $user["ku_id"] . ",<br><br><p>Click this link to recover your password<br><a href='" . PROJECT_HOME . "php-forgot-password-recover-code/reset_password.php?name=" . $user["ku_id"] . "'>" . PROJECT_HOME . "php-forgot-password-recover-code/reset_password.php?name=" . $user["ku_id"] . "</a><br><br></p>Regards,<br> Admin.</div>";
$mail->IsSMTP();
$mail->SMTPDebug = 1;
$mail->SMTPAuth = TRUE;
$mail->SMTPSecure = "tls";
$mail->Port     = PORT;  
$mail->Username = MAIL_USERNAME;
$mail->Password = MAIL_PASSWORD;
$mail->Host     = MAIL_HOST;
$mail->Mailer   = MAILER;
//sender
$mail->SetFrom(SERDER_EMAIL, SENDER_NAME);
$mail->ReturnPath=SERDER_EMAIL; 
//recepient
$mail->AddAddress($user["email"]);
$mail->Subject = "Forgot Password Recovery";        
$mail->MsgHTML($body);
$mail->IsHTML(true);
$mail->Body = $body;
$mail->AltBody = strip_tags($body);
if(!$mail->Send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
    $error_message = 'Problem in Sending Password Recovery Email';
} else {
    $success_message = 'Please check your email to reset password!';
}

?>

在这里,我正在尝试提交带有恢复电子邮件的表单,该电子邮件发送链接以重置用户密码。我有用户 PHPMailer。但它给了我错误:

2017-11-21 06:48:03 客户端 -> 服务器:EHLO 本地主机
2017-11-21 06:48:03 客户端 -> 服务器:STARTTLS
SMTP 错误:无法连接到 SMTP 主机。
2017-11-21 06:48:04 客户端 -> 服务器:退出
2017-11-21 06:48:04
SMTP 连接()失败。 https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

【问题讨论】:

  • 你应该正确缩进你的代码。这将帮助我们和您遵循流程并调试代码。现在,阅读起来非常困难。我看到的一件事是你对SQL Injections很开放,应该真正使用Prepared Statements而不是连接你的查询。特别是因为您根本没有逃避用户输入!
  • 试试$mail-&gt;SMTPSecure = "ssl"; 和 PORT = 465
  • ALERT 立即删除您的问题,在代码中将您的用户名和密码更改为 dummy,然后重新发布您的问题。不要与任何人分享用户名和密码
  • 它不工作。我试过了

标签: php email


【解决方案1】:

在邮件 configuration.php 上应该定义 ("SENDER_EMAIL","admin@admin.com");不是

define("SERDER_EMAIL", "admin@admin.com"); & on forgot-password-recovery-mail.php

在 //sender 下那两行应该是 SENDER 而不是 SERDER 它的语法错误,否则它应该可以工作,因为我昨天测试了它

【讨论】:

    猜你喜欢
    • 2018-10-26
    • 1970-01-01
    • 2012-05-24
    • 2018-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多