mirror of
https://github.com/nikoksr/notify.git
synced 2024-11-30 08:46:43 +02:00
Merge pull request #42 from chanioxaris/feature/mailgun
feat(service): Add Mailgun service
This commit is contained in:
commit
104fe6e275
@ -63,6 +63,7 @@ _ = notifier.Send(
|
||||
|
||||
- *Discord*
|
||||
- *Email*
|
||||
- *Mailgun*
|
||||
- *Microsoft Teams*
|
||||
- *Plivo*
|
||||
- *Pushbullet*
|
||||
|
1
go.mod
1
go.mod
@ -10,6 +10,7 @@ require (
|
||||
github.com/dghubble/oauth1 v0.7.0
|
||||
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible
|
||||
github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible
|
||||
github.com/mailgun/mailgun-go/v4 v4.3.3
|
||||
github.com/pkg/errors v0.9.1
|
||||
github.com/plivo/plivo-go v5.5.1+incompatible
|
||||
github.com/sendgrid/rest v2.6.2+incompatible // indirect
|
||||
|
17
go.sum
17
go.sum
@ -15,18 +15,35 @@ github.com/dghubble/oauth1 v0.7.0 h1:AlpZdbRiJM4XGHIlQ8BuJ/wlpGwFEJNnB4Mc+78tA/w
|
||||
github.com/dghubble/oauth1 v0.7.0/go.mod h1:8pFdfPkv/jr8mkChVbNVuJ0suiHe278BtWI4Tk1ujxk=
|
||||
github.com/dghubble/sling v1.3.0 h1:pZHjCJq4zJvc6qVQ5wN1jo5oNZlNE0+8T/h0XeXBUKU=
|
||||
github.com/dghubble/sling v1.3.0/go.mod h1:XXShWaBWKzNLhu2OxikSNFrlsvowtz4kyRuXUG7oQKY=
|
||||
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/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible h1:2cauKuaELYAEARXRkq2LrJ0yDDv1rW7+wrTEdVL3uaU=
|
||||
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible/go.mod h1:qf9acutJ8cwBUhm1bqgz6Bei9/C/c93FPDljKWwsOgM=
|
||||
github.com/go-test/deep v1.0.4 h1:u2CU3YKy9I2pmu9pX0eq50wCgjfGIt539SqR7FbHiho=
|
||||
github.com/go-test/deep v1.0.4/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
|
||||
github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk=
|
||||
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
|
||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
|
||||
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
|
||||
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||
github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible h1:jdpOPRN1zP63Td1hDQbZW73xKmzDvZHzVdNYxhnTMDA=
|
||||
github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible/go.mod h1:1c7szIrayyPPB/987hsnvNzLushdWf4o/79s3P08L8A=
|
||||
github.com/json-iterator/go v1.1.10 h1:Kz6Cvnvv2wGdaG/V8yMvfkmNiXq9Ya2KUv4rouJJr68=
|
||||
github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
|
||||
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/v4 v4.3.3 h1:fS51t6ZQSkpK0t9irwHtcfzyVj4iR8u+m3oa0R+a12E=
|
||||
github.com/mailgun/mailgun-go/v4 v4.3.3/go.mod h1:fWuBI2iaS/pSSyo6+EBpHjatQO3lV8onwqcRy7joSJI=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg=
|
||||
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
||||
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/plivo/plivo-go v5.5.1+incompatible h1:LtZaUNHjSrNzBCHAe/IdDBnLGlyZB+WX18Dr+dnlVzE=
|
||||
|
51
service/mailgun/mailgun.go
Normal file
51
service/mailgun/mailgun.go
Normal file
@ -0,0 +1,51 @@
|
||||
package mailgun
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/mailgun/mailgun-go/v4"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// Mailgun struct holds necessary data to communicate with the Mailgun API.
|
||||
type Mailgun struct {
|
||||
client mailgun.Mailgun
|
||||
senderAddress string
|
||||
receiverAddresses []string
|
||||
}
|
||||
|
||||
// New returns a new instance of a Mailgun notification service.
|
||||
// You will need a Mailgun API key and domain name.
|
||||
// See https://documentation.mailgun.com/en/latest/
|
||||
func New(domain, apiKey, senderAddress string, opts ...Option) *Mailgun {
|
||||
m := &Mailgun{
|
||||
client: mailgun.NewMailgun(domain, apiKey),
|
||||
senderAddress: senderAddress,
|
||||
receiverAddresses: []string{},
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
opt(m)
|
||||
}
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
// AddReceivers takes email addresses and adds them to the internal address list. The Send method will send
|
||||
// a given message to all those addresses.
|
||||
func (m *Mailgun) AddReceivers(addresses ...string) {
|
||||
m.receiverAddresses = append(m.receiverAddresses, addresses...)
|
||||
}
|
||||
|
||||
// Send takes a message subject and a message body and sends them to all previously set chats. Message body supports
|
||||
// html as markup language.
|
||||
func (m Mailgun) Send(subject, message string) error {
|
||||
mailMessage := m.client.NewMessage(m.senderAddress, subject, message, m.receiverAddresses...)
|
||||
|
||||
_, _, err := m.client.Send(context.Background(), mailMessage)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to send mail using Mailgun service")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
15
service/mailgun/options.go
Normal file
15
service/mailgun/options.go
Normal file
@ -0,0 +1,15 @@
|
||||
package mailgun
|
||||
|
||||
import (
|
||||
"github.com/mailgun/mailgun-go/v4"
|
||||
)
|
||||
|
||||
// Option describes a functional parameter for the Mailgun constructor.
|
||||
type Option func(*Mailgun)
|
||||
|
||||
// WithEurope sets the API Mailgun base url to Europe region.
|
||||
func WithEurope() Option {
|
||||
return func(m *Mailgun) {
|
||||
m.client.SetAPIBase(mailgun.APIBaseEU)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user