我是用Gandi.net server的,我原本是用XAMPP主機測試網頁都一切正常,但我把網頁搬到Gandi.net server去,就得到訊息:SMTP Error: Could not authenticate.PHPMailer就不能運作了。有人知道解決方法嗎?
以下是我目前的程式碼,是有那裡出錯了嗎?
<?php
use PHPMailer\PHPMailer\PHPMailer;
require_once 'phpmailer/Exception.php';
require_once 'phpmailer/PHPMailer.php';
require_once 'phpmailer/SMTP.php';
$mail = new PHPMailer(true);
$alert = '';
if(isset($_POST['submit'])){
  $name = $_POST['name'];
  $email = $_POST['email'];
  $phone = $_POST['phone'];
  $booking = $_POST['booking'];
  $bookDate = $_POST['bookDate'];
  try{
    $mail->isSMTP();
    $mail->Host = 'smtp.gmail.com';
    $mail->SMTPAuth = true;
    $mail->Username = 'XXXXX@gmail.com';
    $mail->Password = 'XXXXXX'; // Gmail address Password
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
    $mail->Port = '587';
    $mail->setFrom('XXXXX@gmail.com');
    $mail->addAddress('XXXXX@gmail.com');  
    $mail->CharSet = 'UTF-8';
    $mail->isHTML(true);
    $mail->Subject = "客戶預約($name)";
    $mail->Body = "<h3>姓名 : $name <br>電郵: $email <br>電話 : $phone <br>預約服務 : $booking <br>預約時間 : $bookDate </h3>";
    $mail->send();
    $alert = '<div class="alert-success">
                 <span>Message Sent! Thank you for contacting us.</span>
                </div>';
  } catch (Exception $e){
    $alert = '<div class="alert-error">
                <span>'.$e->getMessage().'</span>
              </div>';
  }
}
?>
PHPMailer 的版本?
PHP 版本?
到Gmail的帳戶設定裡,把SMTP給打開吧,且到GOOGLE ONE的安全設定裡,把這台主機的登錄動作"允許"試試
應用程式開發中,以Gmail寄信是不能直接用Gmail密碼的
請在https://myaccount.google.com/u/4/security
先申請Gmail Application Password
參考https://support.google.com/mail/answer/185833?hl=en