Initial import of email processor

parent e48ad1d4
email
config.json
package main
import (
"bytes"
"io"
"text/template"
"gitlab.techcultivation.org/sangha/mq"
"gitlab.techcultivation.org/sangha/sangha/config"
"gitlab.techcultivation.org/sangha/sangha/db"
"github.com/muesli/gomail"
)
var (
templates = make(map[string]config.EmailTemplate)
settings config.Data
)
// TemplateHelper combines multiple db-structs to make them become
// accessible from the template
type TemplateHelper struct {
User *db.User
Payment *mq.Payment
BaseURL string
}
// SetupEmailTemplates compiles the email templates
func SetupEmailTemplates(c config.Data) {
settings = c
templates["payment_confirmation"] = config.EmailTemplate{
Subject: c.EmailTemplates.PaymentConfirmation.Subject,
Text: c.EmailTemplates.PaymentConfirmation.Text,
HTML: c.EmailTemplates.PaymentConfirmation.HTML,
}
}
func SendPaymentConfirmation(payment *mq.Payment) error {
tmpl := templates["payment_confirmation"]
th := TemplateHelper{
Payment: payment,
BaseURL: settings.Web.BaseURL,
}
m := gomail.NewMessage()
m.SetHeader("From", settings.Connections.Email.ReplyTo)
m.SetHeader("To", settings.Connections.Email.AdminEmail)
var buf bytes.Buffer
t := template.Must(template.New("payment_confirmation_subject").Parse(tmpl.Subject))
t.Execute(&buf, th)
m.SetHeader("Subject", buf.String())
m.AddAlternativeWriter("text/plain", func(w io.Writer) error {
t := template.Must(template.New("payment_confirmation_text").Parse(tmpl.Text))
return t.Execute(w, th)
})
m.AddAlternativeWriter("text/html", func(w io.Writer) error {
t := template.Must(template.New("payment_confirmation_html").Parse(tmpl.HTML))
return t.Execute(w, th)
})
d := gomail.NewDialer(settings.Connections.Email.SMTP.Server, settings.Connections.Email.SMTP.Port,
settings.Connections.Email.SMTP.User, settings.Connections.Email.SMTP.Password)
return d.DialAndSend(m)
}
package main
import (
"encoding/json"
"fmt"
log "github.com/sirupsen/logrus"
"github.com/streadway/amqp"
"gitlab.techcultivation.org/sangha/mq"
"gitlab.techcultivation.org/sangha/sangha/config"
"gitlab.techcultivation.org/sangha/sangha/logger"
)
func failOnError(err error, msg string) {
if err != nil {
log.Fatalf("%s: %s", msg, err)
panic(fmt.Sprintf("%s: %s", msg, err))
}
}
func main() {
configFile := "config.json"
logLevelStr := "info"
logLevel, err := log.ParseLevel(logLevelStr)
if err != nil {
log.Fatal(err)
}
log.SetLevel(logLevel)
config.ParseSettings(configFile)
logger.SetupLogger(config.Settings.Connections.Logger.Protocol,
config.Settings.Connections.Logger.Address,
"email-processor")
SetupEmailTemplates(*config.Settings)
log.Infoln("Starting payment email processor")
conn, err := amqp.Dial("amqp://quitty:quitty@10.0.3.135:5672/rabbit")
failOnError(err, "Failed to connect to RabbitMQ")
defer conn.Close()
ch, err := conn.Channel()
failOnError(err, "Failed to open a channel")
defer ch.Close()
msgs, err := ch.Consume(
"sandbox.payments.email", // queue
"", // consumer
false, // auto-ack
false, // exclusive
false, // no-local
false, // no-wait
nil, // args
)
failOnError(err, "Failed to register a consumer")
forever := make(chan bool)
go func() {
for d := range msgs {
p := mq.Payment{}
json.Unmarshal(d.Body, &p)
log.Printf("Received a message: %+v", p)
if err := SendPaymentConfirmation(&p); err != nil {
log.Printf("Failed sending email: %v\n", err)
continue
}
log.Printf("Sent email for payment!\n")
d.Ack(false)
}
}()
log.Println("Waiting for messages. To exit press CTRL+C")
<-forever
}
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