Skip to content
Toggle navigation
Toggle navigation
This project
Loading...
Sign in
张磊
/
FileStorageBeego
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Issue Boards
Files
Commits
Network
Compare
Branches
Tags
b87e122a
authored
2013-12-19 18:16:06 +0800
by
1fei
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
Update smtp.go
1 parent
dcaff38c
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
17 additions
and
44 deletions
logs/smtp.go
logs/smtp.go
View file @
b87e122
...
...
@@ -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
Styling with
Markdown
is supported
Attach a file
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 post a comment