設定 PHPMailer 以使用 Off ice365 SMTP
P粉510127741
P粉510127741 2023-10-16 10:39:47
0
2
454

我正在嘗試設定 PHPMailer,以便我們的一位客戶能夠從自己的帳戶中自動產生電子郵件。我登入了他們的Of  fice 365帳戶,發現PHPMailer所需的設定是:

Host: smtp.off   ice365.com
Port: 587
Auth: tls

我已將這些設定套用至 PHPMailer,但沒有發送電子郵件(我呼叫的函數適用於我們自己的郵件,該郵件是從外部伺服器(不是服務網頁的伺服器)發送的)。

"host"      => "smtp.of   fice365.com",
"port"      => 587,
"auth"      => true,
"secure"    => "tls",
"username"  => "clientemail@off  ice365.com",
"password"  => "clientpass",
"to"        => "myemail",
"from"      => "clientemail@of  fice365.com",
"fromname"  => "clientname",
"subject"   => $subject,
"body"      => $body,
"altbody"   => $body,
"message"   => "",
"debug"     => false

有誰知道PHPMailer透過smtp.offi   ce365.com寄送需要什麼設定?

P粉510127741
P粉510127741

全部回覆(2)
P粉176151589

更新:2022 年 5 月

所以我對這個問題苦苦掙扎。對於具有 Exchange Online 和 Microsoft 管理中心存取權限的企業帳戶,我可以提供此問題的答案。

TLDR:前往管理中心並選擇您要傳送郵件的使用者。然後在設置“經過身份驗證的 SMTP”之後查看“電子郵件”和“電子郵件應用程式”後的設置,只需啟用它即可。

仍然無法工作?我已經為您提供了幫助,這就是我如何讓它完全發揮作用。

  1. 使用PHP Composer,實際上節省了很多工作。
  2. 將您的程式碼替換為我的程式碼,並在測試後更改
SMTPDebug = SMTP::DEBUG_off;

//SMTP
$mail = new PHPMailer(true); //important
$mail->CharSet = 'UTF-8';  //not important
$mail->isSMTP(); //important
$mail->Host = 'smtp.office365.com'; //important
$mail->Port       = 587; //important
$mail->SMTPSecure = 'tls'; //important
$mail->SMTPAuth   = true; //important, your IP get banned if not using this

//Auth
$mail->Username = '[email protected]';
$mail->Password = 'your APP password';//Steps mentioned in last are to create App password

//Set who the message is to be sent from, you need permission to that email as 'send as'
$mail->SetFrom('[email protected]', 'Hosting Group Inc.'); //you need "send to" permission on that account, if dont use [email protected]

//Set an alternative reply-to address
$mail->addReplyTo('[email protected]', 'First Last');

//Set who the message is to be sent to
$mail->addAddress('[email protected]', 'SIMON MÜLLER');
//Set the subject line
$mail->Subject = 'PHPMailer SMTP test';
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->msgHTML(file_get_contents('replace-with-file.html'), __DIR__);  //you can also use $mail->Body = "This is a body message in html" 
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
//$mail->addAttachment('../../../images/phpmailer_mini.png');

//send the message, check for errors
if (!$mail->send()) {
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
}
  1. 這可能看起來像您的文件,沒關係,但現在是簡單棘手的部分。與 Google 一樣,Microsoft 也為 SMTP 實作了「開關」。只需從您的企業帳戶前往您的管理中心,或請有權限的人執行該部分操作即可:
  • 導覽至https://admin.microsoft.com/AdminPortal/Home#/users
  • 選擇您要從中傳送電子郵件的使用者
  • 在“電子郵件”標籤下搜尋“電子郵件應用程式”,點擊“管理電子郵件應用程式”
  • 您可以在此選擇“經過驗證的 SMTP”,確保選取該選項並儲存變更
  1. 如果您使用 MFA,請確保使用 https://stackoverflow.com/ 中提到的應用程式密碼a/61359150/14148981

  2. #運行腳本

我希望這對某人有幫助。我花了很長時間才找到這個選項。

取得應用程式密碼和驗證的所有步驟摘要:

  1. #使用管理員帳號:
  • Active Directory AD -> 屬性 -> 安全性預設值:關閉。
  • Active Directory 入口網站 -> 條件存取 -> 設定 MFA 可信任 IP -> 允許使用者應用程式密碼:啟用
  • 管理頁面使用者清單 -> 目標使用者的「多重驗證」:啟用然後強制
  • 管理頁面使用者清單 -> 使用者詳細資料 -> 郵件 -> 「管理電子郵件應用程式」 -> 「經過驗證的 SMTP」:啟用
  1. 使用使用者帳戶:
  • 使用者帳戶設定檔 -> 安全性 -> 新增登入方法:套用密碼
  1. PHP 郵件程式設定:
  • smtp.office365.com、587、tls、電子郵件、appPassword
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!