mailgun.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package providers
  2. import (
  3. "git.wenlab.co/joe/beaconfire"
  4. h2t "github.com/jaytaylor/html2text"
  5. mailgun "github.com/mailgun/mailgun-go"
  6. )
  7. const (
  8. BEACON_NAME_MAILGUN = "mailgun"
  9. )
  10. type OptionsMailgun struct {
  11. Domain string
  12. ApiKey string
  13. DisableTracking bool
  14. From string
  15. To []string
  16. }
  17. type mailgunfire struct {
  18. opt *OptionsMailgun
  19. }
  20. var _ beaconfire.BeaconFire = &mailgunfire{}
  21. func NewMailgunfire(opt *OptionsMailgun) *mailgunfire {
  22. return &mailgunfire{opt: opt}
  23. }
  24. func (c mailgunfire) Name() string {
  25. return BEACON_NAME_MAILGUN
  26. }
  27. func (c *mailgunfire) Send(bp *beaconfire.BeaconMessage) error {
  28. if len(c.opt.To) == 0 {
  29. return beaconfire.ErrNoReceiver
  30. }
  31. mg := mailgun.NewMailgun(c.opt.Domain, c.opt.ApiKey)
  32. content := bp.Content
  33. inHtml := true //format == "html"
  34. if inHtml {
  35. if t, err := h2t.FromString(bp.Content); err == nil {
  36. content = t
  37. }
  38. }
  39. msg := mg.NewMessage(c.opt.From, bp.Title, content, c.opt.To...)
  40. if inHtml {
  41. msg.SetHtml(content)
  42. }
  43. msg.SetTracking(!c.opt.DisableTracking)
  44. msg.SetTrackingClicks(!c.opt.DisableTracking)
  45. msg.SetTrackingOpens(!c.opt.DisableTracking)
  46. _, _, err := mg.Send(msg)
  47. return err
  48. }