Heim > Fragen und Antworten > Hauptteil
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?
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'; }