email_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package parsemail
  2. import (
  3. "bytes"
  4. "fmt"
  5. "github.com/emersion/go-message"
  6. log "github.com/sirupsen/logrus"
  7. "io"
  8. "os"
  9. "pmail/config"
  10. "pmail/db"
  11. "pmail/session"
  12. "testing"
  13. "time"
  14. )
  15. func testInit() {
  16. // 设置日志格式为json格式
  17. //log.SetFormatter(&log.JSONFormatter{})
  18. log.SetReportCaller(true)
  19. log.SetFormatter(&log.TextFormatter{
  20. //以下设置只是为了使输出更美观
  21. DisableColors: true,
  22. TimestampFormat: "2006-01-02 15:03:04",
  23. })
  24. // 设置将日志输出到标准输出(默认的输出为stderr,标准错误)
  25. // 日志消息输出可以是任意的io.writer类型
  26. log.SetOutput(os.Stdout)
  27. // 设置日志级别为warn以上
  28. log.SetLevel(log.TraceLevel)
  29. var cst, _ = time.LoadLocation("Asia/Shanghai")
  30. time.Local = cst
  31. config.Init()
  32. Init()
  33. db.Init()
  34. session.Init()
  35. }
  36. func TestEmail_domainMatch(t *testing.T) {
  37. //e := &Email{}
  38. //dnsNames := []string{
  39. // "*.mail.qq.com",
  40. // "993.dav.qq.com",
  41. // "993.eas.qq.com",
  42. // "993.imap.qq.com",
  43. // "993.pop.qq.com",
  44. // "993.smtp.qq.com",
  45. // "imap.qq.com",
  46. // "mx1.qq.com",
  47. // "mx2.qq.com",
  48. // "mx3.qq.com",
  49. // "pop.qq.com",
  50. // "smtp.qq.com",
  51. // "mail.qq.com",
  52. //}
  53. //
  54. //fmt.Println(e.domainMatch("", dnsNames))
  55. //fmt.Println(e.domainMatch("xjiangwei.cn", dnsNames))
  56. //fmt.Println(e.domainMatch("qq.com", dnsNames))
  57. //fmt.Println(e.domainMatch("test.aaa.mail.qq.com", dnsNames))
  58. //fmt.Println(e.domainMatch("smtp.qq.com", dnsNames))
  59. //fmt.Println(e.domainMatch("pop.qq.com", dnsNames))
  60. //fmt.Println(e.domainMatch("test.mail.qq.com", dnsNames))
  61. }
  62. func Test_buildUser(t *testing.T) {
  63. u := buildUser("Jinnrry N <jiangwei1995910@gmail.com>")
  64. if u.EmailAddress != "jiangwei1995910@gmail.com" {
  65. t.Error("error")
  66. }
  67. if u.Name != "Jinnrry N" {
  68. t.Error("error")
  69. }
  70. u = buildUser("=?UTF-8?B?YWRtaW5AamlubnJyeS5jb20=?=<admin@jinnrry.com>")
  71. if u.EmailAddress != "admin@jinnrry.com" {
  72. t.Error("error")
  73. }
  74. if u.Name != "admin@jinnrry.com" {
  75. t.Error("error")
  76. }
  77. u = buildUser("\"admin@jinnrry.com\" <admin@jinnrry.com>")
  78. if u.EmailAddress != "admin@jinnrry.com" {
  79. t.Error("error")
  80. }
  81. if u.Name != "admin@jinnrry.com" {
  82. t.Error("error")
  83. }
  84. }
  85. func TestEmailBuidlers(t *testing.T) {
  86. var b bytes.Buffer
  87. var h message.Header
  88. h.SetContentType("multipart/alternative", nil)
  89. w, err := message.CreateWriter(&b, h)
  90. if err != nil {
  91. }
  92. var h1 message.Header
  93. h1.SetContentType("text/html", nil)
  94. w1, err := w.CreatePart(h1)
  95. if err != nil {
  96. }
  97. io.WriteString(w1, "<h1>Hello World!</h1><p>This is an HTML part.</p>")
  98. w1.Close()
  99. var h2 message.Header
  100. h2.SetContentType("text/plain", nil)
  101. w2, err := w.CreatePart(h2)
  102. if err != nil {
  103. }
  104. io.WriteString(w2, "Hello World!\n\nThis is a text part.")
  105. w2.Close()
  106. w.Close()
  107. fmt.Println(b.String())
  108. }
  109. func TestEmail_builder(t *testing.T) {
  110. testInit()
  111. e := Email{
  112. From: buildUser("i@test.com"),
  113. To: buildUsers([]string{"to@test.com"}),
  114. Subject: "Title",
  115. HTML: []byte("Html"),
  116. Text: []byte("Text"),
  117. Attachments: []*Attachment{
  118. {
  119. Filename: "a.png",
  120. ContentType: "image/jpeg",
  121. Content: []byte("aaa"),
  122. ContentID: "1",
  123. },
  124. },
  125. }
  126. rest := e.BuildBytes(nil, false)
  127. fmt.Println(string(rest))
  128. }