我正在嘗試設(shè)定 PHPMailer,以便我們的一位客戶能夠從自己的帳戶中自動(dòng)產(chǎn)生電子郵件。我登入了他們的Of? fice 365帳戶,發(fā)現(xiàn)PHPMailer所需的設(shè)定是:
Host: smtp.off ice365.com Port: 587 Auth: tls
我已將這些設(shè)定套用至 PHPMailer,但沒(méi)有發(fā)送電子郵件(我呼叫的函數(shù)適用於我們自己的郵件,該郵件是從外部伺服器(不是服務(wù)網(wǎng)頁(yè)的伺服器)發(fā)送的)。
"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
有誰(shuí)知道PHPMailer透過(guò)smtp.offi? ?ce365.com寄送需要什麼設(shè)定?
所以我對(duì)這個(gè)問(wèn)題苦苦掙扎。對(duì)於具有 Exchange Online 和 Microsoft 管理中心存取權(quán)限的企業(yè)帳戶,我可以提供此問(wèn)題的答案。
TLDR:前往管理中心並選擇您要傳送郵件的使用者。然後在設(shè)置“經(jīng)過(guò)身份驗(yàn)證的 SMTP”之後查看“電子郵件”和“電子郵件應(yīng)用程式”後的設(shè)置,只需啟用它即可。
仍然無(wú)法工作?我已經(jīng)為您提供了幫助,這就是我如何讓它完全發(fā)揮作用。
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,請(qǐng)確保使用 https://stackoverflow.com/ 中提到的應(yīng)用程式密碼a/61359150/14148981
#運(yùn)行腳本
我希望這對(duì)某人有幫助。我花了很長(zhǎng)時(shí)間才找到這個(gè)選項(xiàng)。
取得應(yīng)用程式密碼和驗(yàn)證的所有步驟摘要:
@nitin 的程式碼對(duì)我來(lái)說(shuō)不起作用,因?yàn)樗?SMTPSecure 參數(shù)中缺少「tls」。
這是一個(gè)工作版本。我還添加了兩行註解掉的行,您可以在出現(xiàn)問(wèn)題時(shí)使用它們。
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'; }