2014-03-19 08:27:27 -04:00
|
|
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package mailer
|
|
|
|
|
|
|
|
import (
|
2014-10-09 18:08:07 -04:00
|
|
|
"crypto/tls"
|
2014-03-19 08:27:27 -04:00
|
|
|
"fmt"
|
2014-10-09 18:08:07 -04:00
|
|
|
"net"
|
2014-03-19 08:27:27 -04:00
|
|
|
"net/smtp"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/gogits/gogs/modules/log"
|
2014-05-25 20:11:25 -04:00
|
|
|
"github.com/gogits/gogs/modules/setting"
|
2014-03-19 08:27:27 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type Message struct {
|
|
|
|
To []string
|
|
|
|
From string
|
|
|
|
Subject string
|
|
|
|
Body string
|
|
|
|
User string
|
|
|
|
Type string
|
|
|
|
Massive bool
|
|
|
|
Info string
|
|
|
|
}
|
|
|
|
|
|
|
|
// create mail content
|
|
|
|
func (m Message) Content() string {
|
|
|
|
// set mail type
|
|
|
|
contentType := "text/plain; charset=UTF-8"
|
|
|
|
if m.Type == "html" {
|
|
|
|
contentType = "text/html; charset=UTF-8"
|
|
|
|
}
|
|
|
|
|
|
|
|
// create mail content
|
2014-10-08 03:37:54 -04:00
|
|
|
content := "From: \"" + m.From + "\" <" + m.User +
|
2014-03-19 08:27:27 -04:00
|
|
|
">\r\nSubject: " + m.Subject + "\r\nContent-Type: " + contentType + "\r\n\r\n" + m.Body
|
|
|
|
return content
|
|
|
|
}
|
|
|
|
|
2014-03-19 21:05:48 -04:00
|
|
|
var mailQueue chan *Message
|
|
|
|
|
2014-03-21 01:48:10 -04:00
|
|
|
func NewMailerContext() {
|
2014-05-25 20:11:25 -04:00
|
|
|
mailQueue = make(chan *Message, setting.Cfg.MustInt("mailer", "SEND_BUFFER_LEN", 10))
|
2014-03-19 21:05:48 -04:00
|
|
|
go processMailQueue()
|
|
|
|
}
|
|
|
|
|
|
|
|
func processMailQueue() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case msg := <-mailQueue:
|
|
|
|
num, err := Send(msg)
|
|
|
|
tos := strings.Join(msg.To, "; ")
|
|
|
|
info := ""
|
|
|
|
if err != nil {
|
|
|
|
if len(msg.Info) > 0 {
|
|
|
|
info = ", info: " + msg.Info
|
|
|
|
}
|
2014-07-26 00:24:27 -04:00
|
|
|
log.Error(4, fmt.Sprintf("Async sent email %d succeed, not send emails: %s%s err: %s", num, tos, info, err))
|
2014-03-20 02:25:21 -04:00
|
|
|
} else {
|
|
|
|
log.Trace(fmt.Sprintf("Async sent email %d succeed, sent emails: %s%s", num, tos, info))
|
2014-03-19 21:05:48 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-09 18:08:07 -04:00
|
|
|
// sendMail allows mail with self-signed certificates.
|
2014-12-18 06:34:30 -05:00
|
|
|
func sendMail(settings *setting.Mailer, from string, recipients []string, msgContent []byte) error {
|
|
|
|
host, port, err := net.SplitHostPort(settings.Host)
|
2014-10-09 18:08:07 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-12-18 06:34:30 -05:00
|
|
|
if len(port) == 0 {
|
|
|
|
port = "587"
|
|
|
|
}
|
|
|
|
|
|
|
|
tlsconfig := &tls.Config{
|
|
|
|
InsecureSkipVerify: settings.SkipVerify,
|
2014-10-09 18:08:07 -04:00
|
|
|
ServerName: host,
|
|
|
|
}
|
2014-12-18 06:34:30 -05:00
|
|
|
|
|
|
|
var conn net.Conn
|
|
|
|
if conn, err = net.Dial("tcp", net.JoinHostPort(host, port)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
connection_secure := false
|
|
|
|
// Start TLS directly if the port ends with 465 (SMTPS protocol)
|
|
|
|
if strings.HasSuffix(port, "465") {
|
|
|
|
conn = tls.Client(conn, tlsconfig)
|
|
|
|
connection_secure = true
|
|
|
|
}
|
|
|
|
|
|
|
|
var client *smtp.Client
|
|
|
|
if client, err = smtp.NewClient(conn, host); err != nil {
|
2014-10-09 18:08:07 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-12-18 06:34:30 -05:00
|
|
|
// If not using SMTPS, alway use STARTTLS if available
|
|
|
|
has_starttls, _ := client.Extension("STARTTLS")
|
|
|
|
if !connection_secure && has_starttls {
|
|
|
|
if err = client.StartTLS(tlsconfig); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-18 06:58:18 -05:00
|
|
|
auth_available, options := client.Extension("AUTH")
|
2014-12-18 06:34:30 -05:00
|
|
|
|
|
|
|
if auth_available && len(settings.User) > 0 {
|
2014-12-18 06:58:18 -05:00
|
|
|
var auth smtp.Auth
|
|
|
|
|
|
|
|
if strings.Contains(options, "PLAIN") {
|
|
|
|
auth = smtp.PlainAuth("", settings.User, settings.Passwd, host)
|
|
|
|
} else if strings.Contains(options, "CRAM-MD5") {
|
|
|
|
auth = smtp.CRAMMD5Auth(settings.User, settings.Passwd)
|
|
|
|
}
|
|
|
|
|
|
|
|
if auth != nil {
|
|
|
|
if err = client.Auth(auth); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-10-09 18:08:07 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = client.Mail(from); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, rec := range recipients {
|
|
|
|
if err = client.Rcpt(rec); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
w, err := client.Data()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err = w.Write([]byte(msgContent)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = w.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return client.Quit()
|
|
|
|
}
|
|
|
|
|
2014-03-19 08:27:27 -04:00
|
|
|
// Direct Send mail message
|
2014-03-19 21:05:48 -04:00
|
|
|
func Send(msg *Message) (int, error) {
|
2014-03-19 08:27:27 -04:00
|
|
|
log.Trace("Sending mails to: %s", strings.Join(msg.To, "; "))
|
|
|
|
|
|
|
|
// get message body
|
|
|
|
content := msg.Content()
|
|
|
|
|
|
|
|
if len(msg.To) == 0 {
|
|
|
|
return 0, fmt.Errorf("empty receive emails")
|
2014-05-15 14:46:04 -04:00
|
|
|
} else if len(msg.Body) == 0 {
|
2014-03-19 08:27:27 -04:00
|
|
|
return 0, fmt.Errorf("empty email body")
|
|
|
|
}
|
|
|
|
|
|
|
|
if msg.Massive {
|
|
|
|
// send mail to multiple emails one by one
|
|
|
|
num := 0
|
|
|
|
for _, to := range msg.To {
|
|
|
|
body := []byte("To: " + to + "\r\n" + content)
|
2014-12-18 06:34:30 -05:00
|
|
|
err := sendMail(setting.MailService, msg.From, []string{to}, body)
|
2014-03-19 08:27:27 -04:00
|
|
|
if err != nil {
|
|
|
|
return num, err
|
|
|
|
}
|
|
|
|
num++
|
|
|
|
}
|
|
|
|
return num, nil
|
|
|
|
} else {
|
|
|
|
body := []byte("To: " + strings.Join(msg.To, ";") + "\r\n" + content)
|
|
|
|
|
|
|
|
// send to multiple emails in one message
|
2014-12-18 06:34:30 -05:00
|
|
|
err := sendMail(setting.MailService, msg.From, msg.To, body)
|
2014-03-19 08:27:27 -04:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
} else {
|
|
|
|
return 1, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Async Send mail message
|
2014-03-19 21:05:48 -04:00
|
|
|
func SendAsync(msg *Message) {
|
2014-03-19 08:27:27 -04:00
|
|
|
go func() {
|
2014-03-19 21:05:48 -04:00
|
|
|
mailQueue <- msg
|
2014-03-19 08:27:27 -04:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create html mail message
|
|
|
|
func NewHtmlMessage(To []string, From, Subject, Body string) Message {
|
|
|
|
return Message{
|
|
|
|
To: To,
|
|
|
|
From: From,
|
|
|
|
Subject: Subject,
|
|
|
|
Body: Body,
|
|
|
|
Type: "html",
|
|
|
|
}
|
|
|
|
}
|