2017-06-20 109 views
0

一旦表單提交了正確的信息,就會出現一條消息,通知用戶電子郵件已發送。但是由於某些原因,電子郵件沒有被用戶接收。PHP電子郵件正常工作,但實際上並未被用戶收到

我已經在網上查過,但無濟於事。有人可以幫助我解決這個問題嗎?

以下是HTML表單和PHP的代碼。

<form method="POST" action="contact.php"> 
        <div class="row"> 
         <div class="col-sm-6 form-group"> 
          <input class="form-control" id="name" name="name" placeholder="Name" type="text" required> 
         </div> 
         <div class="col-sm-6 form-group"> 
          <input class="form-control" id="email" name="email" placeholder="Email" type="email" required> 
         </div> 
        </div> 
        <textarea class="form-control inputStyle" id="comments" name="comments" placeholder="Comment" rows="6" required></textarea> 
        <br> 
        <div class="row"> 
         <div class="col-sm-12 form-group"> 
          <button class="btn btn-default pull-right" name="submit" type="submit">Send</button> 
         </div> 
        </div>     

         <!-- Contacting Support Team --> 
         <?php 

          if(isset($_POST['submit'])){ 
           $email = $_POST['email']; 
           $name = $_POST['name']; 
           $comment = $_POST['comments']; 

           if(!empty($email) && !empty($name) && !empty($comment)){ 

            require 'PHPMailer-master/PHPMailerAutoload.php'; 

            $mail = new PHPMailer; 

            $mail->Host = 'ssl://smtp.gmail.com';     // Specify main and backup SMTP servers 
            $mail->Port = 465; 
            $mail->SMTPAuth = true;        // Enable SMTP authentication 
            $mail->Username = '*****';     // SMTP username 
            $mail->Password = '*****';       // SMTP password 
            $mail->SMTPSecure = 'tls';       // Enable encryption, 'ssl' also accepted 

            $mail->From = '*****';; 
            $mail->addAddress($email);       // Name is optional 
            $mail->isHTML(true);         // Set email format to HTML 

            $mail->Subject = 'Test'; 
            $mail->Body = '$comment'; 
            $mail->AltBody = '$comment'; 

            if(!$mail->send()) { 
             echo '<div class="alert alert-danger alert-dismissible" id="myAlert"> 
             <a href="#"class="close">&times;</a> 
             <strong> Email not sent </strong> Something went wrong. 
             </div>'; 

             echo 'Mailer Error: ' . $mail->ErrorInfo; 
            } else { 
             echo '<div class="alert alert-success alert-dismissible" id="myAlert"> 
             <a href="#"class="close">&times;</a> 
             <strong> Email sent!</strong> We will contact you as soon as possible. 
             </div>'; 
            } 
           } 
          } 
         ?> 
       </form>  

在此先感謝!

+3

發送電子郵件並不能保證收到的電子郵件中。該代碼可能正在發送它,但也許您設置的郵件服務器沒有轉發它,或者遠程郵件服務器拒絕了它,或者用戶的垃圾郵件過濾器已將其清除或放入其垃圾文件夾中。可能有許多原因。你所能做的只是檢查郵件是否被正確解析和形成,而且你必須使用郵件服務器正確地進行身份驗證。 FWIW我從來沒有見過像'ssl:// ...'之類的郵件主機。有一些文件告訴你這樣做嗎? – ADyson

+0

嗨,ADyson,其實。我在網上搜索了很長時間,發現了一些類似的代碼片段。我應該嘗試改變它嗎? – user7943

+0

首先嚐試添加'$ mail-> isSMTP();'命令以確保PHPMailer知道使用SMTP。同時設置'$ mail-> SMTPDebug = 3;'以獲得來自PHPMailer的詳細輸出。如果這不起作用,那就試着丟掉'ssl://'部分來幽默我。 – ADyson

回答

0

你設置

$mail->SMTPSecure = 'tls';

但端口465是Gmail的SSL端口。 587是TLS端口。因此,無論切換端口,或者改變使用

$mail->SMTPSecure = 'ssl';

相關問題