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
eb3b2f45
Commit
eb3b2f45
authored
Nov 28, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactored EmailTarget with the new mailer interfaces.
parent
c22409e3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
43 additions
and
31 deletions
+43
-31
BaseYii.php
framework/yii/BaseYii.php
+2
-0
EmailTarget.php
framework/yii/log/EmailTarget.php
+40
-30
Target.php
framework/yii/log/Target.php
+1
-1
No files found.
framework/yii/BaseYii.php
View file @
eb3b2f45
...
...
@@ -519,12 +519,14 @@ class BaseYii
* Configures an object with the initial property values.
* @param object $object the object to be configured
* @param array $properties the property initial values given in terms of name-value pairs.
* @return object the object itself
*/
public
static
function
configure
(
$object
,
$properties
)
{
foreach
(
$properties
as
$name
=>
$value
)
{
$object
->
$name
=
$value
;
}
return
$object
;
}
/**
...
...
framework/yii/log/EmailTarget.php
View file @
eb3b2f45
...
...
@@ -7,6 +7,10 @@
namespace
yii\log
;
use
Yii
;
use
yii\base\InvalidConfigException
;
use
yii\mail\MailerInterface
;
/**
* EmailTarget sends selected log messages to the specified email addresses.
*
...
...
@@ -20,51 +24,57 @@ namespace yii\log;
class
EmailTarget
extends
Target
{
/**
* @var array list of destination email addresses.
*/
public
$emails
=
[];
/**
* @var string email subject
* @var array the configuration array for creating a [[\yii\mail\MessageInterface|message]] object.
* Note that the "to" option must be set, which specifies the destination email address(es).
*/
public
$
subject
;
public
$
message
=
[]
;
/**
* @var string email sent-from address
* @var MailerInterface|string the mailer object or the application component ID of the mailer object.
* After the EmailTarget object is created, if you want to change this property, you should only assign it
* with a mailer object.
*/
public
$sentFrom
;
public
$mail
=
'mail'
;
/**
* @
var array list of additional headers to use when sending an email.
* @
inheritdoc
*/
public
$headers
=
[];
public
function
init
()
{
parent
::
init
();
if
(
empty
(
$this
->
message
[
'to'
]))
{
throw
new
InvalidConfigException
(
'The "to" option must be set for EmailTarget::message.'
);
}
if
(
empty
(
$this
->
message
[
'subject'
]))
{
$this
->
message
[
'subject'
]
=
Yii
::
t
(
'yii'
,
'Application Log'
);
}
if
(
is_string
(
$this
->
mail
))
{
$this
->
mail
=
Yii
::
$app
->
getComponent
(
$this
->
mail
);
}
if
(
!
$this
->
mail
instanceof
MailerInterface
)
{
throw
new
InvalidConfigException
(
"EmailTarget::mailer must be either a mailer object or the application component ID of a mailer object."
);
}
}
/**
* Sends log messages to specified email addresses.
*/
public
function
export
()
{
$body
=
''
;
foreach
(
$this
->
messages
as
$message
)
{
$body
.=
$this
->
formatMessage
(
$message
);
}
$body
=
wordwrap
(
$body
,
70
);
$subject
=
$this
->
subject
===
null
?
\Yii
::
t
(
'yii'
,
'Application Log'
)
:
$this
->
subject
;
foreach
(
$this
->
emails
as
$email
)
{
$this
->
sendEmail
(
$subject
,
$body
,
$email
,
$this
->
sentFrom
,
$this
->
headers
);
}
$message
=
$this
->
mail
->
compose
();
Yii
::
configure
(
$message
,
$this
->
message
);
$this
->
composeMessage
(
$message
);
$this
->
mail
->
send
(
$message
);
}
/**
* Sends an email.
* @param string $subject email subject
* @param string $body email body
* @param string $sentTo sent-to email address
* @param string $sentFrom sent-from email address
* @param array $headers additional headers to be used when sending the email
* Composes the given mail message with body content.
* The default implementation fills the text body of the message with the log messages.
* @param \yii\mail\MessageInterface $message
*/
protected
function
sendEmail
(
$subject
,
$body
,
$sentTo
,
$sentFrom
,
$headers
)
protected
function
composeMessage
(
$message
)
{
if
(
$sentFrom
!==
null
)
{
$headers
[]
=
"From:
{
$sentFrom
}
"
;
}
mail
(
$sentTo
,
$subject
,
$body
,
implode
(
"
\r\n
"
,
$headers
));
$messages
=
array_map
([
$this
,
'formatMessage'
],
$this
->
messages
);
$body
=
wordwrap
(
implode
(
"
\n
"
,
$messages
),
70
);
$message
->
setTextBody
(
$body
);
}
}
framework/yii/log/Target.php
View file @
eb3b2f45
...
...
@@ -233,6 +233,6 @@ abstract class Target extends Component
$text
=
var_export
(
$text
,
true
);
}
$ip
=
isset
(
$_SERVER
[
'REMOTE_ADDR'
])
?
$_SERVER
[
'REMOTE_ADDR'
]
:
'127.0.0.1'
;
return
date
(
'Y/m/d H:i:s'
,
$timestamp
)
.
" [
$ip
] [
$level
] [
$category
]
$text
\n
"
;
return
date
(
'Y/m/d H:i:s'
,
$timestamp
)
.
" [
$ip
] [
$level
] [
$category
]
$text
"
;
}
}
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