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.

176 lines
3.8 KiB

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 ok, _ := client.Extension("AUTH"); ok && 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. var auth smtp.Auth
  110. if len(setting.MailService.Passwd) > 0 {
  111. auth = smtp.PlainAuth("", setting.MailService.User, setting.MailService.Passwd, host[0])
  112. }
  113. if msg.Massive {
  114. // send mail to multiple emails one by one
  115. num := 0
  116. for _, to := range msg.To {
  117. body := []byte("To: " + to + "\r\n" + content)
  118. err := sendMail(setting.MailService.Host, auth, msg.From, []string{to}, body)
  119. if err != nil {
  120. return num, err
  121. }
  122. num++
  123. }
  124. return num, nil
  125. } else {
  126. body := []byte("To: " + strings.Join(msg.To, ";") + "\r\n" + content)
  127. // send to multiple emails in one message
  128. err := sendMail(setting.MailService.Host, auth, msg.From, msg.To, body)
  129. if err != nil {
  130. return 0, err
  131. } else {
  132. return 1, nil
  133. }
  134. }
  135. }
  136. // Async Send mail message
  137. func SendAsync(msg *Message) {
  138. go func() {
  139. mailQueue <- msg
  140. }()
  141. }
  142. // Create html mail message
  143. func NewHtmlMessage(To []string, From, Subject, Body string) Message {
  144. return Message{
  145. To: To,
  146. From: From,
  147. Subject: Subject,
  148. Body: Body,
  149. Type: "html",
  150. }
  151. }