put_message.go 825 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package imaputils
  2. import (
  3. "bytes"
  4. "time"
  5. "git.sr.ht/~blallo/papero/config"
  6. )
  7. var zeroTime = time.Time{}
  8. type PutMessageOpts struct {
  9. Debug bool
  10. Mailbox string
  11. Body *bytes.Buffer
  12. Flags []string
  13. Time time.Time
  14. }
  15. func PutMessage(conf *config.AccountData, opts *PutMessageOpts) error {
  16. conn := NewConnection(conf)
  17. err := conn.Start(opts.Debug)
  18. if err != nil {
  19. return err
  20. }
  21. defer conn.Close()
  22. return PutMessageInSession(conn, opts)
  23. }
  24. func PutMessageInSession(conn *IMAPConnection, opts *PutMessageOpts) error {
  25. var err error
  26. var msgTime time.Time
  27. _, err = conn.client.Select(opts.Mailbox, false)
  28. if err != nil {
  29. return err
  30. }
  31. if opts.Time == zeroTime {
  32. msgTime = time.Now()
  33. } else {
  34. msgTime = opts.Time
  35. }
  36. return conn.client.Append(opts.Mailbox, opts.Flags, msgTime, opts.Body)
  37. }