add smtp email

This commit is contained in:
Harvey Tindall 2020-08-02 17:20:50 +01:00
parent f0be006e16
commit a38d56f362
5 changed files with 110 additions and 1 deletions

View File

@ -2,9 +2,10 @@ package main
import (
"bytes"
// "context"
"context"
"crypto/tls"
"fmt"
jEmail "github.com/jordan-wright/email"
"github.com/knz/strtime"
"github.com/mailgun/mailgun-go/v4"
"html/template"
@ -18,6 +19,8 @@ type Emailer struct {
sendType, sendMethod, fromAddr, fromName string
content Email
mg *mailgun.MailgunImpl
mime string
host string
}
type Email struct {
@ -53,11 +56,15 @@ func (email *Emailer) init(ctx *appContext) {
if email.sendMethod == "mailgun" {
email.mg = mailgun.NewMailgun(strings.Split(email.fromAddr, "@")[1], ctx.config.Section("mailgun").Key("api_key").String())
api_url := ctx.config.Section("mailgun").Key("api_url").String()
// Mailgun client takes the base url, so we need to trim off the end (e.g 'v3/messages'
if strings.Contains(api_url, "messages") {
api_url = api_url[0:strings.LastIndex(api_url, "/")]
api_url = api_url[0:strings.LastIndex(api_url, "/")]
}
email.mg.SetAPIBase(api_url)
} else if email.sendMethod == "smtp" {
ctx.host = ctx.config.Section("smtp").Key("server").String()
email.smtpAuth = smtp.PlainAuth("", email.fromAddr, ctx.config.Section("smtp").Key("password").String(), ctx.host)
}
}
@ -204,6 +211,29 @@ func (email *Emailer) send(address string, ctx *appContext) error {
if err != nil {
return err
}
} else if email.sendMethod == "smtp" {
e := jEmail.NewEmail()
e.Subject = email.content.subject
e.From = fmt.Sprintf("%s <%s>", email.fromName, email.fromAddr)
e.To = []string{address}
e.Text = []byte(email.content.text)
e.HTML = []byte(email.content.html)
smtpType := ctx.config.Section("smtp").Key("encryption").String()
tlsConfig := &tls.Config{
InsecureSkipVerify: false,
ServerName: ctx.host,
}
var err error
if smtpType == "ssl_tls" {
port := ctx.config.Section("smtp").Key("port").MustInt(465)
server := fmt.Sprintf("%s:%d", ctx.host, port)
err = e.SendWithTLS(server, email.smtpAuth, tlsConfig)
} else if smtpType == "starttls" {
port := ctx.config.Section("smtp").Key("port").MustInt(587)
server := fmt.Sprintf("%s:%d", ctx.host, port)
e.SendWithStartTLS(server, email.smtpAuth, tlsConfig)
}
return err
}
return nil
}

1
go.mod
View File

@ -10,6 +10,7 @@ require (
github.com/go-chi/chi v4.1.2+incompatible // indirect
github.com/go-playground/validator/v10 v10.3.0 // indirect
github.com/golang/protobuf v1.4.2 // indirect
github.com/jordan-wright/email v0.0.0-20200602115436-fd8a7622303e
github.com/json-iterator/go v1.1.10 // indirect
github.com/knz/strtime v0.0.0-20200318182718-be999391ffa9
github.com/lithammer/shortuuid/v3 v3.0.4

2
go.sum
View File

@ -59,6 +59,8 @@ github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/jordan-wright/email v0.0.0-20200602115436-fd8a7622303e h1:OGunVjqY7y4U4laftpEHv+mvZBlr7UGimJXKEGQtg48=
github.com/jordan-wright/email v0.0.0-20200602115436-fd8a7622303e/go.mod h1:Fy2gCFfZhay8jplf/Csj6cyH/oshQTkLQYZbKkcV+SY=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/json-iterator/go v1.1.7/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=

BIN
jfa-go Executable file

Binary file not shown.

76
nohup.out Normal file
View File

@ -0,0 +1,76 @@
(org.inkscape.Inkscape:7575): Gtk-CRITICAL **: 13:14:07.946: gtk_box_gadget_distribute: assertion 'size >= 0' failed in GdlSwitcher
(org.inkscape.Inkscape:7575): Gtk-CRITICAL **: 13:14:08.081: gtk_box_gadget_distribute: assertion 'size >= 0' failed in GdlSwitcher
(org.inkscape.Inkscape:7575): Gtk-CRITICAL **: 13:14:08.164: gtk_distribute_natural_allocation: assertion 'extra_space >= 0' failed
(org.inkscape.Inkscape:7575): Gtk-CRITICAL **: 13:14:09.844: gtk_box_gadget_distribute: assertion 'size >= 0' failed in GdlSwitcher
(org.inkscape.Inkscape:7575): Gtk-CRITICAL **: 13:14:09.920: gtk_box_gadget_distribute: assertion 'size >= 0' failed in GdlSwitcher
(org.inkscape.Inkscape:7575): Gtk-CRITICAL **: 13:14:09.967: gtk_box_gadget_distribute: assertion 'size >= 0' failed in GdlSwitcher
(org.inkscape.Inkscape:7575): Gtk-CRITICAL **: 13:14:09.982: gtk_box_gadget_distribute: assertion 'size >= 0' failed in GdlSwitcher
(org.inkscape.Inkscape:7575): Gtk-CRITICAL **: 13:14:09.999: gtk_box_gadget_distribute: assertion 'size >= 0' failed in GdlSwitcher
(org.inkscape.Inkscape:7575): Gtk-CRITICAL **: 13:14:10.016: gtk_box_gadget_distribute: assertion 'size >= 0' failed in GdlSwitcher
(org.inkscape.Inkscape:7575): Gtk-CRITICAL **: 13:14:10.032: gtk_box_gadget_distribute: assertion 'size >= 0' failed in GdlSwitcher
(org.inkscape.Inkscape:7575): Gtk-CRITICAL **: 13:14:10.049: gtk_box_gadget_distribute: assertion 'size >= 0' failed in GdlSwitcher
(org.inkscape.Inkscape:7575): Gtk-CRITICAL **: 13:14:10.066: gtk_box_gadget_distribute: assertion 'size >= 0' failed in GdlSwitcher
(org.inkscape.Inkscape:7575): Gtk-CRITICAL **: 13:14:10.083: gtk_box_gadget_distribute: assertion 'size >= 0' failed in GdlSwitcher
(org.inkscape.Inkscape:7575): Gtk-CRITICAL **: 13:14:10.099: gtk_box_gadget_distribute: assertion 'size >= 0' failed in GdlSwitcher
(org.inkscape.Inkscape:7575): Gtk-WARNING **: 13:14:11.669: Failed to measure available space: The specified location is not supported
(org.inkscape.Inkscape:7575): Gtk-CRITICAL **: 13:14:17.107: gtk_box_gadget_distribute: assertion 'size >= 0' failed in GdlSwitcher
(org.inkscape.Inkscape:7575): Gtk-CRITICAL **: 13:14:17.520: gtk_box_gadget_distribute: assertion 'size >= 0' failed in GdlSwitcher
(org.inkscape.Inkscape:7575): Gtk-CRITICAL **: 13:14:21.612: gtk_box_gadget_distribute: assertion 'size >= 0' failed in GdlSwitcher
(org.inkscape.Inkscape:7575): Gtk-CRITICAL **: 13:14:21.670: gtk_box_gadget_distribute: assertion 'size >= 0' failed in GdlSwitcher
(org.inkscape.Inkscape:7575): Gtk-CRITICAL **: 13:14:21.684: gtk_box_gadget_distribute: assertion 'size >= 0' failed in GdlSwitcher
(org.inkscape.Inkscape:7575): Gtk-CRITICAL **: 13:14:21.706: gtk_box_gadget_distribute: assertion 'size >= 0' failed in GdlSwitcher
(org.inkscape.Inkscape:7575): Gtk-CRITICAL **: 13:14:23.182: gtk_box_gadget_distribute: assertion 'size >= 0' failed in GdlSwitcher
(org.inkscape.Inkscape:7575): Gtk-CRITICAL **: 13:14:26.321: gtk_box_gadget_distribute: assertion 'size >= 0' failed in GdlSwitcher
** (org.inkscape.Inkscape:7575): WARNING **: 13:15:05.695: shape reference on non-text object
(org.inkscape.Inkscape:7575): Gtk-CRITICAL **: 13:15:05.777: gtk_box_gadget_distribute: assertion 'size >= 0' failed in GdlSwitcher
(org.inkscape.Inkscape:7575): Gtk-CRITICAL **: 13:15:05.796: gtk_box_gadget_distribute: assertion 'size >= 0' failed in GdlSwitcher
(org.inkscape.Inkscape:7575): Gtk-CRITICAL **: 13:15:05.807: gtk_box_gadget_distribute: assertion 'size >= 0' failed in GdlSwitcher
(org.inkscape.Inkscape:7575): Gtk-CRITICAL **: 13:15:05.818: gtk_box_gadget_distribute: assertion 'size >= 0' failed in GdlSwitcher
(org.inkscape.Inkscape:7575): Gtk-CRITICAL **: 13:15:05.839: gtk_box_gadget_distribute: assertion 'size >= 0' failed in GdlSwitcher
(org.inkscape.Inkscape:7575): Gtk-CRITICAL **: 13:15:05.854: gtk_box_gadget_distribute: assertion 'size >= 0' failed in GdlSwitcher
(org.inkscape.Inkscape:7575): Gtk-CRITICAL **: 13:15:05.870: gtk_box_gadget_distribute: assertion 'size >= 0' failed in GdlSwitcher
(org.inkscape.Inkscape:7575): Gtk-CRITICAL **: 13:15:05.889: gtk_box_gadget_distribute: assertion 'size >= 0' failed in GdlSwitcher
(org.inkscape.Inkscape:7575): Gtk-CRITICAL **: 13:15:05.905: gtk_box_gadget_distribute: assertion 'size >= 0' failed in GdlSwitcher
(org.inkscape.Inkscape:7575): Gtk-CRITICAL **: 13:15:05.919: gtk_box_gadget_distribute: assertion 'size >= 0' failed in GdlSwitcher
(org.inkscape.Inkscape:7575): Gtk-CRITICAL **: 13:15:05.936: gtk_box_gadget_distribute: assertion 'size >= 0' failed in GdlSwitcher
(org.inkscape.Inkscape:7575): Gtk-CRITICAL **: 13:15:05.953: gtk_box_gadget_distribute: assertion 'size >= 0' failed in GdlSwitcher
(org.inkscape.Inkscape:7575): Gtk-CRITICAL **: 13:15:06.198: gtk_box_gadget_distribute: assertion 'size >= 0' failed in GdlSwitcher
(org.inkscape.Inkscape:7575): Gtk-CRITICAL **: 13:15:08.802: gtk_box_gadget_distribute: assertion 'size >= 0' failed in GdlSwitcher