如果你看一看的\Illuminate\Mail\TransportManager
類,在createSmtpDriver()
方法,你會發現其中的SMTP設置在從配置拉昇。在您的配置中,如果您將驅動程序設置爲smtp
,則Laravel將調用此方法。
Laravel Mailer
類包含一個公共方法來設置Swift Mailer實例。所以,你基本上可以創建自己的實例,並將通過您在發送郵件前(我沒有測試過這一點):
public function yourMethod(Mailer $mailer)
{
$transport = Swift_SmtpTransport::newInstance(
'YourHost', 'YourPort'
);
$transport->setUsername('YourPassword');
$transport->setPassword('YourPassword');
$swift = new Swift_Mailer($transport);
$mailer->setSwiftMailer($swift);
$mailer->send(...);
}
通常,如果你想介紹一些自定義的功能集成到像梅勒一個Laravel類,你將能夠定義你自己的驅動程序(在這種情況下你自己的傳輸),並使用Laravel的API來擴展它並添加你自己的發送郵件的方法。
同樣,我沒有測試過這一點,但你可能延長TransportManager
類添加自己的驅動程序。你可以把下面的代碼說AppServiceProvider
:
$manager = app('swift.transport'); // Will get you an instance of TransportManager
$manager->extend('customsmtp', function() {
// Your custom code to create your own transport for the mailer. Just like the createSmtpDriver method in TransportManager.
$transport = Swift_SmtpTransport::newInstance(
'YourHost', 'YourPort'
);
$transport->setUsername('YourPassword');
$transport->setPassword('YourPassword');
return $transport;
});
謝謝,會試試! – user1365447