You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

173 lines
3.7 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package mailer
  5. import (
  6. "crypto/tls"
  7. "fmt"
  8. "net"
  9. "net/smtp"
  10. "strings"
  11. "github.com/gogits/gogs/modules/log"
  12. "github.com/gogits/gogs/modules/setting"
  13. )
  14. type Message struct {
  15. To []string
  16. From string
  17. Subject string
  18. Body string
  19. User string
  20. Type string
  21. Massive bool
  22. Info string
  23. }
  24. // create mail content
  25. func (m Message) Content() string {
  26. // set mail type
  27. contentType := "text/plain; charset=UTF-8"
  28. if m.Type == "html" {
  29. contentType = "text/html; charset=UTF-8"
  30. }
  31. // create mail content
  32. content := "From: \"" + m.From + "\" <" + m.User +
  33. ">\r\nSubject: " + m.Subject + "\r\nContent-Type: " + contentType + "\r\n\r\n" + m.Body
  34. return content
  35. }
  36. var mailQueue chan *Message
  37. func NewMailerContext() {
  38. mailQueue = make(chan *Message, setting.Cfg.MustInt("mailer", "SEND_BUFFER_LEN", 10))
  39. go processMailQueue()
  40. }
  41. func processMailQueue() {
  42. for {
  43. select {
  44. case msg := <-mailQueue:
  45. num, err := Send(msg)
  46. tos := strings.Join(msg.To, "; ")
  47. info := ""
  48. if err != nil {
  49. if len(msg.Info) > 0 {
  50. info = ", info: " + msg.Info
  51. }
  52. log.Error(4, fmt.Sprintf("Async sent email %d succeed, not send emails: %s%s err: %s", num, tos, info, err))
  53. } else {
  54. log.Trace(fmt.Sprintf("Async sent email %d succeed, sent emails: %s%s", num, tos, info))
  55. }
  56. }
  57. }
  58. }
  59. // sendMail allows mail with self-signed certificates.
  60. func sendMail(hostAddressWithPort string, auth smtp.Auth, from string, recipients []string, msgContent []byte) error {
  61. client, err := smtp.Dial(hostAddressWithPort)
  62. if err != nil {
  63. return err
  64. }
  65. host, _, _ := net.SplitHostPort(hostAddressWithPort)
  66. tlsConn := &tls.Config{
  67. InsecureSkipVerify: true,
  68. ServerName: host,
  69. }
  70. if err = client.StartTLS(tlsConn); err != nil {
  71. return err
  72. }
  73. if auth != nil {
  74. if err = client.Auth(auth); err != nil {
  75. return err
  76. }
  77. }
  78. if err = client.Mail(from); err != nil {
  79. return err
  80. }
  81. for _, rec := range recipients {
  82. if err = client.Rcpt(rec); err != nil {
  83. return err
  84. }
  85. }
  86. w, err := client.Data()
  87. if err != nil {
  88. return err
  89. }
  90. if _, err = w.Write([]byte(msgContent)); err != nil {
  91. return err
  92. }
  93. if err = w.Close(); err != nil {
  94. return err
  95. }
  96. return client.Quit()
  97. }
  98. // Direct Send mail message
  99. func Send(msg *Message) (int, error) {
  100. log.Trace("Sending mails to: %s", strings.Join(msg.To, "; "))
  101. host := strings.Split(setting.MailService.Host, ":")
  102. // get message body
  103. content := msg.Content()
  104. if len(msg.To) == 0 {
  105. return 0, fmt.Errorf("empty receive emails")
  106. } else if len(msg.Body) == 0 {
  107. return 0, fmt.Errorf("empty email body")
  108. }
  109. auth := smtp.PlainAuth("", setting.MailService.User, setting.MailService.Passwd, host[0])
  110. if msg.Massive {
  111. // send mail to multiple emails one by one
  112. num := 0
  113. for _, to := range msg.To {
  114. body := []byte("To: " + to + "\r\n" + content)
  115. err := sendMail(setting.MailService.Host, auth, msg.From, []string{to}, body)
  116. if err != nil {
  117. return num, err
  118. }
  119. num++
  120. }
  121. return num, nil
  122. } else {
  123. body := []byte("To: " + strings.Join(msg.To, ";") + "\r\n" + content)
  124. // send to multiple emails in one message
  125. err := sendMail(setting.MailService.Host, auth, msg.From, msg.To, body)
  126. if err != nil {
  127. return 0, err
  128. } else {
  129. return 1, nil
  130. }
  131. }
  132. }
  133. // Async Send mail message
  134. func SendAsync(msg *Message) {
  135. go func() {
  136. mailQueue <- msg
  137. }()
  138. }
  139. // Create html mail message
  140. func NewHtmlMessage(To []string, From, Subject, Body string) Message {
  141. return Message{
  142. To: To,
  143. From: From,
  144. Subject: Subject,
  145. Body: Body,
  146. Type: "html",
  147. }
  148. }