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
5d679813
Commit
5d679813
authored
Nov 09, 2013
by
Alexander Makarov
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1167 from dmeroff/filemailer
Added FileMailer implementation of mailer interface
parents
3bda94ee
7d6d01b4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
217 additions
and
0 deletions
+217
-0
FileMailer.php
extensions/swiftmailer/FileMailer.php
+136
-0
README.md
extensions/swiftmailer/README.md
+14
-0
composer.json
extensions/swiftmailer/composer.json
+4
-0
FileMailerTest.php
tests/unit/extensions/swiftmailer/FileMailerTest.php
+63
-0
No files found.
extensions/swiftmailer/FileMailer.php
0 → 100644
View file @
5d679813
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace
yii\swiftmailer
;
use
yii\helpers\FileHelper
;
use
yii\mail\BaseMailer
;
use
Yii
;
/**
* FileMailer implements a mailer that saves mails as files under runtime directory.
*
* To use FileMailer, you should configure it in the application configuration like the following,
*
* ~~~
* 'components' => array(
* ...
* 'email' => array(
* 'class' => 'yii\swiftmailer\FileMailer'
* ),
* ...
* ),
* ~~~
*
* To send an email, you may use the following code:
*
* ~~~
* Yii::$app->mail->compose('contact/html', ['contactForm' => $form])
* ->setFrom('from@domain.com')
* ->setTo($form->email)
* ->setSubject($form->subject)
* ->send();
* ~~~
*
* @author Dmitry Erofeev <dmeroff@gmail.com>
* @since 2.0
*/
class
FileMailer
extends
BaseMailer
{
/**
* @var string message default class name.
*/
public
$messageClass
=
'yii\swiftmailer\Message'
;
/**
* @var string The path under which mail files will be written. Defaults to "@app/runtime/mail".
*/
protected
$path
;
/**
* @var callable Callback that will be used to generate name of the message to save.
*/
protected
$callback
;
/**
* Sets path under which mail files will be written.
* Any trailing '/' and '\' characters in the given path will be trimmed.
*
* @param $path
* @throws \InvalidArgumentException
*/
public
function
setPath
(
$path
)
{
$path
=
Yii
::
getAlias
(
$path
);
if
(
!
is_dir
(
$path
)
||
!
is_writable
(
$path
))
{
throw
new
\InvalidArgumentException
(
'Filemailer::setPath expects a valid path in which to write mail files'
);
}
$this
->
path
=
rtrim
(
$path
,
'\\/'
);
}
/**
* Gets path under which mail files will be written.
*
* @return string
*/
public
function
getPath
()
{
if
(
$this
->
path
==
null
)
{
$path
=
Yii
::
getAlias
(
'@app/runtime/mail'
);
if
(
!
is_dir
(
$path
))
{
FileHelper
::
createDirectory
(
$path
);
}
$this
->
setPath
(
$path
);
}
return
$this
->
path
;
}
/**
* Sets callback that will be used to generate name of the message to save.
*
* @param callable $callback
*/
public
function
setCallback
(
callable
$callback
)
{
$this
->
callback
=
$callback
;
}
/**
* Gets callback that will be used to generate name of the message to save.
*
* @return callable
*/
public
function
getCallback
()
{
if
(
$this
->
callback
==
null
)
{
$this
->
setCallback
(
function
()
{
return
uniqid
(
'Message_'
)
.
'.txt'
;
});
}
return
$this
->
callback
;
}
/**
* @inheritdoc
*/
public
function
send
(
$message
)
{
$address
=
$message
->
getTo
();
if
(
is_array
(
$address
))
{
$address
=
implode
(
', '
,
array_keys
(
$address
));
}
Yii
::
trace
(
'Sending email "'
.
$message
->
getSubject
()
.
'" to "'
.
$address
.
'"'
,
__METHOD__
);
$filename
=
$this
->
getPath
()
.
DIRECTORY_SEPARATOR
.
call_user_func
(
$this
->
getCallback
());
return
file_put_contents
(
$filename
,
$message
->
toString
())
!==
false
;
}
}
\ No newline at end of file
extensions/swiftmailer/README.md
View file @
5d679813
...
@@ -16,6 +16,20 @@ return [
...
@@ -16,6 +16,20 @@ return [
];
];
```
```
Extension also includes
`FileMailer`
that saves mails as files under runtime directory.
It can be useful during development. To use it add the following code in your application configuration:
```
php
return
[
//....
'components'
=>
[
'mail'
=>
[
'class'
=>
'yii\swiftmailer\FileMailer'
,
],
],
];
```
You can then send an email as follows:
You can then send an email as follows:
```
php
```
php
...
...
extensions/swiftmailer/composer.json
View file @
5d679813
...
@@ -15,6 +15,10 @@
...
@@ -15,6 +15,10 @@
{
{
"name"
:
"Paul Klimov"
,
"name"
:
"Paul Klimov"
,
"email"
:
"klimov.paul@gmail.com"
"email"
:
"klimov.paul@gmail.com"
},
{
"name"
:
"Dmitry Erofeev"
,
"email"
:
"dmeroff@gmail.com"
}
}
],
],
"minimum-stability"
:
"dev"
,
"minimum-stability"
:
"dev"
,
...
...
tests/unit/extensions/swiftmailer/FileMailerTest.php
0 → 100644
View file @
5d679813
<?php
namespace
yiiunit\extensions\swiftmailer
;
require
__DIR__
.
'/../../../../extensions/swiftmailer/FileMailer.php'
;
use
yii\swiftmailer\FileMailer
;
use
yiiunit\VendorTestCase
;
/**
* @group vendor
* @group mail
* @group swiftmailer
*/
class
FileMailerTest
extends
VendorTestCase
{
public
function
setUp
()
{
$this
->
mockApplication
([
'components'
=>
[
'mail'
=>
$this
->
createTestEmailComponent
()
]
]);
}
/**
* @return FileMailer test email component instance.
*/
protected
function
createTestEmailComponent
()
{
$component
=
new
FileMailer
([
'callback'
=>
function
()
{
return
'Message_test.txt'
;
}
]);
return
$component
;
}
public
function
testConfigurePath
()
{
$mailer
=
new
FileMailer
();
$this
->
assertEquals
(
\Yii
::
getAlias
(
'@app/runtime/mail'
),
$mailer
->
getPath
());
$mailer
->
setPath
(
'@yiiunit/runtime/'
);
$this
->
assertEquals
(
\Yii
::
getAlias
(
'@yiiunit/runtime'
),
$mailer
->
getPath
());
}
public
function
testSend
()
{
$message
=
\Yii
::
$app
->
mail
->
compose
()
->
setTo
(
'tester@example.com'
)
->
setFrom
(
'admin@example.com'
)
->
setSubject
(
'Just a test'
)
->
setHtmlBody
(
'This is html body'
);
$message
->
send
();
$this
->
assertEquals
(
$message
->
toString
(),
file_get_contents
(
\Yii
::
$app
->
getRuntimePath
()
.
'/mail/Message_test.txt'
)
);
}
}
\ No newline at end of file
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