Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
02c0bfcb
Commit
02c0bfcb
authored
Nov 21, 2013
by
Paul Klimov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
"yii\swiftmailer\Mailer" transport setup has been advanced to support…
"yii\swiftmailer\Mailer" transport setup has been advanced to support constructor arguments and plugins.
parent
1c540894
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
117 additions
and
9 deletions
+117
-9
Mailer.php
extensions/swiftmailer/Mailer.php
+67
-9
MailerTest.php
tests/unit/extensions/swiftmailer/MailerTest.php
+50
-0
No files found.
extensions/swiftmailer/Mailer.php
View file @
02c0bfcb
...
@@ -37,6 +37,23 @@ use yii\mail\BaseMailer;
...
@@ -37,6 +37,23 @@ use yii\mail\BaseMailer;
* You may also skip the configuration of the [[transport]] property. In that case, the default
* You may also skip the configuration of the [[transport]] property. In that case, the default
* PHP `mail()` function will be used to send emails.
* PHP `mail()` function will be used to send emails.
*
*
* You specify the transport constructor arguments using 'constructArgs' key in the config.
* You can also specify the list of plugins, which should be registered to the transport using
* 'plugins' key. For example:
*
* ~~~
* 'transport' => [
* 'class' => 'Swift_SmtpTransport',
* 'constructArgs' => ['localhost', 25]
* 'plugins' => [
* [
* 'class' => 'Swift_Plugins_ThrottlerPlugin',
* 'constructArgs' => [20],
* ],
* ],
* ],
* ~~~
*
* To send an email, you may use the following code:
* To send an email, you may use the following code:
*
*
* ~~~
* ~~~
...
@@ -131,28 +148,69 @@ class Mailer extends BaseMailer
...
@@ -131,28 +148,69 @@ class Mailer extends BaseMailer
*/
*/
protected
function
createTransport
(
array
$config
)
protected
function
createTransport
(
array
$config
)
{
{
if
(
!
isset
(
$config
[
'class'
]))
{
$config
[
'class'
]
=
'Swift_MailTransport'
;
}
if
(
isset
(
$config
[
'plugins'
]))
{
$plugins
=
$config
[
'plugins'
];
unset
(
$config
[
'plugins'
]);
}
/** @var \Swift_MailTransport $transport */
$transport
=
$this
->
createSwiftObject
(
$config
);
if
(
isset
(
$plugins
))
{
foreach
(
$plugins
as
$plugin
)
{
if
(
is_array
(
$plugin
)
&&
isset
(
$plugin
[
'class'
]))
{
$plugin
=
$this
->
createSwiftObject
(
$plugin
);
}
$transport
->
registerPlugin
(
$plugin
);
}
}
return
$transport
;
}
/**
* Creates Swift library object, from given array configuration.
* @param array $config object configuration
* @return Object created object
* @throws \yii\base\InvalidConfigException on invalid configuration.
*/
protected
function
createSwiftObject
(
array
$config
)
{
if
(
isset
(
$config
[
'class'
]))
{
if
(
isset
(
$config
[
'class'
]))
{
$className
=
$config
[
'class'
];
$className
=
$config
[
'class'
];
unset
(
$config
[
'class'
]);
unset
(
$config
[
'class'
]);
}
else
{
}
else
{
$className
=
'Swift_MailTransport'
;
throw
new
InvalidConfigException
(
'Object configuration must be an array containing a "class" element.'
);
}
if
(
isset
(
$config
[
'constructArgs'
]))
{
$args
=
[];
foreach
(
$config
[
'constructArgs'
]
as
$arg
)
{
if
(
is_array
(
$arg
)
&&
isset
(
$arg
[
'class'
]))
{
$args
[]
=
$this
->
createSwiftObject
(
$arg
);
}
else
{
$args
[]
=
$arg
;
}
}
unset
(
$config
[
'constructArgs'
]);
array_unshift
(
$args
,
[
'class'
=>
$className
]);
$object
=
call_user_func_array
([
'Yii'
,
'createObject'
],
$args
);
}
else
{
$object
=
new
$className
;
}
}
/** @var \Swift_MailTransport $transport */
$transport
=
$className
::
newInstance
();
if
(
!
empty
(
$config
))
{
if
(
!
empty
(
$config
))
{
foreach
(
$config
as
$name
=>
$value
)
{
foreach
(
$config
as
$name
=>
$value
)
{
if
(
property_exists
(
$
transpor
t
,
$name
))
{
if
(
property_exists
(
$
objec
t
,
$name
))
{
$
transpor
t
->
$name
=
$value
;
$
objec
t
->
$name
=
$value
;
}
else
{
}
else
{
$setter
=
'set'
.
$name
;
$setter
=
'set'
.
$name
;
if
(
method_exists
(
$
transport
,
$setter
)
||
method_exists
(
$transpor
t
,
'__call'
))
{
if
(
method_exists
(
$
object
,
$setter
)
||
method_exists
(
$objec
t
,
'__call'
))
{
$
transpor
t
->
$setter
(
$value
);
$
objec
t
->
$setter
(
$value
);
}
else
{
}
else
{
throw
new
InvalidConfigException
(
'Setting unknown property: '
.
get_class
(
$transport
)
.
'::'
.
$name
);
throw
new
InvalidConfigException
(
'Setting unknown property: '
.
$className
.
'::'
.
$name
);
}
}
}
}
}
}
}
}
return
$
transpor
t
;
return
$
objec
t
;
}
}
}
}
tests/unit/extensions/swiftmailer/MailerTest.php
View file @
02c0bfcb
...
@@ -63,6 +63,56 @@ class MailerTest extends VendorTestCase
...
@@ -63,6 +63,56 @@ class MailerTest extends VendorTestCase
$this
->
assertEquals
(
$transportConfig
[
'host'
],
$transport
->
getHost
(),
'Invalid transport host!'
);
$this
->
assertEquals
(
$transportConfig
[
'host'
],
$transport
->
getHost
(),
'Invalid transport host!'
);
}
}
/**
* @depends testConfigureTransport
*/
public
function
testConfigureTransportConstruct
()
{
$mailer
=
new
Mailer
();
$host
=
'some.test.host'
;
$port
=
999
;
$transportConfig
=
[
'class'
=>
'Swift_SmtpTransport'
,
'constructArgs'
=>
[
$host
,
$port
,
],
];
$mailer
->
setTransport
(
$transportConfig
);
$transport
=
$mailer
->
getTransport
();
$this
->
assertTrue
(
is_object
(
$transport
),
'Unable to setup transport via config!'
);
$this
->
assertEquals
(
$host
,
$transport
->
getHost
(),
'Invalid transport host!'
);
$this
->
assertEquals
(
$port
,
$transport
->
getPort
(),
'Invalid transport host!'
);
}
/**
* @depends testConfigureTransportConstruct
*/
public
function
testConfigureTransportWithPlugins
()
{
$mailer
=
new
Mailer
();
$pluginClass
=
'Swift_Plugins_ThrottlerPlugin'
;
$rate
=
10
;
$transportConfig
=
[
'class'
=>
'Swift_SmtpTransport'
,
'plugins'
=>
[
[
'class'
=>
$pluginClass
,
'constructArgs'
=>
[
$rate
,
],
],
],
];
$mailer
->
setTransport
(
$transportConfig
);
$transport
=
$mailer
->
getTransport
();
$this
->
assertTrue
(
is_object
(
$transport
),
'Unable to setup transport via config!'
);
$this
->
assertContains
(
':'
.
$pluginClass
.
':'
,
print_r
(
$transport
,
true
),
'Plugin not added'
);
}
public
function
testGetSwiftMailer
()
public
function
testGetSwiftMailer
()
{
{
$mailer
=
new
Mailer
();
$mailer
=
new
Mailer
();
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment