2014-03-19 06:27:27 -06:00
|
|
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
2017-06-06 19:14:31 -06:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2014-03-19 06:27:27 -06:00
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package mailer
|
|
|
|
|
|
|
|
import (
|
2019-02-02 19:06:52 -07:00
|
|
|
"bytes"
|
2020-05-02 17:04:31 -06:00
|
|
|
"context"
|
2014-10-09 16:08:07 -06:00
|
|
|
"crypto/tls"
|
2014-03-19 06:27:27 -06:00
|
|
|
"fmt"
|
2015-09-16 23:54:12 -06:00
|
|
|
"io"
|
2014-10-09 16:08:07 -06:00
|
|
|
"net"
|
2014-03-19 06:27:27 -06:00
|
|
|
"net/smtp"
|
2015-02-19 00:47:05 -07:00
|
|
|
"os"
|
2016-12-25 06:55:22 -07:00
|
|
|
"os/exec"
|
2014-03-19 06:27:27 -06:00
|
|
|
"strings"
|
2015-09-16 23:54:12 -06:00
|
|
|
"time"
|
|
|
|
|
2017-09-11 00:33:28 -06:00
|
|
|
"code.gitea.io/gitea/modules/base"
|
2020-01-16 10:55:36 -07:00
|
|
|
"code.gitea.io/gitea/modules/graceful"
|
2016-11-10 09:24:48 -07:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2020-05-02 17:04:31 -06:00
|
|
|
"code.gitea.io/gitea/modules/process"
|
2020-01-16 10:55:36 -07:00
|
|
|
"code.gitea.io/gitea/modules/queue"
|
2016-11-10 09:24:48 -07:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2017-06-06 19:14:31 -06:00
|
|
|
|
|
|
|
"github.com/jaytaylor/html2text"
|
|
|
|
"gopkg.in/gomail.v2"
|
2014-03-19 06:27:27 -06:00
|
|
|
)
|
|
|
|
|
2016-11-24 18:44:04 -07:00
|
|
|
// Message mail body and log info
|
2015-09-16 23:54:12 -06:00
|
|
|
type Message struct {
|
2020-01-16 10:55:36 -07:00
|
|
|
Info string // Message information for log purpose.
|
|
|
|
FromAddress string
|
|
|
|
FromDisplayName string
|
|
|
|
To []string
|
|
|
|
Subject string
|
|
|
|
Date time.Time
|
|
|
|
Body string
|
|
|
|
Headers map[string][]string
|
2015-09-16 23:54:12 -06:00
|
|
|
}
|
|
|
|
|
2020-01-16 10:55:36 -07:00
|
|
|
// ToMessage converts a Message to gomail.Message
|
|
|
|
func (m *Message) ToMessage() *gomail.Message {
|
2015-09-16 23:54:12 -06:00
|
|
|
msg := gomail.NewMessage()
|
2020-01-16 10:55:36 -07:00
|
|
|
msg.SetAddressHeader("From", m.FromAddress, m.FromDisplayName)
|
|
|
|
msg.SetHeader("To", m.To...)
|
|
|
|
for header := range m.Headers {
|
|
|
|
msg.SetHeader(header, m.Headers[header]...)
|
|
|
|
}
|
|
|
|
|
2019-04-16 22:56:40 -06:00
|
|
|
if len(setting.MailService.SubjectPrefix) > 0 {
|
2020-01-16 10:55:36 -07:00
|
|
|
msg.SetHeader("Subject", setting.MailService.SubjectPrefix+" "+m.Subject)
|
2019-04-16 22:56:40 -06:00
|
|
|
} else {
|
2020-01-16 10:55:36 -07:00
|
|
|
msg.SetHeader("Subject", m.Subject)
|
2019-04-16 22:56:40 -06:00
|
|
|
}
|
2020-01-16 10:55:36 -07:00
|
|
|
msg.SetDateHeader("Date", m.Date)
|
2019-04-02 09:45:54 -06:00
|
|
|
msg.SetHeader("X-Auto-Response-Suppress", "All")
|
2016-05-30 02:32:01 -06:00
|
|
|
|
2020-01-16 10:55:36 -07:00
|
|
|
plainBody, err := html2text.FromString(m.Body)
|
2017-06-06 19:14:31 -06:00
|
|
|
if err != nil || setting.MailService.SendAsPlainText {
|
2020-01-16 10:55:36 -07:00
|
|
|
if strings.Contains(base.TruncateString(m.Body, 100), "<html>") {
|
2017-06-06 19:14:31 -06:00
|
|
|
log.Warn("Mail contains HTML but configured to send as plain text.")
|
2016-05-30 02:50:20 -06:00
|
|
|
}
|
2017-06-06 19:14:31 -06:00
|
|
|
msg.SetBody("text/plain", plainBody)
|
|
|
|
} else {
|
|
|
|
msg.SetBody("text/plain", plainBody)
|
2020-01-16 10:55:36 -07:00
|
|
|
msg.AddAlternative("text/html", m.Body)
|
2016-05-30 02:18:49 -06:00
|
|
|
}
|
2020-01-16 10:55:36 -07:00
|
|
|
return msg
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetHeader adds additional headers to a message
|
|
|
|
func (m *Message) SetHeader(field string, value ...string) {
|
|
|
|
m.Headers[field] = value
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewMessageFrom creates new mail message object with custom From header.
|
|
|
|
func NewMessageFrom(to []string, fromDisplayName, fromAddress, subject, body string) *Message {
|
|
|
|
log.Trace("NewMessageFrom (body):\n%s", body)
|
2015-09-16 23:54:12 -06:00
|
|
|
|
|
|
|
return &Message{
|
2020-01-16 10:55:36 -07:00
|
|
|
FromAddress: fromAddress,
|
|
|
|
FromDisplayName: fromDisplayName,
|
|
|
|
To: to,
|
|
|
|
Subject: subject,
|
|
|
|
Date: time.Now(),
|
|
|
|
Body: body,
|
|
|
|
Headers: map[string][]string{},
|
2015-09-16 23:54:12 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewMessage creates new mail message object with default From header.
|
|
|
|
func NewMessage(to []string, subject, body string) *Message {
|
2017-09-20 22:29:45 -06:00
|
|
|
return NewMessageFrom(to, setting.MailService.FromName, setting.MailService.FromEmail, subject, body)
|
2015-09-16 23:54:12 -06:00
|
|
|
}
|
|
|
|
|
2015-08-19 23:56:25 -06:00
|
|
|
type loginAuth struct {
|
2015-08-20 05:12:55 -06:00
|
|
|
username, password string
|
2015-08-19 23:56:25 -06:00
|
|
|
}
|
|
|
|
|
2016-11-24 18:44:04 -07:00
|
|
|
// LoginAuth SMTP AUTH LOGIN Auth Handler
|
2015-08-19 23:56:25 -06:00
|
|
|
func LoginAuth(username, password string) smtp.Auth {
|
|
|
|
return &loginAuth{username, password}
|
|
|
|
}
|
|
|
|
|
2016-11-24 18:44:04 -07:00
|
|
|
// Start start SMTP login auth
|
2015-08-19 23:56:25 -06:00
|
|
|
func (a *loginAuth) Start(server *smtp.ServerInfo) (string, []byte, error) {
|
|
|
|
return "LOGIN", []byte{}, nil
|
|
|
|
}
|
|
|
|
|
2016-11-24 18:44:04 -07:00
|
|
|
// Next next step of SMTP login auth
|
2015-08-19 23:56:25 -06:00
|
|
|
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:
|
2017-06-17 18:30:04 -06:00
|
|
|
return nil, fmt.Errorf("unknown fromServer: %s", string(fromServer))
|
2015-08-19 23:56:25 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2016-12-25 06:55:22 -07:00
|
|
|
// Sender SMTP mail sender
|
|
|
|
type smtpSender struct {
|
2014-03-19 06:27:27 -06:00
|
|
|
}
|
|
|
|
|
2016-11-24 18:44:04 -07:00
|
|
|
// Send send email
|
2016-12-25 06:55:22 -07:00
|
|
|
func (s *smtpSender) Send(from string, to []string, msg io.WriterTo) error {
|
2015-09-16 23:54:12 -06:00
|
|
|
opts := setting.MailService
|
2014-03-19 19:05:48 -06:00
|
|
|
|
2015-09-16 23:54:12 -06:00
|
|
|
host, port, err := net.SplitHostPort(opts.Host)
|
2014-10-09 16:08:07 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-12-18 04:34:30 -07:00
|
|
|
tlsconfig := &tls.Config{
|
2015-09-16 23:54:12 -06:00
|
|
|
InsecureSkipVerify: opts.SkipVerify,
|
2014-10-09 16:08:07 -06:00
|
|
|
ServerName: host,
|
|
|
|
}
|
2014-12-18 04:34:30 -07:00
|
|
|
|
2015-09-16 23:54:12 -06:00
|
|
|
if opts.UseCertificate {
|
|
|
|
cert, err := tls.LoadX509KeyPair(opts.CertFile, opts.KeyFile)
|
2015-02-13 00:33:55 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
tlsconfig.Certificates = []tls.Certificate{cert}
|
2014-10-09 16:08:07 -06:00
|
|
|
}
|
2014-12-18 04:34:30 -07:00
|
|
|
|
2014-12-18 22:24:17 -07:00
|
|
|
conn, err := net.Dial("tcp", net.JoinHostPort(host, port))
|
|
|
|
if err != nil {
|
2014-12-18 04:34:30 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
2018-11-26 12:21:42 -07:00
|
|
|
isSecureConn := opts.IsTLSEnabled || (strings.HasSuffix(port, "465"))
|
2014-12-18 04:34:30 -07:00
|
|
|
// Start TLS directly if the port ends with 465 (SMTPS protocol)
|
2018-11-26 12:21:42 -07:00
|
|
|
if isSecureConn {
|
2014-12-18 04:34:30 -07:00
|
|
|
conn = tls.Client(conn, tlsconfig)
|
|
|
|
}
|
|
|
|
|
2014-12-18 22:24:17 -07:00
|
|
|
client, err := smtp.NewClient(conn, host)
|
|
|
|
if err != nil {
|
2015-12-19 00:44:34 -07:00
|
|
|
return fmt.Errorf("NewClient: %v", err)
|
2014-10-09 16:08:07 -06:00
|
|
|
}
|
|
|
|
|
2016-08-29 01:10:21 -06:00
|
|
|
if !opts.DisableHelo {
|
|
|
|
hostname := opts.HeloHostname
|
2015-07-03 00:08:18 -06:00
|
|
|
if len(hostname) == 0 {
|
|
|
|
hostname, err = os.Hostname()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-02-20 00:12:27 -07:00
|
|
|
|
2015-07-03 00:08:18 -06:00
|
|
|
if err = client.Hello(hostname); err != nil {
|
2015-12-19 00:44:34 -07:00
|
|
|
return fmt.Errorf("Hello: %v", err)
|
2015-07-03 00:08:18 -06:00
|
|
|
}
|
2015-02-20 00:12:27 -07:00
|
|
|
}
|
2015-02-19 00:47:05 -07:00
|
|
|
|
2017-03-14 18:52:01 -06:00
|
|
|
// If not using SMTPS, always use STARTTLS if available
|
2014-12-18 22:24:17 -07:00
|
|
|
hasStartTLS, _ := client.Extension("STARTTLS")
|
|
|
|
if !isSecureConn && hasStartTLS {
|
2014-12-18 04:34:30 -07:00
|
|
|
if err = client.StartTLS(tlsconfig); err != nil {
|
2015-12-19 00:44:34 -07:00
|
|
|
return fmt.Errorf("StartTLS: %v", err)
|
2014-12-18 04:34:30 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-18 22:24:17 -07:00
|
|
|
canAuth, options := client.Extension("AUTH")
|
2015-09-16 23:54:12 -06:00
|
|
|
if canAuth && len(opts.User) > 0 {
|
2014-12-18 04:58:18 -07:00
|
|
|
var auth smtp.Auth
|
|
|
|
|
2014-12-18 05:15:13 -07:00
|
|
|
if strings.Contains(options, "CRAM-MD5") {
|
2015-09-16 23:54:12 -06:00
|
|
|
auth = smtp.CRAMMD5Auth(opts.User, opts.Passwd)
|
2014-12-18 05:15:13 -07:00
|
|
|
} else if strings.Contains(options, "PLAIN") {
|
2015-09-16 23:54:12 -06:00
|
|
|
auth = smtp.PlainAuth("", opts.User, opts.Passwd, host)
|
2015-08-19 23:56:25 -06:00
|
|
|
} else if strings.Contains(options, "LOGIN") {
|
2015-08-20 05:12:55 -06:00
|
|
|
// Patch for AUTH LOGIN
|
2015-09-16 23:54:12 -06:00
|
|
|
auth = LoginAuth(opts.User, opts.Passwd)
|
2014-12-18 04:58:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if auth != nil {
|
|
|
|
if err = client.Auth(auth); err != nil {
|
2015-12-19 00:44:34 -07:00
|
|
|
return fmt.Errorf("Auth: %v", err)
|
2014-12-18 04:58:18 -07:00
|
|
|
}
|
2014-10-09 16:08:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-16 23:54:12 -06:00
|
|
|
if err = client.Mail(from); err != nil {
|
2015-12-19 00:44:34 -07:00
|
|
|
return fmt.Errorf("Mail: %v", err)
|
2014-10-09 16:08:07 -06:00
|
|
|
}
|
|
|
|
|
2015-09-16 23:54:12 -06:00
|
|
|
for _, rec := range to {
|
2014-10-09 16:08:07 -06:00
|
|
|
if err = client.Rcpt(rec); err != nil {
|
2015-12-19 00:44:34 -07:00
|
|
|
return fmt.Errorf("Rcpt: %v", err)
|
2014-10-09 16:08:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
w, err := client.Data()
|
|
|
|
if err != nil {
|
2015-12-19 00:44:34 -07:00
|
|
|
return fmt.Errorf("Data: %v", err)
|
2015-09-16 23:54:12 -06:00
|
|
|
} else if _, err = msg.WriteTo(w); err != nil {
|
2015-12-19 00:44:34 -07:00
|
|
|
return fmt.Errorf("WriteTo: %v", err)
|
2015-09-16 23:54:12 -06:00
|
|
|
} else if err = w.Close(); err != nil {
|
2015-12-19 00:44:34 -07:00
|
|
|
return fmt.Errorf("Close: %v", err)
|
2014-10-09 16:08:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return client.Quit()
|
|
|
|
}
|
|
|
|
|
2016-12-25 06:55:22 -07:00
|
|
|
// Sender sendmail mail sender
|
|
|
|
type sendmailSender struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send send email
|
|
|
|
func (s *sendmailSender) Send(from string, to []string, msg io.WriterTo) error {
|
|
|
|
var err error
|
|
|
|
var closeError error
|
|
|
|
var waitError error
|
|
|
|
|
2020-01-24 15:59:39 -07:00
|
|
|
args := []string{"-f", from, "-i"}
|
2017-10-25 13:27:25 -06:00
|
|
|
args = append(args, setting.MailService.SendmailArgs...)
|
2016-12-25 06:55:22 -07:00
|
|
|
args = append(args, to...)
|
|
|
|
log.Trace("Sending with: %s %v", setting.MailService.SendmailPath, args)
|
2020-05-02 17:04:31 -06:00
|
|
|
|
|
|
|
pm := process.GetManager()
|
|
|
|
desc := fmt.Sprintf("SendMail: %s %v", setting.MailService.SendmailPath, args)
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(graceful.GetManager().HammerContext(), setting.MailService.SendmailTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
cmd := exec.CommandContext(ctx, setting.MailService.SendmailPath, args...)
|
2016-12-25 06:55:22 -07:00
|
|
|
pipe, err := cmd.StdinPipe()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = cmd.Start(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-05-02 17:04:31 -06:00
|
|
|
pid := pm.Add(desc, cancel)
|
|
|
|
|
2016-12-30 00:26:05 -07:00
|
|
|
_, err = msg.WriteTo(pipe)
|
2014-03-19 06:27:27 -06:00
|
|
|
|
2016-12-25 06:55:22 -07:00
|
|
|
// we MUST close the pipe or sendmail will hang waiting for more of the message
|
|
|
|
// Also we should wait on our sendmail command even if something fails
|
|
|
|
closeError = pipe.Close()
|
2016-12-30 00:26:05 -07:00
|
|
|
waitError = cmd.Wait()
|
2020-05-02 17:04:31 -06:00
|
|
|
pm.Remove(pid)
|
2016-12-25 06:55:22 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if closeError != nil {
|
|
|
|
return closeError
|
|
|
|
} else {
|
|
|
|
return waitError
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-02 19:06:52 -07:00
|
|
|
// Sender sendmail mail sender
|
|
|
|
type dummySender struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send send email
|
|
|
|
func (s *dummySender) Send(from string, to []string, msg io.WriterTo) error {
|
|
|
|
buf := bytes.Buffer{}
|
|
|
|
if _, err := msg.WriteTo(&buf); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Info("Mail From: %s To: %v Body: %s", from, to, buf.String())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-16 10:55:36 -07:00
|
|
|
var mailQueue queue.Queue
|
2015-09-16 23:54:12 -06:00
|
|
|
|
2016-12-25 06:55:22 -07:00
|
|
|
// Sender sender for sending mail synchronously
|
|
|
|
var Sender gomail.Sender
|
|
|
|
|
2016-11-24 18:44:04 -07:00
|
|
|
// NewContext start mail queue service
|
2015-09-16 23:54:12 -06:00
|
|
|
func NewContext() {
|
2016-02-20 15:32:34 -07:00
|
|
|
// Need to check if mailQueue is nil because in during reinstall (user had installed
|
|
|
|
// before but swithed install lock off), this function will be called again
|
|
|
|
// while mail queue is already processing tasks, and produces a race condition.
|
|
|
|
if setting.MailService == nil || mailQueue != nil {
|
2015-09-16 23:54:12 -06:00
|
|
|
return
|
2014-03-19 06:27:27 -06:00
|
|
|
}
|
2015-09-16 23:54:12 -06:00
|
|
|
|
2019-02-02 19:06:52 -07:00
|
|
|
switch setting.MailService.MailerType {
|
|
|
|
case "smtp":
|
2016-12-25 06:55:22 -07:00
|
|
|
Sender = &smtpSender{}
|
2019-02-02 19:06:52 -07:00
|
|
|
case "sendmail":
|
|
|
|
Sender = &sendmailSender{}
|
|
|
|
case "dummy":
|
|
|
|
Sender = &dummySender{}
|
2016-12-25 06:55:22 -07:00
|
|
|
}
|
|
|
|
|
2020-01-16 10:55:36 -07:00
|
|
|
mailQueue = queue.CreateQueue("mail", func(data ...queue.Data) {
|
|
|
|
for _, datum := range data {
|
|
|
|
msg := datum.(*Message)
|
|
|
|
gomailMsg := msg.ToMessage()
|
|
|
|
log.Trace("New e-mail sending request %s: %s", gomailMsg.GetHeader("To"), msg.Info)
|
|
|
|
if err := gomail.Send(Sender, gomailMsg); err != nil {
|
|
|
|
log.Error("Failed to send emails %s: %s - %v", gomailMsg.GetHeader("To"), msg.Info, err)
|
|
|
|
} else {
|
|
|
|
log.Trace("E-mails sent %s: %s", gomailMsg.GetHeader("To"), msg.Info)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, &Message{})
|
|
|
|
|
|
|
|
go graceful.GetManager().RunWithShutdownFns(mailQueue.Run)
|
2014-03-19 06:27:27 -06:00
|
|
|
}
|
|
|
|
|
2019-11-18 01:08:20 -07:00
|
|
|
// SendAsync send mail asynchronously
|
2014-03-19 19:05:48 -06:00
|
|
|
func SendAsync(msg *Message) {
|
2021-04-02 04:25:13 -06:00
|
|
|
SendAsyncs([]*Message{msg})
|
2014-03-19 06:27:27 -06:00
|
|
|
}
|
2019-11-18 01:08:20 -07:00
|
|
|
|
|
|
|
// SendAsyncs send mails asynchronously
|
|
|
|
func SendAsyncs(msgs []*Message) {
|
2021-04-02 04:25:13 -06:00
|
|
|
if setting.MailService == nil {
|
|
|
|
log.Error("Mailer: SendAsyncs is being invoked but mail service hasn't been initialized")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-18 01:08:20 -07:00
|
|
|
go func() {
|
|
|
|
for _, msg := range msgs {
|
2020-01-16 10:55:36 -07:00
|
|
|
_ = mailQueue.Push(msg)
|
2019-11-18 01:08:20 -07:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|