email.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 `xorm:"id pk unsigned int autoincr notnull" json:"id"`
  10. Type int8 `xorm:"type tinyint(4) notnull default(0) comment('邮件类型,0:收到的邮件,1:发送的邮件')" json:"type"`
  11. GroupId int `xorm:"group_id int notnull default(0) comment('分组id')'" json:"group_id"`
  12. Subject string `xorm:"subject varchar(1000) notnull default('') comment('邮件标题')" json:"subject"`
  13. ReplyTo string `xorm:"reply_to text comment('回复人')" json:"reply_to"`
  14. FromName string `xorm:"from_name varchar(50) notnull default('') comment('发件人名称')" json:"from_name"`
  15. FromAddress string `xorm:"from_address varchar(100) notnull default('') comment('发件人邮件地址')" json:"from_address"`
  16. To string `xorm:"to text comment('收件人地址')" json:"to"`
  17. Bcc string `xorm:"bcc text comment('密送')" json:"bcc"`
  18. Cc string `xorm:"cc text comment('抄送')" json:"cc"`
  19. Text sql.NullString `xorm:"text text comment('文本内容')" json:"text"`
  20. Html sql.NullString `xorm:"html mediumtext comment('html内容')" json:"html"`
  21. Sender string `xorm:"sender text comment('发送人')" json:"sender"`
  22. Attachments string `xorm:"attachments longtext comment('附件')" json:"attachments"`
  23. SPFCheck int8 `xorm:"spf_check tinyint(1) comment('spf校验是否通过')" json:"spf_check"`
  24. DKIMCheck int8 `xorm:"dkim_check tinyint(1) comment('dkim校验是否通过')" json:"dkim_check"`
  25. Status int8 `xorm:"status tinyint(4) notnull default(0) comment('0未发送,1已发送,2发送失败,3删除')" json:"status"` // 0未发送,1已发送,2发送失败,3删除
  26. CronSendTime time.Time `xorm:"cron_send_time comment('定时发送时间')" json:"cron_send_time"`
  27. UpdateTime time.Time `xorm:"update_time updated comment('更新时间')" json:"update_time"`
  28. SendUserID int `xorm:"send_user_id unsigned int notnull default(0) comment('发件人用户id')" json:"send_user_id"`
  29. IsRead int8 `xorm:"is_read tinyint(1) comment('是否已读')" json:"is_read"`
  30. Error sql.NullString `xorm:"error text comment('投递错误信息')" json:"error"`
  31. SendDate time.Time `xorm:"send_date comment('投递时间')" json:"send_date"`
  32. CreateTime time.Time `xorm:"create_time created" json:"create_time"`
  33. }
  34. func (d Email) TableName() string {
  35. return "email"
  36. }
  37. type attachments struct {
  38. Filename string
  39. ContentType string
  40. Index int
  41. //Content []byte
  42. }
  43. func (d Email) GetTos() []*parsemail.User {
  44. var ret []*parsemail.User
  45. json.Unmarshal([]byte(d.To), &ret)
  46. return ret
  47. }
  48. func (d Email) GetReplyTo() []*parsemail.User {
  49. var ret []*parsemail.User
  50. json.Unmarshal([]byte(d.ReplyTo), &ret)
  51. return ret
  52. }
  53. func (d Email) GetSender() *parsemail.User {
  54. var ret *parsemail.User
  55. json.Unmarshal([]byte(d.Sender), &ret)
  56. return ret
  57. }
  58. func (d Email) GetBcc() []*parsemail.User {
  59. var ret []*parsemail.User
  60. json.Unmarshal([]byte(d.Bcc), &ret)
  61. return ret
  62. }
  63. func (d Email) GetCc() []*parsemail.User {
  64. var ret []*parsemail.User
  65. json.Unmarshal([]byte(d.Cc), &ret)
  66. return ret
  67. }
  68. func (d Email) GetAttachments() []*parsemail.Attachment {
  69. var ret []*parsemail.Attachment
  70. json.Unmarshal([]byte(d.Attachments), &ret)
  71. return ret
  72. }
  73. func (d Email) MarshalJSON() ([]byte, error) {
  74. type Alias Email
  75. var allAtt = []attachments{}
  76. var showAtt = []attachments{}
  77. if d.Attachments != "" {
  78. _ = json.Unmarshal([]byte(d.Attachments), &allAtt)
  79. for i, att := range allAtt {
  80. att.Index = i
  81. if att.ContentType == "application/octet-stream" {
  82. showAtt = append(showAtt, att)
  83. }
  84. }
  85. }
  86. return json.Marshal(&struct {
  87. Alias
  88. CronSendTime string `json:"send_time"`
  89. SendDate string `json:"send_date"`
  90. UpdateTime string `json:"update_time"`
  91. CreateTime string `json:"create_time"`
  92. Text string `json:"text"`
  93. Html string `json:"html"`
  94. Error string `json:"error"`
  95. Attachments []attachments `json:"attachments"`
  96. }{
  97. Alias: (Alias)(d),
  98. CronSendTime: d.CronSendTime.Format("2006-01-02 15:04:05"),
  99. UpdateTime: d.UpdateTime.Format("2006-01-02 15:04:05"),
  100. CreateTime: d.CreateTime.Format("2006-01-02 15:04:05"),
  101. SendDate: d.SendDate.Format("2006-01-02 15:04:05"),
  102. Text: d.Text.String,
  103. Html: d.Html.String,
  104. Error: d.Error.String,
  105. Attachments: showAtt,
  106. })
  107. }
  108. func (d Email) ToTransObj() *parsemail.Email {
  109. return &parsemail.Email{
  110. From: &parsemail.User{
  111. Name: d.FromName,
  112. EmailAddress: d.FromAddress,
  113. },
  114. To: d.GetTos(),
  115. Subject: d.Subject,
  116. Text: []byte(d.Text.String),
  117. HTML: []byte(d.Html.String),
  118. Sender: d.GetSender(),
  119. ReplyTo: d.GetReplyTo(),
  120. Bcc: d.GetBcc(),
  121. Cc: d.GetCc(),
  122. Attachments: d.GetAttachments(),
  123. Date: d.SendDate.Format("2006-01-02 15:04:05"),
  124. }
  125. }