首頁  >  問答  >  主體

PHP 郵件程式在本機上運作還是這裡有問題

嘗試使用 PHP 郵件程式 (localhost) 發送電子郵件時,我不斷收到錯誤訊息。或者 php 郵件程式無法在本機上運行嗎?

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

require 'src/Exception.php';
require 'src/PHPMailer.php';
require 'src/SMTP.php';


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

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

    try {
        //Server settings
        $mail->SMTPDebug = SMTP::DEBUG_SERVER;                      //Enable verbose debug output
        $mail->isSMTP();                                            //Send using SMTP
        $mail->Host       = 'smtp.gmail.com';                     //Set the SMTP server to send through
        $mail->SMTPAuth   = true;                                   //Enable SMTP authentication
        $mail->Username   = 'default@gmail.com';                     //SMTP username
        $mail->Password   = '00000120';                               //SMTP password
        // $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;            //Enable implicit TLS encryption
        $mail->Port       = 465;                                    //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`

        //Recipients
        $mail->setFrom('NatsuDragneelxd42069@gmail.com', 'Mailer');
        $mail->addAddress('Received@gmail.com', 'Joe User');     //Add a recipient
        //$mail->addAddress('ellen@example.com');               //Name is optional
        $mail->addReplyTo('Noreply@gmail.com', 'Info');
        // $mail->addCC('cc@example.com');
        // $mail->addBCC('bcc@example.com');

        //Attachments
        // $mail->addAttachment('/var/tmp/file.tar.gz');         //Add attachments
        // $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    //Optional name

        //Content
        $mail->isHTML(true);                                  //Set email format to HTML
        $mail->Subject = 'Here is the subject';
        $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
        $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

        $mail->send();
        echo 'Message has been sent';
    } catch (Exception $e) {
        echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
    }

這是我得到的錯誤:

伺服器-> 客戶端: SMTP 錯誤:無法連線到 SMTP 主機。 Message could not be sent. Mailer Error: SMTP 錯誤:無法連線到 SMTP 主機。

P粉969253139P粉969253139211 天前327

全部回覆(1)我來回復

  • P粉364129744

    P粉3641297442024-02-26 11:12:42

    我不知道你為什麼註解掉這一行,但它會使連接失敗,因為它將嘗試與需要加密的連接埠建立未加密的連接:

    // $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;

    取消註釋,您可能會更幸運。您可能還想嘗試設定 SMTPDebug = SMTP::DEBUG_CONNECTION,因為它會為您提供有關連接的 TLS 階段的更多資訊。

    這可能無法解決您的整個問題,因為 Gmail(自 2022 年 5 月起)不再允許使用常規 ID 和密碼進行身份驗證。您需要切換到使用 XOAUTH2(PHPMailer 支援),或在 Gmail 控制台中建立應用程式密碼。

    另請注意,gmail 不允許您使用任意位址,只能使用您的使用者名稱 位址和預定義別名。

    PHPMailer 故障排除指南中涵蓋了所有這些內容。

    回覆
    0
  • 取消回覆