email.go 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package models
  2. import (
  3. "database/sql"
  4. "encoding/json"
  5. "time"
  6. )
  7. type Email struct {
  8. Id int `xorm:"id pk unsigned int autoincr notnull" json:"id"`
  9. Type int8 `xorm:"type tinyint(4) notnull default(0) comment('邮件类型,0:收到的邮件,1:发送的邮件')" json:"type"`
  10. Subject string `xorm:"subject varchar(1000) notnull default('') comment('邮件标题')" json:"subject"`
  11. ReplyTo string `xorm:"reply_to text comment('回复人')" json:"reply_to"`
  12. FromName string `xorm:"from_name varchar(50) notnull default('') comment('发件人名称')" json:"from_name"`
  13. FromAddress string `xorm:"from_address varchar(100) notnull default('') comment('发件人邮件地址')" json:"from_address"`
  14. To string `xorm:"to text comment('收件人地址')" json:"to"`
  15. Bcc string `xorm:"bcc text comment('密送')" json:"bcc"`
  16. Cc string `xorm:"cc text comment('抄送')" json:"cc"`
  17. Text sql.NullString `xorm:"text text comment('文本内容')" json:"text"`
  18. Html sql.NullString `xorm:"html mediumtext comment('html内容')" json:"html"`
  19. Sender string `xorm:"sender text comment('发送人')" json:"sender"`
  20. Attachments string `xorm:"attachments longtext comment('附件')" json:"attachments"`
  21. SPFCheck int8 `xorm:"spf_check tinyint(1) comment('spf校验是否通过')" json:"spf_check"`
  22. DKIMCheck int8 `xorm:"dkim_check tinyint(1) comment('dkim校验是否通过')" json:"dkim_check"`
  23. Status int8 `xorm:"status tinyint(4) notnull default(0) comment('0未发送,1已发送,2发送失败')" json:"status"` // 0未发送,1已发送,2发送失败
  24. CronSendTime time.Time `xorm:"cron_send_time comment('定时发送时间')" json:"cron_send_time"`
  25. UpdateTime time.Time `xorm:"update_time updated comment('更新时间')" json:"update_time"`
  26. SendUserID int `xorm:"send_user_id unsigned int notnull default(0) comment('发件人用户id')" json:"send_user_id"`
  27. Size int `xorm:"size unsigned int notnull default(1000) comment('邮件大小')" json:"size"`
  28. Error sql.NullString `xorm:"error text comment('投递错误信息')" json:"error"`
  29. SendDate time.Time `xorm:"send_date comment('投递时间')" json:"send_date"`
  30. CreateTime time.Time `xorm:"create_time created" json:"create_time"`
  31. }
  32. func (d *Email) TableName() string {
  33. return "email"
  34. }
  35. type attachments struct {
  36. Filename string
  37. ContentType string
  38. Index int
  39. //Content []byte
  40. }
  41. func (d *Email) MarshalJSON() ([]byte, error) {
  42. type Alias Email
  43. var allAtt = []attachments{}
  44. var showAtt = []attachments{}
  45. if d.Attachments != "" {
  46. _ = json.Unmarshal([]byte(d.Attachments), &allAtt)
  47. for i, att := range allAtt {
  48. att.Index = i
  49. //if att.ContentType == "application/octet-stream" {
  50. showAtt = append(showAtt, att)
  51. //}
  52. }
  53. }
  54. return json.Marshal(&struct {
  55. Alias
  56. CronSendTime string `json:"send_time"`
  57. SendDate string `json:"send_date"`
  58. UpdateTime string `json:"update_time"`
  59. CreateTime string `json:"create_time"`
  60. Text string `json:"text"`
  61. Html string `json:"html"`
  62. Error string `json:"error"`
  63. Attachments []attachments `json:"attachments"`
  64. }{
  65. Alias: (Alias)(*d),
  66. CronSendTime: d.CronSendTime.Format("2006-01-02 15:04:05"),
  67. UpdateTime: d.UpdateTime.Format("2006-01-02 15:04:05"),
  68. CreateTime: d.CreateTime.Format("2006-01-02 15:04:05"),
  69. SendDate: d.SendDate.Format("2006-01-02 15:04:05"),
  70. Text: d.Text.String,
  71. Html: d.Html.String,
  72. Error: d.Error.String,
  73. Attachments: showAtt,
  74. })
  75. }