我正在努力使用 Exchange 在線帳戶從網(wǎng)絡(luò)發(fā)送郵件。最近,在遷移到 Laravel 時(shí),我發(fā)現(xiàn)與 PHPMailer 配合良好的現(xiàn)有設(shè)置不適用于基于 SwiftMailer 的 Laravel。PHPMailer 的工作原理:$data = new PHPMailer(true);$data->CharSet = 'UTF-8';$data->isSMTP();$data->Host = config('mail.host');$data->SMTPAuth = true; // apparently this line does the trick$data->Username = config('mail.username');$data->Password = config('mail.password');$data->SMTPSecure = config('mail.encryption');$data->Port = config('mail.port');$data->setFrom('site@mydomain.com','site mailer');$data->addAddress('me@mydomain.com', 'Me');$data->Subject = 'Wonderful Subject PHPMailer';$data->Body = 'Here is the message itself PHPMailer';$data->send();與 SwiftMailer 相同的邏輯:$transport = (new Swift_SmtpTransport(config('mail.host'), config("mail.port"), config('mail.encryption'))) ->setUsername(config('mail.username')) ->setPassword(config('mail.password'));$mailer = new Swift_Mailer($transport);$message = (new Swift_Message('Wonderful Subject')) ->setFrom(['site@mydomain.com'=>'site mailer']) ->setTo(['me@mydomain.com'=>'Me']) ->setBody('Here is the message itself');$numSent = $mailer->send($message);SwiftMailer,給出錯(cuò)誤:530 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM [xxxxxxxxxxxxx.xxxxxxxx.prod.outlook.com]兩種情況下的 SMTP 服務(wù)器smtp-mail.outlook.com和端口 587 相同NB是的,我很清楚其他地方使用的建議mydomain-com.mail.protection.outlook.com和端口 25。但是這樣做后,垃圾郵件中會(huì)收到消息,原因是“我們無法驗(yàn)證發(fā)件人的身份”,這是我無法接受的行為。我們談?wù)摰氖巧倭?,所以?duì)其他/第 3 方群發(fā)郵件服務(wù)不感興趣。到目前為止,我的發(fā)現(xiàn)是,這$phpMailer->SMTPAuth = true;改變了游戲規(guī)則。如果沒有這一行,它會(huì)產(chǎn)生與 SwiftMailer 相同的錯(cuò)誤。 問題是如何強(qiáng)制執(zhí)行相同的行為SwiftMailer?如前所述,我實(shí)際上使用了 Laravel 郵件,但為了這個(gè)示例的目的,我直接提取了 SwiftMailer 調(diào)用。編輯: SwiftMailer 有$transport->setAuthMode(),它應(yīng)該與$phpMailer->AuthType. 為兩者嘗試了可用的 CRAM-MD5、LOGIN、PLAIN、XOAUTH2 值。PHPMailer 與所有這些一起工作,除了 XOAUTH2。對(duì)于 SwiftMailer,這些都沒有改變?nèi)魏螙|西,仍然給出錯(cuò)誤。EDIT2:我確實(shí)有 SPF 記錄(DNS TXT)v=spf1 include:spf.protection.outlook.com -all已解決:添加tls到 Swift 運(yùn)輸結(jié)構(gòu)中。顯然PHPMailer的默認(rèn)設(shè)置為TLS,因?yàn)閏onfig('mail.encryption')是null。
SwiftMailer 像 PHPMailer 一樣強(qiáng)制 SMTPAuth
寶慕林4294392
2022-01-02 17:25:33