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
092d14bc
Commit
092d14bc
authored
Apr 03, 2019
by
Pasan Mallawaarachchi
💻
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
attachment commit
parent
b10741dd
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
60 additions
and
12 deletions
+60
-12
worker.go
worker.go
+60
-12
No files found.
worker.go
View file @
092d14bc
...
...
@@ -12,9 +12,12 @@ import (
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"path"
"strings"
"sync"
"time"
...
...
@@ -89,8 +92,10 @@ type MailBody struct {
From
string
`json:"from"`
To
string
`json:"to"`
Cc
string
`json:"cc"`
Bcc
string
`json:"bcc"`
Subject
string
`json:"subject"`
Body
string
`json:"body"`
Attachment
string
`json:"attachment"`
}
/**
...
...
@@ -101,6 +106,7 @@ var waitGroup sync.WaitGroup
var
mailData
chan
string
var
outboxReadTime
chan
string
var
outboxDeleteTime
chan
string
var
fileName
=
""
func
main
()
{
// get configurations
...
...
@@ -312,16 +318,30 @@ func worker(i int) {
body
:=
mail
.
Body
recipient
:=
mail
.
To
cc
:=
mail
.
Cc
bcc
:=
mail
.
Bcc
attachment
:=
mail
.
Attachment
// sending mail via Mailgun
mg
:=
mailgun
.
NewMailgun
(
config
.
Mailgun
.
Domain
,
config
.
Mailgun
.
Apikey
)
message
:=
mg
.
NewMessage
(
sender
,
subject
,
body
,
recipient
)
/**
* Add CC
if cc recipiants are
available
* Add CC
, BCC & ATTACHMENTS if
available
*/
if
len
(
cc
)
>
0
{
message
.
AddCC
(
cc
)
}
if
len
(
bcc
)
>
0
{
message
.
AddBCC
(
bcc
)
}
if
len
(
attachment
)
>
0
{
//url := "http://www.orimi.com/pdf-test.pdf"
r
,
_
:=
http
.
NewRequest
(
"GET"
,
attachment
,
nil
)
fileName
=
path
.
Base
(
r
.
URL
.
Path
)
getFile
(
fileName
,
attachment
)
message
.
AddHeader
(
"Content-Type"
,
"multipart/form-data"
)
message
.
AddAttachment
(
fileName
)
}
ctx
,
cancel
:=
context
.
WithTimeout
(
context
.
Background
(),
time
.
Second
*
30
)
defer
cancel
()
MailgunResp
,
id
,
err
:=
mg
.
Send
(
ctx
,
message
)
...
...
@@ -329,17 +349,19 @@ func worker(i int) {
log
.
Fatal
(
err
)
//alertAdmin(" MAilgun Error \n" + err.Error())
}
if
len
(
attachment
)
>
0
{
os
.
Remove
(
fileName
)
}
//fmt.Println(resp)
if
config
.
AppMod
.
Production
==
"0"
{
fmt
.
Printf
(
"Email sent ID: %s Resp: %s
\n
"
,
id
,
MailgunResp
)
}
//sendMessage(mg, sender, subject, body, recipient, cc)
mailSentTimeMailgun
:=
time
.
Now
()
.
String
()
/**
* add the email as new document in "SENT ITEMS" DB
*/
mailBody
:=
strings
.
NewReader
(
`{"from":"`
+
sender
+
`","to":"`
+
recipient
+
`","subject":"`
+
subject
+
`","body":"`
+
body
+
`"}`
)
mailBody
:=
strings
.
NewReader
(
`{"from":"`
+
sender
+
`","to":"`
+
recipient
+
`","subject":"`
+
subject
+
`","body":"`
+
body
+
`"
,"cc":"`
+
cc
+
`","bcc":"`
+
bcc
+
`","attachment":"`
+
attachment
+
`"
}`
)
req
,
mailBodyerr
:=
http
.
NewRequest
(
config
.
Requests
.
Put
,
config
.
Curl
.
AddSentMail
+
mail
.
ID
,
mailBody
)
if
mailBodyerr
!=
nil
{
alert
:=
" Error requesting newmail to add in SENTITEMS DB
\n
"
+
mailBodyerr
.
Error
()
...
...
@@ -355,7 +377,7 @@ func worker(i int) {
ioutil
.
ReadAll
(
resp
.
Body
)
mailAddedSentitems
:=
time
.
Now
()
.
String
()
logmail
(
subject
,
sender
,
recipient
,
readTimeOutbox
,
mailSentTimeMailgun
,
id
,
MailgunResp
,
mailAddedSentitems
,
deleteTimeOutbox
,
mail
.
ID
)
logmail
(
subject
,
sender
,
recipient
,
cc
,
bcc
,
attachment
,
readTimeOutbox
,
mailSentTimeMailgun
,
id
,
MailgunResp
,
mailAddedSentitems
,
deleteTimeOutbox
,
mail
.
ID
)
}
...
...
@@ -423,10 +445,33 @@ func alertAdmin(err string) {
}
/**
* Gets the file from given URL
*/
func
getFile
(
fileName
,
url
string
)
{
// Get the data
resp
,
err
:=
http
.
Get
(
url
)
if
err
!=
nil
{
panic
(
err
)
}
defer
resp
.
Body
.
Close
()
// Create the file
out
,
err
:=
os
.
Create
(
fileName
)
if
err
!=
nil
{
panic
(
err
)
}
defer
out
.
Close
()
// Write the body to file
_
,
err
=
io
.
Copy
(
out
,
resp
.
Body
)
}
/**
* log worker tasks
*/
func
logmail
(
subject
,
sender
,
recipient
,
readTimeOutbox
,
mailSentTimeMailgun
,
id
,
MailgunResp
,
mailAddedSentitems
,
deleteTimeOutbox
,
mailID
string
)
{
func
logmail
(
subject
,
sender
,
recipient
,
cc
,
bcc
,
attachment
,
readTimeOutbox
,
mailSentTimeMailgun
,
id
,
MailgunResp
,
mailAddedSentitems
,
deleteTimeOutbox
,
mailID
string
)
{
// get configurations
var
config
Config
...
...
@@ -442,6 +487,9 @@ func logmail(subject, sender, recipient, readTimeOutbox, mailSentTimeMailgun, id
logBody
:=
strings
.
NewReader
(
`{"Mail Subject":"`
+
subject
+
`","Mail sender":"`
+
sender
+
`","Mail recipiants":"`
+
recipient
+
`","Mail cc":"`
+
cc
+
`","Mail bcc":"`
+
bcc
+
`","Mail attachment":"`
+
attachment
+
`","OUTBOX read time":"`
+
readTimeOutbox
+
`","Mail sent to mailgun":"`
+
mailSentTimeMailgun
+
`","Mailgun ID":"`
+
id
+
...
...
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