From aab56909cbf53251fa7c48b5d46451fdeecc838f Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Wed, 13 May 2020 06:11:06 +0000 Subject: [PATCH] add mailgun test --- .gitignore | 2 ++ cmd/mailer/mailer.go | 65 ++++++++++++++++++++++++++++++++++++++++++++ go.mod | 1 + go.sum | 14 ++++++++++ 4 files changed, 82 insertions(+) create mode 100644 cmd/mailer/mailer.go diff --git a/.gitignore b/.gitignore index 2ef6f89..397739c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ +.env + /public-jwks /go-mockid diff --git a/cmd/mailer/mailer.go b/cmd/mailer/mailer.go new file mode 100644 index 0000000..2c3c7ed --- /dev/null +++ b/cmd/mailer/mailer.go @@ -0,0 +1,65 @@ +package main + +import ( + "context" + "flag" + "fmt" + "os" + "time" + + mailgun "github.com/mailgun/mailgun-go/v3" + + _ "github.com/joho/godotenv/autoload" +) + +func main() { + /* + MAILGUN_API_KEY=key-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx + MAILGUN_DOMAIN=mail.example.com + MAILGUN_FROM="Rob the Robot " + */ + + to := flag.String("to", "", "message recipient in the format of 'John Doe '") + replyTo := flag.String("reply-to", "", "reply-to in the format of 'John Doe '") + subject := flag.String("subject", "Test Subject", "the utf8-encoded subject of the email") + text := flag.String( + "text", + "Testing some Mailgun awesomeness!", + "the body of the email as utf8-encoded plain-text format", + ) + flag.Parse() + + if 0 == len(*to) { + flag.Usage() + os.Exit(1) + } + + domain := os.Getenv("MAILGUN_DOMAIN") + apiKey := os.Getenv("MAILGUN_API_KEY") + from := os.Getenv("MAILGUN_FROM") + + if 0 == len(*text) { + *text = "Testing some Mailgun awesomeness!" + } + + msgId, err := SendSimpleMessage(domain, apiKey, *to, from, *subject, *text, *replyTo) + if nil != err { + panic(err) + } + fmt.Printf("Queued with Message ID %q\n", msgId) +} + +func SendSimpleMessage(domain, apiKey, to, from, subject, text, replyTo string) (string, error) { + mg := mailgun.NewMailgun(domain, apiKey) + m := mg.NewMessage(from, subject, text, to) + if 0 != len(replyTo) { + // mailgun's required "h:" prefix is added by the library + m.AddHeader("Reply-To", replyTo) + } + + ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) + defer cancel() + + _, id, err := mg.Send(ctx, m) + return id, err +} diff --git a/go.mod b/go.mod index 5ea9d89..908fba3 100644 --- a/go.mod +++ b/go.mod @@ -5,4 +5,5 @@ go 1.13 require ( git.rootprojects.org/root/keypairs v0.5.2 github.com/joho/godotenv v1.3.0 + github.com/mailgun/mailgun-go/v3 v3.6.4 ) diff --git a/go.sum b/go.sum index bb51609..737f977 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,18 @@ git.rootprojects.org/root/keypairs v0.5.2 h1:jr+drUUm/REaCDJTl5gT3kF2PwlXygcLsBZlqoKTZZw= git.rootprojects.org/root/keypairs v0.5.2/go.mod h1:WGI8PadOp+4LjUuI+wNlSwcJwFtY8L9XuNjuO3213HA= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/facebookgo/ensure v0.0.0-20160127193407-b4ab57deab51/go.mod h1:Yg+htXGokKKdzcwhuNDwVvN+uBxDGXJ7G/VN1d8fa64= +github.com/facebookgo/stack v0.0.0-20160209184415-751773369052/go.mod h1:UbMTZqLaRiH3MsBH8va0n7s1pQYcu3uTb8G4tygF4Zg= +github.com/facebookgo/subset v0.0.0-20150612182917-8dac2c3c4870/go.mod h1:5tD+neXqOorC30/tWg0LCSkrqj/AR6gu8yY8/fpw1q0= +github.com/go-chi/chi v4.0.0+incompatible h1:SiLLEDyAkqNnw+T/uDTf3aFB9T4FTrwMpuYrgaRcnW4= +github.com/go-chi/chi v4.0.0+incompatible/go.mod h1:eB3wogJHnLi3x/kFX2A+IbTBlXxmMeXJVKy9tTv1XzQ= github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc= github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg= +github.com/mailgun/mailgun-go v1.1.1 h1:mjMcm4qz+SbjAYbGJ6DKROViKtO5S0YjpuOUxQfdr2A= +github.com/mailgun/mailgun-go v2.0.0+incompatible h1:0FoRHWwMUctnd8KIR3vtZbqdfjpIMxOZgcSa51s8F8o= +github.com/mailgun/mailgun-go/v3 v3.6.4 h1:+cvbZRgLSHivbz/w1iWLmxVl6Bqf4geD2D7QMj4+8PE= +github.com/mailgun/mailgun-go/v3 v3.6.4/go.mod h1:ZjVnH8S0dR2BLjvkZc/rxwerdcirzlA12LQDuGAadR0= +github.com/mailru/easyjson v0.0.0-20180823135443-60711f1a8329 h1:2gxZ0XQIU/5z3Z3bUBu+FXuk2pFbkN6tcwi/pjyaDic= +github.com/mailru/easyjson v0.0.0-20180823135443-60711f1a8329/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= +github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= +github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=