Nie chciało mi się za bardzo opisywać jak to zrobić, więc naszkicowałem kawałek kodu, wydaje mi się, że zadziała. Konfiguracja poprzez factories.yml lub bezpośrednio przez php, jak wolisz.
Tworzysz nowy transport:
class MultipleTransports implements Swift_Transport
{
protected
$transports = array(); protected $currentTransportName = '';
public function isStarted($transportName = null)
{
return $this->getTransport($transportName)->isStarted();
}
public function start($transportName = null)
{
return $this->getTransport($transportName)->start();
}
public function stop($transportName = null)
{
return $this->getTransport($transportName)->stop();
}
public function send(Swift_Mime_Message $message, &$failedRecipients = null, $transportName = null)
{
$this->getTransport($transportName)->send($message, $failedRecipients);
}
public function registerPlugin(Swift_Events_EventListener $plugin)
{
foreach($this->transports as $transport)
{
$transport->registerPlugin($plugin);
}
}
public function setTransports($transports)
{
foreach($transports as $name => $parameters)
{
//wiem, ze nie tak, ale to tylko szkic
$dispatcher = sfContext::getInstance()->getEventDispatcher();
$mailer = new sfMailer($dispatcher, $paramters);
$this->transports[$name] = $mailer->getTransport();
}
}
public function getTransport($name = null)
{
$name = $name ? $name : $this->currentTransportName;
return $this->transports[$name];
}
public function setCurrentTransportName($name)
{
$this->currentTransportName = $name;
}
}
Konfigurujesz:
Kod
mailer:
class: sfMailer
param:
logging: %SF_LOGGING_ENABLED%
charset: %SF_CHARSET%
delivery_strategy: realtime
transport:
class: MultipleTransports
param:
currentTransportName: first
transports:
first:
class: Swift_SmtpTransport
param:
host: localhost
port: 25
encryption: ~
username: ~
password: ~
second:
class: Swift_SmtpTransport
param:
host: localhost
port: 25
encryption: ~
username: ~
password: ~
third:
class: Swift_SmtpTransport
param:
host: localhost
port: 25
encryption: ~
username: ~
password: ~
Używasz:
class someActions extends sfActions
{
public function executeSendMail(sfWebRequest $request)
{
$mailer = $this->getMailer();
$message = $mailer->compose(/* ... */);
$mailer->send($message);
//inny transport
$mailer->getTransport()->setCurrentTransportName('second');
$mailer->send($message);
//z nadpisaną metodą sfMailer::send()
$mailer->send($message, null, 'third');
}
}
Potrzebna jeszcze obsługa błędów ale nie chciało mi się i może tak wygląda czytelniej.