Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
GO Email Worker
Project
Project
Details
Activity
Releases
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
Stephan Handuwala
GO Email Worker
Commits
9ead916b
Commit
9ead916b
authored
May 03, 2019
by
Stephan Handuwala
🏋🏻
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'master' into 'master'
Master See merge request
!15
parents
b5278538
0953dc47
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
28 additions
and
24 deletions
+28
-24
config.yaml
config.yaml
+1
-1
worker.go
worker.go
+27
-23
No files found.
config.yaml
View file @
9ead916b
...
...
@@ -9,7 +9,7 @@ curl:
delete_outbox_mail
:
http://root:root@couchdb_emanger:5984/outbox/
add_sent_mail
:
http://root:root@couchdb_emanger:5984/sentitems/
add_mail_log
:
http://root:root@couchdb_emanger:5984/mail_logs/
read_log
:
http://root:root@couchdb_emanger:5984/read_logs/
read_log
s
:
http://root:root@couchdb_emanger:5984/read_logs
protocols
:
method
:
http://
requests
:
...
...
worker.go
View file @
9ead916b
...
...
@@ -41,7 +41,7 @@ type Config struct {
DeleteOutboxMail
string
`yaml:"delete_outbox_mail"`
AddSentMail
string
`yaml:"add_sent_mail"`
AddMailLog
string
`yaml:"add_mail_log"`
ReadLog
string
`yaml:"read_log"`
ReadLog
string
`yaml:"read_log
s
"`
}
`yaml:"curl"`
Protocols
struct
{
Request
string
`yaml:"method"`
...
...
@@ -107,6 +107,7 @@ var mailData chan string
var
outboxReadTime
chan
string
var
outboxDeleteTime
chan
string
var
filename
=
""
var
loc
,
_
=
time
.
LoadLocation
(
"Asia/Colombo"
)
func
main
()
{
// get configurations
...
...
@@ -182,8 +183,10 @@ func main() {
alertAdmin
(
alert
)
}
fmt
.
Printf
(
" %s, %d | "
,
resp
.
Status
,
newDoc
.
TotalRows
)
// fmt.Println(string(respbody))
fmt
.
Printf
(
" %s, %s | "
,
resp
.
Status
,
strconv
.
Itoa
(
newDoc
.
TotalRows
))
logOutbox
(
resp
.
Status
,
strconv
.
Itoa
(
newDoc
.
TotalRows
),
time
.
Now
()
.
In
(
loc
)
.
String
()
/*string(respbody)*/
)
//fmt.Printf("%s, %s, %s", resp.Status, strconv.Itoa(newDoc.TotalRows), string(respbody))
/**
* if: the "OUTBOX" DB has new records, it returns the number of Rows
* so there are new documents the value is always greaterthan 0
...
...
@@ -204,9 +207,9 @@ func main() {
* Now we get the documents' email data from the "OUTBOX" DB
* cURL reuest to get the document
*/
req
,
reqErr
:=
http
.
NewRequest
(
config
.
Requests
.
Get
,
config
.
Curl
.
GetOutboxMail
+
newMailID
,
nil
)
req
.
Header
.
Set
(
config
.
Requests
.
HeaderType
,
config
.
Requests
.
HeaderJson
)
resp
,
respErr
:=
http
.
DefaultClient
.
Do
(
req
)
req
New
,
reqErr
:=
http
.
NewRequest
(
config
.
Requests
.
Get
,
config
.
Curl
.
GetOutboxMail
+
newMailID
,
nil
)
req
New
.
Header
.
Set
(
config
.
Requests
.
HeaderType
,
config
.
Requests
.
HeaderJson
)
resp
New
,
respErr
:=
http
.
DefaultClient
.
Do
(
reqNew
)
if
reqErr
!=
nil
{
alert
:=
" Error requesting the OUTBOX mail data
\n
"
+
reqErr
.
Error
()
alertAdmin
(
alert
)
...
...
@@ -215,10 +218,10 @@ func main() {
alert
:=
" Error getting the OUTBOX mail data
\n
"
+
respErr
.
Error
()
alertAdmin
(
alert
)
}
defer
resp
.
Body
.
Close
()
respbody
,
_
:=
ioutil
.
ReadAll
(
resp
.
Body
)
newMail
:=
string
(
respbody
)
readTimeOutbox
:=
time
.
Now
()
.
String
()
defer
resp
New
.
Body
.
Close
()
respbody
New
,
_
:=
ioutil
.
ReadAll
(
respNew
.
Body
)
newMail
:=
string
(
respbody
New
)
readTimeOutbox
:=
time
.
Now
()
.
In
(
loc
)
.
String
()
// mailReadTime := mailReadTime.String()
/**
...
...
@@ -255,7 +258,7 @@ func main() {
}
defer
delResp
.
Body
.
Close
()
ioutil
.
ReadAll
(
delResp
.
Body
)
deleteTimeOutbox
:=
time
.
Now
()
.
String
()
deleteTimeOutbox
:=
time
.
Now
()
.
In
(
loc
)
.
String
()
outboxDeleteTime
<-
(
deleteTimeOutbox
)
}
else
{
...
...
@@ -265,7 +268,6 @@ func main() {
outboxDeleteTime
<-
""
}
}
}
...
...
@@ -397,7 +399,7 @@ func worker(i int) {
attachment
=
""
}
ctx
,
cancel
:=
context
.
WithTimeout
(
context
.
Background
(),
time
.
Second
*
3
0
)
ctx
,
cancel
:=
context
.
WithTimeout
(
context
.
Background
(),
time
.
Second
*
5
0
)
defer
cancel
()
MailgunResp
,
id
,
err
:=
mg
.
Send
(
ctx
,
message
)
if
err
!=
nil
{
...
...
@@ -413,7 +415,7 @@ func worker(i int) {
fmt
.
Printf
(
"Email sent ID: %s Resp: %s
\n
"
,
id
,
MailgunResp
)
}
//sendMessage(mg, sender, subject, body, recipient, cc)
mailSentTimeMailgun
:=
time
.
Now
()
.
String
()
mailSentTimeMailgun
:=
time
.
Now
()
.
In
(
loc
)
.
String
()
/**
* add the email as new document in "SENT ITEMS" DB
*/
...
...
@@ -433,7 +435,7 @@ func worker(i int) {
defer resp.Body.Close()
ioutil.ReadAll(resp.Body)
*/
mailAddedSentitems
:=
time
.
Now
()
.
String
()
mailAddedSentitems
:=
time
.
Now
()
.
In
(
loc
)
.
String
()
sentmail
(
subject
,
sender
,
recipient
,
cc
,
bcc
,
attachment
,
body
,
mail
.
ID
)
logmail
(
subject
,
sender
,
recipient
,
cc
,
bcc
,
attachment
,
readTimeOutbox
,
mailSentTimeMailgun
,
id
,
MailgunResp
,
mailAddedSentitems
,
deleteTimeOutbox
,
mail
.
ID
)
...
...
@@ -589,34 +591,36 @@ func sentmail(subject, sender, recipient, cc, bcc, attachment, body, mailID stri
ioutil
.
ReadAll
(
logResp
.
Body
)
}
func
logOutbox
(
status
,
error
,
id
string
)
{
func
logOutbox
(
status
,
totalRows
,
time
/*respBody*/
string
)
{
// get configurations
var
config
Config
source
,
sourceErr
:=
ioutil
.
ReadFile
(
"config.yaml"
)
if
sourceErr
!=
nil
{
fmt
.
Printf
(
"
Log
mail sourceErr : %s"
,
sourceErr
.
Error
())
fmt
.
Printf
(
"
Sent
mail sourceErr : %s"
,
sourceErr
.
Error
())
// panic(sourceErr)
}
configErr
:=
yaml
.
Unmarshal
(
source
,
&
config
)
if
configErr
!=
nil
{
fmt
.
Printf
(
"
Log
mail configErr : %s"
,
configErr
.
Error
())
fmt
.
Printf
(
"
Sent
mail configErr : %s"
,
configErr
.
Error
())
// panic(configErr)
}
sentBody
:=
strings
.
NewReader
(
`{"status":"`
+
status
+
`","
error":"`
+
error
+
`","
id":"`
+
id
+
`"}`
)
logReq
,
logBodyerr
:=
http
.
NewRequest
(
config
.
Requests
.
P
ut
,
config
.
Curl
.
ReadLog
+
id
,
sentBody
)
`","
row_count":"`
+
totalRows
+
`","
date_time":"`
+
time
+
`"}`
)
logReq
,
logBodyerr
:=
http
.
NewRequest
(
config
.
Requests
.
P
ost
,
config
.
Curl
.
ReadLog
,
sentBody
)
if
logBodyerr
!=
nil
{
alert
:=
" Error requesting
log
mail
\n
"
+
logBodyerr
.
Error
()
alert
:=
" Error requesting
sent
mail
\n
"
+
logBodyerr
.
Error
()
alertAdmin
(
alert
)
}
logReq
.
Header
.
Set
(
config
.
Requests
.
HeaderType
,
config
.
Requests
.
HeaderJson
)
logResp
,
respErr
:=
http
.
DefaultClient
.
Do
(
logReq
)
if
respErr
!=
nil
{
alert
:=
" Error adding data to
LOG
S DB
\n
"
+
respErr
.
Error
()
alert
:=
" Error adding data to
SENT ITEM
S DB
\n
"
+
respErr
.
Error
()
alertAdmin
(
alert
)
}
// aa, _ := ioutil.ReadAll(logResp.Body)
// fmt.Println(aa)
defer
logResp
.
Body
.
Close
()
ioutil
.
ReadAll
(
logResp
.
Body
)
}
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