summaryrefslogtreecommitdiffstats
path: root/modules
diff options
context:
space:
mode:
authorUnknwon <joe2010xtmf@163.com>2014-10-09 18:08:07 -0400
committerUnknwon <joe2010xtmf@163.com>2014-10-09 18:08:07 -0400
commit39931f8e001af113d3ea0905a79f152fb93f70ec (patch)
treed7984f25c3c741b18ff72092d42262baa5efe18f /modules
parent1aa76bd27913e40780aa66fe6b6c1158e20b7bef (diff)
downloadgitea-39931f8e001af113d3ea0905a79f152fb93f70ec.tar.gz
gitea-39931f8e001af113d3ea0905a79f152fb93f70ec.zip
Allow mail with self-signed certificates
Diffstat (limited to 'modules')
-rw-r--r--modules/mailer/mailer.go53
1 files changed, 51 insertions, 2 deletions
diff --git a/modules/mailer/mailer.go b/modules/mailer/mailer.go
index 6397ccc41a..758792a351 100644
--- a/modules/mailer/mailer.go
+++ b/modules/mailer/mailer.go
@@ -5,7 +5,9 @@
package mailer
import (
+ "crypto/tls"
"fmt"
+ "net"
"net/smtp"
"strings"
@@ -64,6 +66,53 @@ func processMailQueue() {
}
}
+// 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
+ }
+
+ if 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()
+}
+
// Direct Send mail message
func Send(msg *Message) (int, error) {
log.Trace("Sending mails to: %s", strings.Join(msg.To, "; "))
@@ -85,7 +134,7 @@ func Send(msg *Message) (int, error) {
num := 0
for _, to := range msg.To {
body := []byte("To: " + to + "\r\n" + content)
- err := smtp.SendMail(setting.MailService.Host, auth, msg.From, []string{to}, body)
+ err := sendMail(setting.MailService.Host, auth, msg.From, []string{to}, body)
if err != nil {
return num, err
}
@@ -96,7 +145,7 @@ func Send(msg *Message) (int, error) {
body := []byte("To: " + strings.Join(msg.To, ";") + "\r\n" + content)
// send to multiple emails in one message
- err := smtp.SendMail(setting.MailService.Host, auth, msg.From, msg.To, body)
+ err := sendMail(setting.MailService.Host, auth, msg.From, msg.To, body)
if err != nil {
return 0, err
} else {