Commit db314fd2 authored by Stephan Handuwala's avatar Stephan Handuwala 🏋🏻

Merge branch 'master' into 'master'

Master

See merge request !4
parents 100582dc ad8bc85d
File added
......@@ -19,8 +19,8 @@ requests:
header_type: Content-Type
header_json: application/json
mailgun:
domain: sandbox69b93297d9f045b5ae2383ee3b5d234c.mailgun.org
apikey: 0b6326227e0797740c93e5cfffc901c1-49a2671e-e9e05eca
domain: mailgun-bp.comsolvia.se
apikey: 68efe0d516a7f70133fee60a2cd5ceba-6140bac2-41d4c8d2
error_report:
error_sender: qa@parkwaylabs.com
error_head: Email Worker Crashed
......
......@@ -10,11 +10,14 @@ package main
*/
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"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
......@@ -114,6 +120,13 @@ func main() {
panic(configErr)
}
_, reqErrOutbox := http.NewRequest(config.Requests.Get, config.Curl.CheckMail, nil)
if reqErrOutbox != nil {
fmt.Println("Error Connectiong to CouchDB. Error is :\n" + reqErrOutbox.Error())
// panic(reqErrOutbox)
os.Exit(1)
} else {
if config.AppMod.Production == "0" {
fmt.Println("Starting the Email Manager...")
}
......@@ -256,6 +269,7 @@ func main() {
close(outboxReadTime)
close(outboxDeleteTime)
waitGroup.Wait()
}
}
func worker(i int) {
......@@ -312,16 +326,60 @@ 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"
attachReq, _ := http.NewRequest("GET", attachment, nil)
attchResp, err := http.DefaultClient.Do(attachReq)
// if
//if(attachErr != nil)
//attchResp, err := http.DefaultClient.Do(attachReq)
if err != nil {
panic(err)
}
defer attchResp.Body.Close()
respbody, _ := ioutil.ReadAll(attchResp.Body)
resp := string(respbody)
time := time.Now().UnixNano()
filename = strconv.FormatInt(time, 10) + ".pdf"
dec, err := base64.StdEncoding.DecodeString(resp)
if err != nil {
panic(err)
}
f, err := os.Create(filename)
if err != nil {
panic(err)
}
//defer f.Close()
if _, err := f.Write(dec); err != nil {
panic(err)
}
if err := f.Sync(); err != nil {
panic(err)
}
//message.AddHeader("Content-Type", "multipart/form-data")
message.AddAttachment(filename)
// message.AddReaderAttachment(filename, f)
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()
MailgunResp, id, err := mg.Send(ctx, message)
......@@ -329,17 +387,19 @@ func worker(i int) {
log.Fatal(err)
//alertAdmin(" MAilgun Error \n" + err.Error())
}
if len(filename) > 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 +415,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)
}
......@@ -426,7 +486,7 @@ func alertAdmin(err string) {
/**
* 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 +502,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 +
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment