Skip to content

Commit

Permalink
Switch to dynamic transport system.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmhobbs committed Sep 21, 2011
1 parent b03e90c commit 8b471ee
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 37 deletions.
44 changes: 7 additions & 37 deletions classes/kohana/mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,44 +224,14 @@ public function connect( $config = "default" )
// Load configuration
$config = Kohana::config('mailer.'.$config);

$transport = $config['transport'];
$transport = ( is_null( $config['transport'] ) ) ? 'native' : $config['transport'];
$config = $config['options'];

switch ( $transport )
{
case 'smtp':

//Create the Transport
$transport = Swift_SmtpTransport::newInstance()
->setHost(empty($config['hostname']) ? "localhost" : (string) $config['hostname'])
->setUsername(empty($config['username']) ? NULL : (string) $config['username'])
->setPassword(empty($config['password']) ? NULL : (string) $config['password']);

//Port?
$port = empty($config['port']) ? 25 : (int) $config['port'];
$transport->setPort($port);

//Use encryption?
if (! empty($config['encryption']))
{
$transport->setEncryption($config['encryption']);
}

break;

case 'sendmail':
// Create a sendmail connection
$transport = Swift_SendmailTransport::newInstance(empty($config['options']) ? "/usr/sbin/sendmail -bs" : $config['options']);
break;

default:
// Use the native connection
$transport = Swift_MailTransport::newInstance($config['options']);
break;
}

//Create the Mailer using the appropriate transport
return $this->_mailer = Swift_Mailer::newInstance($transport);
$klass = 'Mailer_Transport_' . ucfirst($transport);
$factory = new $klass();

//Create the Mailer
return $this->_mailer = Swift_Mailer::newInstance($factory->build($config));
}

/**
Expand Down Expand Up @@ -484,4 +454,4 @@ protected function set_data(& $view)
return $view;
}

}// end of Mailer
}// end of Mailer
6 changes: 6 additions & 0 deletions classes/kohana/mailer/transport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

abstract class Kohana_Mailer_Transport {
public function build ( $config ) {}
}

10 changes: 10 additions & 0 deletions classes/kohana/mailer/transport/native.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php defined('SYSPATH') or die('No direct script access.');

class Kohana_Mailer_Transport_Native extends Kohana_Mailer_Transport {

public function build ( $config ) {
return Swift_MailTransport::newInstance();
}

}

10 changes: 10 additions & 0 deletions classes/kohana/mailer/transport/sendmail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php defined('SYSPATH') or die('No direct script access.');

class Kohana_Mailer_Transport_Sendmail extends Kohana_Mailer_Transport {

public function build ( $config ) {
return Swift_SendmailTransport::newInstance(empty($config['options']) ? "/usr/sbin/sendmail -bs" : $config['options']);
}

}

26 changes: 26 additions & 0 deletions classes/kohana/mailer/transport/smtp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php defined('SYSPATH') or die('No direct script access.');

class Kohana_Mailer_Transport_SMTP extends Kohana_Mailer_Transport {

public function build ( $config ) {
//Create the Transport
$transport = Swift_SmtpTransport::newInstance()
->setHost(empty($config['hostname']) ? "localhost" : (string) $config['hostname'])
->setUsername(empty($config['username']) ? NULL : (string) $config['username'])
->setPassword(empty($config['password']) ? NULL : (string) $config['password']);

//Port?
$port = empty($config['port']) ? 25 : (int) $config['port'];
$transport->setPort($port);

//Use encryption?
if (! empty($config['encryption']))
{
$transport->setEncryption($config['encryption']);
}

return $transport;
}

}

3 changes: 3 additions & 0 deletions classes/mailer/transport/native.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php defined('SYSPATH') or die('No direct script access.');

class Mailer_Transport_Native extends Kohana_Mailer_Transport_Native {}
3 changes: 3 additions & 0 deletions classes/mailer/transport/sendmail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php defined('SYSPATH') or die('No direct script access.');

class Mailer_Transport_Sendmail extends Kohana_Mailer_Transport_Sendmail {}
3 changes: 3 additions & 0 deletions classes/mailer/transport/smtp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php defined('SYSPATH') or die('No direct script access.');

class Mailer_Transport_SMTP extends Kohana_Mailer_Transport_SMTP {}

0 comments on commit 8b471ee

Please sign in to comment.