簡體   English   中英

即使包含 SMTP.php,PHPMailer 也不發送郵件

[英]PHPMailer doesn't send mail even after including SMTP.php

出於某種奇怪的原因,我的 PHP 登錄系統發送確認 email 以激活您的帳戶,但它沒有發送任何內容。

<?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

它讓我很緊張。 識別問題。 如果有人知道解決方案或知道為什么它不起作用,那就太好了。 謝謝。 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();

} 

所以一旦你得到你的錯誤日志,你可能可以做一個谷歌或粘貼你的錯誤日志,也許我們可以提供幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM