email.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package models
  2. import (
  3. "database/sql"
  4. "encoding/json"
  5. "pmail/dto/parsemail"
  6. "time"
  7. )
  8. type Email struct {
  9. Id int `db:"id" json:"id"`
  10. Type int8 `db:"type" json:"type"`
  11. GroupId int `db:"group_id" json:"group_id"`
  12. Subject string `db:"subject" json:"subject"`
  13. ReplyTo string `db:"reply_to" json:"reply_to"`
  14. FromName string `db:"from_name" json:"from_name"`
  15. FromAddress string `db:"from_address" json:"from_address"`
  16. To string `db:"to" json:"to"`
  17. Bcc string `db:"bcc" json:"bcc"`
  18. Cc string `db:"cc" json:"cc"`
  19. Text sql.NullString `db:"text" json:"text"`
  20. Html sql.NullString `db:"html" json:"html"`
  21. Sender string `db:"sender" json:"sender"`
  22. Attachments string `db:"attachments" json:"attachments"`
  23. SPFCheck int8 `db:"spf_check" json:"spf_check"`
  24. DKIMCheck int8 `db:"dkim_check" json:"dkim_check"`
  25. Status int8 `db:"status" json:"status"` // 0未发送,1已发送,2发送失败,3删除
  26. CronSendTime time.Time `db:"cron_send_time" json:"cron_send_time"`
  27. UpdateTime time.Time `db:"update_time" json:"update_time"`
  28. SendUserID int `db:"send_user_id" json:"send_user_id"`
  29. IsRead int8 `db:"is_read" json:"is_read"`
  30. Error sql.NullString `db:"error" json:"error"`
  31. SendDate time.Time `db:"send_date" json:"send_date"`
  32. CreateTime time.Time `db:"create_time" json:"create_time"`
  33. }
  34. type attachments struct {
  35. Filename string
  36. ContentType string
  37. Index int
  38. //Content []byte
  39. }
  40. func (d Email) GetTos() []*parsemail.User {
  41. var ret []*parsemail.User
  42. json.Unmarshal([]byte(d.To), &ret)
  43. return ret
  44. }
  45. func (d Email) GetReplyTo() []*parsemail.User {
  46. var ret []*parsemail.User
  47. json.Unmarshal([]byte(d.ReplyTo), &ret)
  48. return ret
  49. }
  50. func (d Email) GetSender() *parsemail.User {
  51. var ret *parsemail.User
  52. json.Unmarshal([]byte(d.Sender), &ret)
  53. return ret
  54. }
  55. func (d Email) GetBcc() []*parsemail.User {
  56. var ret []*parsemail.User
  57. json.Unmarshal([]byte(d.Bcc), &ret)
  58. return ret
  59. }
  60. func (d Email) GetCc() []*parsemail.User {
  61. var ret []*parsemail.User
  62. json.Unmarshal([]byte(d.Cc), &ret)
  63. return ret
  64. }
  65. func (d Email) GetAttachments() []*parsemail.Attachment {
  66. var ret []*parsemail.Attachment
  67. json.Unmarshal([]byte(d.Attachments), &ret)
  68. return ret
  69. }
  70. func (d Email) MarshalJSON() ([]byte, error) {
  71. type Alias Email
  72. var allAtt = []attachments{}
  73. var showAtt = []attachments{}
  74. if d.Attachments != "" {
  75. _ = json.Unmarshal([]byte(d.Attachments), &allAtt)
  76. for i, att := range allAtt {
  77. att.Index = i
  78. if att.ContentType == "application/octet-stream" {
  79. showAtt = append(showAtt, att)
  80. }
  81. }
  82. }
  83. return json.Marshal(&struct {
  84. Alias
  85. CronSendTime string `json:"send_time"`
  86. SendDate string `json:"send_date"`
  87. UpdateTime string `json:"update_time"`
  88. CreateTime string `json:"create_time"`
  89. Text string `json:"text"`
  90. Html string `json:"html"`
  91. Error string `json:"error"`
  92. Attachments []attachments `json:"attachments"`
  93. }{
  94. Alias: (Alias)(d),
  95. CronSendTime: d.CronSendTime.Format("2006-01-02 15:04:05"),
  96. UpdateTime: d.UpdateTime.Format("2006-01-02 15:04:05"),
  97. CreateTime: d.CreateTime.Format("2006-01-02 15:04:05"),
  98. SendDate: d.SendDate.Format("2006-01-02 15:04:05"),
  99. Text: d.Text.String,
  100. Html: d.Html.String,
  101. Error: d.Error.String,
  102. Attachments: showAtt,
  103. })
  104. }
  105. func (d Email) ToTransObj() *parsemail.Email {
  106. return &parsemail.Email{
  107. From: &parsemail.User{
  108. Name: d.FromName,
  109. EmailAddress: d.FromAddress,
  110. },
  111. To: d.GetTos(),
  112. Subject: d.Subject,
  113. Text: []byte(d.Text.String),
  114. HTML: []byte(d.Html.String),
  115. Sender: d.GetSender(),
  116. ReplyTo: d.GetReplyTo(),
  117. Bcc: d.GetBcc(),
  118. Cc: d.GetCc(),
  119. Attachments: d.GetAttachments(),
  120. Date: d.SendDate.Format("2006-01-02 15:04:05"),
  121. }
  122. }