email.go 5.0 KB

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