首頁  >  問答  >  主體

PHP郵件傳送器無法正常運作:沒有錯誤日誌,訊息顯示已傳送,但未收到

我的網站伺服器的 php 錯誤日誌中沒有收到任何錯誤日誌。我嘗試過多個不同的 SMTP 伺服器(我知道的其他電子郵件提供者也可以使用)。這是我的 HTML 表單:

<form action="/mailfunction.php" method="post" id="contact-form">
      <div class="nameInput">
      <input id="boxes" type="text" id="fname" name="name" value="" class="formFormL" placeholder="Name" maxlength="50"></input>
      </div>
      <div class="emailInput">
      <input id="boxes" type="text" id="lname" name="email" value="" class="formFormR" placeholder="Email" maxlength="50"></input>
      </div>

      <div class="messageInput">
      <textarea id="boxes" id="fname" name="message" value="" class="formFormM" placeholder="Message" maxlength="1000"></textarea>
      </div>
      <div style="padding: 5px;">
      <button type="submit" value="Send" class="up" name="submit">Send</button>
      </div>
    </form>

這是我的 mailfunction.php,它在公共 html 之外呼叫我的郵件程式函數。

<?php 
require '[REDACTED]/mailer.php';

$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];

mails($name, $email, $message);

?>

這是我的郵件功能。

<?php
//Import PHPMailer classes into the global namespace
//These must be at the top of your script, not inside a function
use PHPMailerPHPMailerPHPMailer;
use PHPMailerPHPMailerSMTP;
use PHPMailerPHPMailerException;

//Load Composer's autoloader
require 'vendor/autoload.php';

//Create an instance; passing `true` enables exceptions
$mail = new PHPMailer(true);

function mails($name, $email, $message) {

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

        try{
            $mail = new PHPMailerPHPMailerPHPMailer();
            $mail->isSMTP();
            $mail->Host = 'smtp.gmail.com';
            $mail->SMTPAuth   = true; 
            $mail->Username   = '[REDACTED]'; 
            $mail->Password   = '[REDACTED]'; 
            $mail->SMTPSecure = 'SSL'; 
            $mail->Port       = 465;  

            $mail->setFrom('[REDACTED]');
            $mail->addAddress('[REDACTED]');

            $mail->isHTML(true);                                  
            $mail->Subject = 'Message Received (Contact Page)';
            $mail->Body    = '<h3>Name : $name <br>Email: $email <br>Message : $message</h3>';
            $mail->send();
            echo 'Message has been sent';
            } catch (Exception $e) {
                echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
            }
    }
}

?>

沒有訊息被發送。這裡可能有什麼問題? 謝謝。

錯誤: 2022-07-07 01:16:57 SMTP 錯誤:無法連線到伺服器:連線被拒絕 (111) SMTP 連線()失敗。 https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

#
P粉593118425P粉593118425318 天前657

全部回覆(1)我來回復

  • P粉030479054

    P粉0304790542023-11-11 10:28:42

    我沒有發現您的程式碼有問題。該錯誤可能是由於密碼設定不正確造成的。嗯,授權程序可能不正確。當我遇到同樣的錯誤時,我確定這就是問題所在並解決了它。您可以嘗試觀看此影片來解決該問題嗎? (觀看到 5:20 將會足以解決您的問題。)

    回覆
    0
  • 取消回覆