-
Notifications
You must be signed in to change notification settings - Fork 228
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42 from chanioxaris/feature/mailgun
feat(service): Add Mailgun service
- Loading branch information
Showing
5 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -63,6 +63,7 @@ _ = notifier.Send( | |
|
||
- *Discord* | ||
- *Email* | ||
- *Mailgun* | ||
- *Microsoft Teams* | ||
- *Plivo* | ||
- *Pushbullet* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} |