2014-03-19 17:57:27 +05:30
|
|
|
// 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-10 03:38:07 +05:30
|
|
|
"crypto/tls"
|
2014-03-19 17:57:27 +05:30
|
|
|
"fmt"
|
2015-09-17 11:24:12 +05:30
|
|
|
"io"
|
2014-10-10 03:38:07 +05:30
|
|
|
"net"
|
2014-03-19 17:57:27 +05:30
|
|
|
"net/smtp"
|
2015-02-19 13:17:05 +05:30
|
|
|
"os"
|
2014-03-19 17:57:27 +05:30
|
|
|
"strings"
|
2015-09-17 11:24:12 +05:30
|
|
|
"time"
|
|
|
|
|
|
|
|
"gopkg.in/gomail.v2"
|
2015-08-20 16:42:55 +05:30
|
|
|
|
2014-03-19 17:57:27 +05:30
|
|
|
"github.com/gogits/gogs/modules/log"
|
2014-05-26 05:41:25 +05:30
|
|
|
"github.com/gogits/gogs/modules/setting"
|
2014-03-19 17:57:27 +05:30
|
|
|
)
|
|
|
|
|
2015-09-17 11:24:12 +05:30
|
|
|
type Message struct {
|
|
|
|
Info string // Message information for log purpose.
|
|
|
|
*gomail.Message
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewMessageFrom creates new mail message object with custom From header.
|
|
|
|
func NewMessageFrom(to []string, from, subject, body string) *Message {
|
|
|
|
msg := gomail.NewMessage()
|
|
|
|
msg.SetHeader("From", from)
|
|
|
|
msg.SetHeader("To", to...)
|
|
|
|
msg.SetHeader("Subject", subject)
|
|
|
|
msg.SetDateHeader("Date", time.Now())
|
|
|
|
msg.SetBody("text/plain", body)
|
|
|
|
msg.AddAlternative("text/html", body)
|
|
|
|
|
|
|
|
return &Message{
|
|
|
|
Message: msg,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewMessage creates new mail message object with default From header.
|
|
|
|
func NewMessage(to []string, subject, body string) *Message {
|
|
|
|
return NewMessageFrom(to, setting.MailService.From, subject, body)
|
|
|
|
}
|
|
|
|
|
2015-08-20 11:26:25 +05:30
|
|
|
type loginAuth struct {
|
2015-08-20 16:42:55 +05:30
|
|
|
username, password string
|
2015-08-20 11:26:25 +05:30
|
|
|
}
|
|
|
|
|
2015-08-20 16:42:55 +05:30
|
|
|
// SMTP AUTH LOGIN Auth Handler
|
2015-08-20 11:26:25 +05:30
|
|
|
func LoginAuth(username, password string) smtp.Auth {
|
|
|
|
return &loginAuth{username, password}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *loginAuth) Start(server *smtp.ServerInfo) (string, []byte, error) {
|
|
|
|
return "LOGIN", []byte{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *loginAuth) Next(fromServer []byte, more bool) ([]byte, error) {
|
|
|
|
if more {
|
|
|
|
switch string(fromServer) {
|
|
|
|
case "Username:":
|
|
|
|
return []byte(a.username), nil
|
|
|
|
case "Password:":
|
|
|
|
return []byte(a.password), nil
|
|
|
|
default:
|
2015-08-20 16:42:55 +05:30
|
|
|
return nil, fmt.Errorf("unknwon fromServer: %s", string(fromServer))
|
2015-08-20 11:26:25 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2015-09-17 11:24:12 +05:30
|
|
|
type Sender struct {
|
2014-03-19 17:57:27 +05:30
|
|
|
}
|
|
|
|
|
2015-09-17 11:24:12 +05:30
|
|
|
func (s *Sender) Send(from string, to []string, msg io.WriterTo) error {
|
|
|
|
opts := setting.MailService
|
2014-03-20 06:35:48 +05:30
|
|
|
|
2015-09-17 11:24:12 +05:30
|
|
|
host, port, err := net.SplitHostPort(opts.Host)
|
2014-10-10 03:38:07 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-12-18 17:04:30 +05:30
|
|
|
tlsconfig := &tls.Config{
|
2015-09-17 11:24:12 +05:30
|
|
|
InsecureSkipVerify: opts.SkipVerify,
|
2014-10-10 03:38:07 +05:30
|
|
|
ServerName: host,
|
|
|
|
}
|
2014-12-18 17:04:30 +05:30
|
|
|
|
2015-09-17 11:24:12 +05:30
|
|
|
if opts.UseCertificate {
|
|
|
|
cert, err := tls.LoadX509KeyPair(opts.CertFile, opts.KeyFile)
|
2015-02-13 13:03:55 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
tlsconfig.Certificates = []tls.Certificate{cert}
|
2014-10-10 03:38:07 +05:30
|
|
|
}
|
2014-12-18 17:04:30 +05:30
|
|
|
|
2014-12-19 10:54:17 +05:30
|
|
|
conn, err := net.Dial("tcp", net.JoinHostPort(host, port))
|
|
|
|
if err != nil {
|
2014-12-18 17:04:30 +05:30
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
2014-12-19 10:54:17 +05:30
|
|
|
isSecureConn := false
|
2014-12-18 17:04:30 +05:30
|
|
|
// Start TLS directly if the port ends with 465 (SMTPS protocol)
|
|
|
|
if strings.HasSuffix(port, "465") {
|
|
|
|
conn = tls.Client(conn, tlsconfig)
|
2014-12-19 10:54:17 +05:30
|
|
|
isSecureConn = true
|
2014-12-18 17:04:30 +05:30
|
|
|
}
|
|
|
|
|
2014-12-19 10:54:17 +05:30
|
|
|
client, err := smtp.NewClient(conn, host)
|
|
|
|
if err != nil {
|
2015-12-19 13:14:34 +05:30
|
|
|
return fmt.Errorf("NewClient: %v", err)
|
2014-10-10 03:38:07 +05:30
|
|
|
}
|
|
|
|
|
2015-07-03 11:38:18 +05:30
|
|
|
if !setting.MailService.DisableHelo {
|
|
|
|
hostname := setting.MailService.HeloHostname
|
|
|
|
if len(hostname) == 0 {
|
|
|
|
hostname, err = os.Hostname()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-02-20 12:42:27 +05:30
|
|
|
|
2015-07-03 11:38:18 +05:30
|
|
|
if err = client.Hello(hostname); err != nil {
|
2015-12-19 13:14:34 +05:30
|
|
|
return fmt.Errorf("Hello: %v", err)
|
2015-07-03 11:38:18 +05:30
|
|
|
}
|
2015-02-20 12:42:27 +05:30
|
|
|
}
|
2015-02-19 13:17:05 +05:30
|
|
|
|
2014-12-18 17:04:30 +05:30
|
|
|
// If not using SMTPS, alway use STARTTLS if available
|
2014-12-19 10:54:17 +05:30
|
|
|
hasStartTLS, _ := client.Extension("STARTTLS")
|
|
|
|
if !isSecureConn && hasStartTLS {
|
2014-12-18 17:04:30 +05:30
|
|
|
if err = client.StartTLS(tlsconfig); err != nil {
|
2015-12-19 13:14:34 +05:30
|
|
|
return fmt.Errorf("StartTLS: %v", err)
|
2014-12-18 17:04:30 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-19 10:54:17 +05:30
|
|
|
canAuth, options := client.Extension("AUTH")
|
2015-09-17 11:24:12 +05:30
|
|
|
if canAuth && len(opts.User) > 0 {
|
2014-12-18 17:28:18 +05:30
|
|
|
var auth smtp.Auth
|
|
|
|
|
2014-12-18 17:45:13 +05:30
|
|
|
if strings.Contains(options, "CRAM-MD5") {
|
2015-09-17 11:24:12 +05:30
|
|
|
auth = smtp.CRAMMD5Auth(opts.User, opts.Passwd)
|
2014-12-18 17:45:13 +05:30
|
|
|
} else if strings.Contains(options, "PLAIN") {
|
2015-09-17 11:24:12 +05:30
|
|
|
auth = smtp.PlainAuth("", opts.User, opts.Passwd, host)
|
2015-08-20 11:26:25 +05:30
|
|
|
} else if strings.Contains(options, "LOGIN") {
|
2015-08-20 16:42:55 +05:30
|
|
|
// Patch for AUTH LOGIN
|
2015-09-17 11:24:12 +05:30
|
|
|
auth = LoginAuth(opts.User, opts.Passwd)
|
2014-12-18 17:28:18 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if auth != nil {
|
|
|
|
if err = client.Auth(auth); err != nil {
|
2015-12-19 13:14:34 +05:30
|
|
|
return fmt.Errorf("Auth: %v", err)
|
2014-12-18 17:28:18 +05:30
|
|
|
}
|
2014-10-10 03:38:07 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-17 11:24:12 +05:30
|
|
|
if err = client.Mail(from); err != nil {
|
2015-12-19 13:14:34 +05:30
|
|
|
return fmt.Errorf("Mail: %v", err)
|
2014-10-10 03:38:07 +05:30
|
|
|
}
|
|
|
|
|
2015-09-17 11:24:12 +05:30
|
|
|
for _, rec := range to {
|
2014-10-10 03:38:07 +05:30
|
|
|
if err = client.Rcpt(rec); err != nil {
|
2015-12-19 13:14:34 +05:30
|
|
|
return fmt.Errorf("Rcpt: %v", err)
|
2014-10-10 03:38:07 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
w, err := client.Data()
|
|
|
|
if err != nil {
|
2015-12-19 13:14:34 +05:30
|
|
|
return fmt.Errorf("Data: %v", err)
|
2015-09-17 11:24:12 +05:30
|
|
|
} else if _, err = msg.WriteTo(w); err != nil {
|
2015-12-19 13:14:34 +05:30
|
|
|
return fmt.Errorf("WriteTo: %v", err)
|
2015-09-17 11:24:12 +05:30
|
|
|
} else if err = w.Close(); err != nil {
|
2015-12-19 13:14:34 +05:30
|
|
|
return fmt.Errorf("Close: %v", err)
|
2014-10-10 03:38:07 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return client.Quit()
|
|
|
|
}
|
|
|
|
|
2015-09-17 11:24:12 +05:30
|
|
|
func processMailQueue() {
|
|
|
|
sender := &Sender{}
|
2014-03-19 17:57:27 +05:30
|
|
|
|
2015-09-17 11:24:12 +05:30
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case msg := <-mailQueue:
|
|
|
|
log.Trace("New e-mail sending request %s: %s", msg.GetHeader("To"), msg.Info)
|
|
|
|
if err := gomail.Send(sender, msg.Message); err != nil {
|
|
|
|
log.Error(4, "Fail to send e-mails %s: %s - %v", msg.GetHeader("To"), msg.Info, err)
|
|
|
|
} else {
|
|
|
|
log.Trace("E-mails sent %s: %s", msg.GetHeader("To"), msg.Info)
|
2014-03-19 17:57:27 +05:30
|
|
|
}
|
|
|
|
}
|
2015-09-17 11:24:12 +05:30
|
|
|
}
|
|
|
|
}
|
2014-03-19 17:57:27 +05:30
|
|
|
|
2015-09-17 11:24:12 +05:30
|
|
|
var mailQueue chan *Message
|
|
|
|
|
|
|
|
func NewContext() {
|
|
|
|
if setting.MailService == nil {
|
|
|
|
return
|
2014-03-19 17:57:27 +05:30
|
|
|
}
|
2015-09-17 11:24:12 +05:30
|
|
|
|
|
|
|
mailQueue = make(chan *Message, setting.MailService.QueueLength)
|
|
|
|
go processMailQueue()
|
2014-03-19 17:57:27 +05:30
|
|
|
}
|
|
|
|
|
2014-03-20 06:35:48 +05:30
|
|
|
func SendAsync(msg *Message) {
|
2014-03-19 17:57:27 +05:30
|
|
|
go func() {
|
2014-03-20 06:35:48 +05:30
|
|
|
mailQueue <- msg
|
2014-03-19 17:57:27 +05:30
|
|
|
}()
|
|
|
|
}
|