Ich versuche, PHPMailer so einzurichten, dass einer unserer Kunden automatisch E-Mails von seinem eigenen Konto generieren kann. Ich habe mich bei ihrem Office 365-Konto angemeldet und festgestellt, dass die erforderlichen Einstellungen für PHPMailer sind:
Host: smtp.off ice365.com Port: 587 Auth: tls
Ich habe diese Einstellungen auf PHPMailer angewendet, aber es werden keine E-Mails gesendet (die Funktion, die ich aufrufe, ist für unsere eigenen E-Mails, die von einem externen Server gesendet werden (nicht dem, der die Webseite bedient)).
"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
Wei? jemand, welche Einstellungen erforderlich sind, damit PHPMailer über smtp.offi ce365.com senden kann?
所以我在這個問題上苦苦掙扎。對于具有 Exchange Online 和 Microsoft 管理中心訪問權(quán)限的企業(yè)帳戶,我可以提供此問題的答案。
TLDR:轉(zhuǎn)到管理中心并選擇您要發(fā)送郵件的用戶。然后在設(shè)置“經(jīng)過身份驗(yàn)證的 SMTP”之后查看“電子郵件”和“電子郵件應(yīng)用程序”后的設(shè)置,只需啟用它即可。
仍然無法工作?我已經(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,請確保使用 https://stackoverflow.com/ 中提到的應(yīng)用密碼a/61359150/14148981
運(yùn)行腳本
我希望這對某人有幫助。我花了很長時間才找到這個選項(xiàng)。
獲取應(yīng)用密碼和身份驗(yàn)證的所有步驟摘要:
@nitin 的代碼對我不起作用,因?yàn)樗?SMTPSecure 參數(shù)中缺少“tls”。
這是一個工作版本。我還添加了兩行注釋掉的行,您可以在出現(xiàn)問題時使用它們。
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'; }