Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
B
beego
Project
Project
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
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
beego
Commits
b87e122a
Commit
b87e122a
authored
Dec 19, 2013
by
1fei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update smtp.go
parent
dcaff38c
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
17 additions
and
44 deletions
+17
-44
smtp.go
logs/smtp.go
+17
-44
No files found.
logs/smtp.go
View file @
b87e122a
...
...
@@ -2,7 +2,6 @@ package logs
import
(
"encoding/json"
"errors"
"fmt"
"net/smtp"
"strings"
...
...
@@ -15,77 +14,51 @@ const (
// smtpWriter is used to send emails via given SMTP-server.
type
SmtpWriter
struct
{
username
string
password
string
host
string
subject
string
recipientAddresses
[]
string
level
int
Username
string
`json:"Username"`
Password
string
`json:"password"`
Host
string
`json:"Host"`
Subject
string
`json:"subject"`
RecipientAddresses
[]
string
`json:"sendTos"`
Level
int
`json:"level"`
}
func
NewSmtpWriter
()
LoggerInterface
{
return
&
SmtpWriter
{
l
evel
:
LevelTrace
}
return
&
SmtpWriter
{
L
evel
:
LevelTrace
}
}
func
(
s
*
SmtpWriter
)
Init
(
jsonconfig
string
)
error
{
var
m
map
[
string
]
interface
{}
err
:=
json
.
Unmarshal
([]
byte
(
jsonconfig
),
&
m
)
err
:=
json
.
Unmarshal
([]
byte
(
jsonconfig
),
s
)
if
err
!=
nil
{
return
err
}
if
username
,
ok
:=
m
[
"username"
];
!
ok
{
return
errors
.
New
(
"smtp config must have auth username"
)
}
else
if
password
,
ok
:=
m
[
"password"
];
!
ok
{
return
errors
.
New
(
"smtp config must have auth password"
)
}
else
if
hostname
,
ok
:=
m
[
"host"
];
!
ok
{
return
errors
.
New
(
"smtp config must have host like 'mail.example.com:25'"
)
}
else
if
sendTos
,
ok
:=
m
[
"sendTos"
];
!
ok
{
return
errors
.
New
(
"smtp config must have sendTos"
)
}
else
{
s
.
username
=
username
.
(
string
)
s
.
password
=
password
.
(
string
)
s
.
host
=
hostname
.
(
string
)
for
_
,
v
:=
range
sendTos
.
([]
interface
{})
{
s
.
recipientAddresses
=
append
(
s
.
recipientAddresses
,
v
.
(
string
))
}
}
if
subject
,
ok
:=
m
[
"subject"
];
ok
{
s
.
subject
=
subject
.
(
string
)
}
else
{
s
.
subject
=
subjectPhrase
}
if
lv
,
ok
:=
m
[
"level"
];
ok
{
s
.
level
=
int
(
lv
.
(
float64
))
}
return
nil
}
func
(
s
*
SmtpWriter
)
WriteMsg
(
msg
string
,
level
int
)
error
{
if
level
<
s
.
l
evel
{
if
level
<
s
.
L
evel
{
return
nil
}
hp
:=
strings
.
Split
(
s
.
h
ost
,
":"
)
hp
:=
strings
.
Split
(
s
.
H
ost
,
":"
)
// Set up authentication information.
auth
:=
smtp
.
PlainAuth
(
""
,
s
.
u
sername
,
s
.
p
assword
,
s
.
U
sername
,
s
.
P
assword
,
hp
[
0
],
)
// Connect to the server, authenticate, set the sender and recipient,
// and send the email all in one step.
content_type
:=
"Content-Type: text/plain"
+
"; charset=UTF-8"
mailmsg
:=
[]
byte
(
"To: "
+
strings
.
Join
(
s
.
recipientAddresses
,
";"
)
+
"
\r\n
From: "
+
s
.
username
+
"<"
+
s
.
u
sername
+
">
\r\n
Subject: "
+
s
.
s
ubject
+
"
\r\n
"
+
content_type
+
"
\r\n\r\n
"
+
fmt
.
Sprintf
(
".%s"
,
time
.
Now
()
.
Format
(
"2006-01-02 15:04:05"
))
+
msg
)
mailmsg
:=
[]
byte
(
"To: "
+
strings
.
Join
(
s
.
RecipientAddresses
,
";"
)
+
"
\r\n
From: "
+
s
.
Username
+
"<"
+
s
.
U
sername
+
">
\r\n
Subject: "
+
s
.
S
ubject
+
"
\r\n
"
+
content_type
+
"
\r\n\r\n
"
+
fmt
.
Sprintf
(
".%s"
,
time
.
Now
()
.
Format
(
"2006-01-02 15:04:05"
))
+
msg
)
err
:=
smtp
.
SendMail
(
s
.
h
ost
,
s
.
H
ost
,
auth
,
s
.
u
sername
,
s
.
r
ecipientAddresses
,
s
.
U
sername
,
s
.
R
ecipientAddresses
,
mailmsg
,
)
...
...
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