bench-forgejo/modules/mailer/mailer.go

174 lines
3.7 KiB
Go
Raw Normal View History

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 (
"crypto/tls"
2014-03-19 17:57:27 +05:30
"fmt"
"net"
2014-03-19 17:57:27 +05:30
"net/smtp"
"strings"
"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
)
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 13:07:54 +05:30
content := "From: \"" + m.From + "\" <" + m.User +
2014-03-19 17:57:27 +05:30
">\r\nSubject: " + m.Subject + "\r\nContent-Type: " + contentType + "\r\n\r\n" + m.Body
return content
}
2014-03-20 06:35:48 +05:30
var mailQueue chan *Message
2014-03-21 11:18:10 +05:30
func NewMailerContext() {
2014-05-26 05:41:25 +05:30
mailQueue = make(chan *Message, setting.Cfg.MustInt("mailer", "SEND_BUFFER_LEN", 10))
2014-03-20 06:35:48 +05:30
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 09:54:27 +05:30
log.Error(4, fmt.Sprintf("Async sent email %d succeed, not send emails: %s%s err: %s", num, tos, info, err))
2014-03-20 11:55:21 +05:30
} else {
log.Trace(fmt.Sprintf("Async sent email %d succeed, sent emails: %s%s", num, tos, info))
2014-03-20 06:35:48 +05:30
}
}
}
}
// sendMail allows mail with self-signed certificates.
func sendMail(hostAddressWithPort string, auth smtp.Auth, from string, recipients []string, msgContent []byte) error {
client, err := smtp.Dial(hostAddressWithPort)
if err != nil {
return err
}
host, _, _ := net.SplitHostPort(hostAddressWithPort)
tlsConn := &tls.Config{
InsecureSkipVerify: true,
ServerName: host,
}
if err = client.StartTLS(tlsConn); err != nil {
return err
}
2014-11-01 07:22:03 +05:30
if ok, _ := client.Extension("AUTH"); ok && auth != nil {
if err = client.Auth(auth); err != nil {
return err
}
}
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 17:57:27 +05:30
// Direct Send mail message
2014-03-20 06:35:48 +05:30
func Send(msg *Message) (int, error) {
2014-03-19 17:57:27 +05:30
log.Trace("Sending mails to: %s", strings.Join(msg.To, "; "))
2014-05-26 05:41:25 +05:30
host := strings.Split(setting.MailService.Host, ":")
2014-03-19 17:57:27 +05:30
// get message body
content := msg.Content()
if len(msg.To) == 0 {
return 0, fmt.Errorf("empty receive emails")
2014-05-16 00:16:04 +05:30
} else if len(msg.Body) == 0 {
2014-03-19 17:57:27 +05:30
return 0, fmt.Errorf("empty email body")
}
2014-05-26 05:41:25 +05:30
auth := smtp.PlainAuth("", setting.MailService.User, setting.MailService.Passwd, host[0])
2014-05-16 00:16:04 +05:30
2014-03-19 17:57:27 +05:30
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)
err := sendMail(setting.MailService.Host, auth, msg.From, []string{to}, body)
2014-03-19 17:57:27 +05:30
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
err := sendMail(setting.MailService.Host, auth, msg.From, msg.To, body)
2014-03-19 17:57:27 +05:30
if err != nil {
return 0, err
} else {
return 1, nil
}
}
}
// Async Send mail message
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
}()
}
// 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",
}
}