messenger.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package messenger
  2. import (
  3. "net/textproto"
  4. "github.com/knadh/listmonk/models"
  5. )
  6. // Messenger is an interface for a generic messaging backend,
  7. // for instance, e-mail, SMS etc.
  8. type Messenger interface {
  9. Name() string
  10. Push(Message) error
  11. Flush() error
  12. Close() error
  13. }
  14. // Message is the message pushed to a Messenger.
  15. type Message struct {
  16. From string
  17. To []string
  18. Subject string
  19. ContentType string
  20. Body []byte
  21. AltBody []byte
  22. Headers textproto.MIMEHeader
  23. Attachments []Attachment
  24. Subscriber models.Subscriber
  25. // Campaign is generally the same instance for a large number of subscribers.
  26. Campaign *models.Campaign
  27. }
  28. // Attachment represents a file or blob attachment that can be
  29. // sent along with a message by a Messenger.
  30. type Attachment struct {
  31. Name string
  32. Header textproto.MIMEHeader
  33. Content []byte
  34. }
  35. // MakeAttachmentHeader is a helper function that returns a
  36. // textproto.MIMEHeader tailored for attachments, primarily
  37. // email. If no encoding is given, base64 is assumed.
  38. func MakeAttachmentHeader(filename, encoding string) textproto.MIMEHeader {
  39. if encoding == "" {
  40. encoding = "base64"
  41. }
  42. h := textproto.MIMEHeader{}
  43. h.Set("Content-Disposition", "attachment; filename="+filename)
  44. h.Set("Content-Type", "application/json; name=\""+filename+"\"")
  45. h.Set("Content-Transfer-Encoding", encoding)
  46. return h
  47. }