我正在嘗試設定 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粉1761515892023-10-17 10:00:21
所以我對這個問題苦苦掙扎。對於具有 Exchange Online 和 Microsoft 管理中心存取權限的企業帳戶,我可以提供此問題的答案。
TLDR:前往管理中心並選擇您要傳送郵件的使用者。然後在設置“經過身份驗證的 SMTP”之後查看“電子郵件”和“電子郵件應用程式”後的設置,只需啟用它即可。
仍然無法工作?我已經為您提供了幫助,這就是我如何讓它完全發揮作用。
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 = 'yourname@mail.org'; $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('hosting@mail.org', 'Hosting Group Inc.'); //you need "send to" permission on that account, if dont use yourname@mail.org //Set an alternative reply-to address $mail->addReplyTo('no-reply@mail.com', 'First Last'); //Set who the message is to be sent to $mail->addAddress('customer@othermail.com', '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 { }
如果您使用 MFA,請確保使用 https://stackoverflow.com/ 中提到的應用程式密碼a/61359150/14148981
#運行腳本
我希望這對某人有幫助。我花了很長時間才找到這個選項。
取得應用程式密碼和驗證的所有步驟摘要:
P粉3950561962023-10-17 00:05:06
@nitin 的程式碼對我來說不起作用,因為它在 SMTPSecure 參數中缺少「tls」。
這是一個工作版本。我還添加了兩行註解掉的行,您可以在出現問題時使用它們。
isSMTP(); $mail->Host = 'smtp.office365.com'; $mail->Port = 587; $mail->SMTPSecure = 'tls'; $mail->SMTPAuth = true; $mail->Username = 'somebody@somewhere.com'; $mail->Password = 'YourPassword'; $mail->SetFrom('somebody@somewhere.com', 'FromEmail'); $mail->addAddress('recipient@domain.com', 'ToEmail'); //$mail->SMTPDebug = 3; //$mail->Debugoutput = function($str, $level) {echo "debug level $level; message: $str";}; //$mail->Debugoutput = 'echo'; $mail->IsHTML(true); $mail->Subject = 'Here is the subject'; $mail->Body = 'This is the HTML message body in bold!'; $mail->AltBody = 'This is the body in plain text for non-HTML mail clients'; if(!$mail->send()) { echo 'Message could not be sent.'; echo 'Mailer Error: ' . $mail->ErrorInfo; } else { echo 'Message has been sent'; }