动态更改 SwiftMailer 中的 smtp 设置 [英] Changing smtp settings in SwiftMailer dynamically
问题描述
我在 Symfony 2 中使用 SwiftMailer 包.我在 config.yml 文件中传递了我的 smtp 用户/密码设置,它工作得很好,但是当我发送邮件时,我需要从数据库中获取此设置.我可以访问这个参数:
I'm using SwiftMailer bundle with Symfony 2. I pass my smtp user/password settings in config.yml file, it works great, but I need to take this settings from database, when I'm sending mail. I can acces this params:
$mailer = $this->getContainer()->get('mailer')->getTransport();
但是可以在运行时更改它们吗?我没有看到任何 setter 方法.非常感谢!
But is it possible to change them on runtime ? I dont see any setter methods. many thanks!
推荐答案
非常感谢,但这不是我想要的解决方案,在内核请求时我不知道我将使用哪个帐户.我需要更改发送邮件循环中的设置.我找到了很酷的解决方案:
Many thanks, but it's not the solution i was looking, on kernel request I don't know which account I'll use. I needed to change settings inside my send mail loop. I found pretty cool solution:
foreach ($locations as $location) {
// get settings for account
$user = $location->getSmtpUser();
$pass = $location->getSmtpPass();
// switch to new settings
$transport = $this->getContainer()->get('mailer')->getTransport();
$ext = $transport->getExtensionHandlers();
$auth_handler = $ext[0];
$auth_handler->setUserName($user);
$auth_handler->setPassword($pass);
// send message using new settings
$message = \Swift_Message::newInstance()
->setSubject( $subject )
->setFrom( $from )
->setTo( $email )
->setBody( $body )
->setContentType('text/html');
$this->getContainer()->get('mailer')->send( $message );
}
这篇关于动态更改 SwiftMailer 中的 smtp 设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!