php - 即使包含 SMTP.php,PHPMailer 也不发送邮件
问题描述
出于某种奇怪的原因,我的 PHP 登录系统发送了一封确认电子邮件以激活您的帐户,但它没有发送任何内容。
<?php
$error = NULL;
require "phpmailer/class.phpmailer.php";
require "phpmailer/class.smtp.php";
require "phpmailer/PHPMailerAutoload.php";
if(isset($_POST['submit'])) {
//Form Data GET
$u = $_POST['u'];
$p = $_POST['p'];
$p2 = $_POST['p2'];
$e = $_POST['e'];
//Throw Username too short error
if(strlen($u) < 5){
$error = "(!) Your username must be at least 5 characters.";
}elseif($p2 != $p) {
//Throw password pair error.
$error .= "(!) Your passwords do not match :/";
}else{
//200 [OK]
//Establish Connection to Database
$mysqli = NEW MySQLi('localhost','root','','test');
//Convert special chars.
$u = $mysqli->real_escape_string($u);
$p = $mysqli->real_escape_string($p);
$p2 = $mysqli->real_escape_string($p2);
$e = $mysqli->real_escape_string($e);
//Generate Verification Key
$vkey = md5(time().$u);
//Insert account into database
$p = md5($p);
$insert = $mysqli->query("INSERT INTO accounts(username,password,email,vkey)
VALUES('$u','$p','$e','$vkey')");
if($insert){
//Start PHPMailer
$mail = new PHPMailer(true);
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPDebug = 0; //No Debug. Set to 3 for verbose logging.
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; //Set to SSL to pass checks.
$mail->Host = "smtp.mail.com"; //Set to mail.com server, it has ssl, smtp, and it's free. If you'd like to use gmail, use smtp.gmail.com
$mail->Port = 465; //SSL/TLS Port for mail.com and gmail.com
//FILL WITH YOUR DETAILS
$mail->Username = 'example@mail.com';
$mail->Password = 'example';
//DON'T SET TO SENDER ADDRESS WILL FAIL SPF CHECKS!!!
$mail->SetFrom('example@mail.com', 'example');
$mail->AddAddress($e);
//Send the email.
$mail->Subject = trim("Email Verifcation");
$mail->isHTML(true);
//The Message
$mail->Body = '<h1>Hi, ' . $u . '!</h1><br><p><a href="https://localhost' . $vkey . '">Activate</a><br><p>Alternatively copy and paste this link in your browser: <br> <b>Https://localhost' . $vkey . '';
echo "<center><div class='alert success'><strong>Successfully Registered!</strong> Please check your email.</div></center>";
}
//OOPS! Throw $error.
echo $mysqli->error;
}
}
?>
一开始,我需要 PHPMailer 文件夹中的文件,然后我启动 PHPMailer
它让我很紧张。Idk 问题。如果有人知道解决方案或知道为什么它不起作用,那就太好了。谢谢。同一文件中的 HTML
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title></title>
<style>
.alert {
padding: 20px;
background-color: #f44336;
color: white;
opacity: 1;
transition: opacity 0.6s;
margin-bottom: 15px;
font-family: Sans-Serif;
border-radius: 15px;
}
.alert.success {background-color: #4CAF50;}
.alert.info {background-color: #2196F3;}
.alert.warning {background-color: #ff9800;}
.closebtn {
margin-left: 15px;
color: white;
font-weight: bold;
float: right;
font-size: 22px;
line-height: 20px;
cursor: pointer;
transition: 0.3s;
}
.closebtn:hover {
color: black;
}
</style>
</head>
<body>
<form method="POST" action="">
<table border="0" align="center" cellpadding="5">
<tr>
<td align="right">Username:</td>
<td><input type="TEXT" name="u" required/></td>
</tr>
<tr>
<td align="right">Password</td>
<td><input type="PASSWORD" name="p" required/></td>
</tr>
<tr>
<td align="right">Repeat Password</td>
<td><input type="PASSWORD" name="p2" required/></td>
</tr>
<tr>
<td align="right">Email Address</td>
<td><input type="EMAIL" name="e" required/></td>
</tr>
<td colspan="2" align="center"><input type="SUBMIT" name="submit" value="Register" required/></td>
</table>
</form>
<center>
<?php
echo $error ;
?>
</center>
</body>
</html>
解决方案
你可以试试这个我也有一个错误所以我不得不在主机之前添加这一行
$mail->SMTPOptions=array('ssl'=>array('verify_peer'=>false,'verify_peer_name'=>false,'allow_self_signed'=>false));
但如果这不起作用,您可以尝试像这样进行故障排除
if($mail->Send()){
}else{
var_dump($mail);
die();
}
所以一旦你得到你的错误日志,你可能可以做一个谷歌或粘贴你的错误日志,也许我们可以提供帮助
推荐阅读
- java - How to write a lambda expression when you are using a string array?
- python - 在google Big-query中设置IAM策略时如何检查用户是否存在?
- python - 在 python 中使用地理空间数据
- git - 为什么我不能在不指定分支的情况下将 HEAD^ 推送到原点?
- ios - 我们是否需要 iOS Xcode 中的 Storyboard ID
- java - 当我在移动设备上同时在网站上运行自动化测试时,如何仅捕获一个屏幕截图?
- ssl - 将 Nexus-IQ 配置为使用 .pfx 证书
- java - 使用 JUnit 测试 Java 服务器-客户端应用程序(或尝试)
- r - 根据多列中的多个条件创建新列
- c# - 反序列化 Json 并访问结果