Added support for SMTP TLS.

This commit is contained in:
Azareal 2019-03-03 14:00:04 +10:00
parent e9b46e1cd6
commit 41e7464f8d
2 changed files with 37 additions and 22 deletions

View File

@ -1,7 +1,7 @@
package common package common
import ( import (
"fmt" "crypto/tls"
"net/smtp" "net/smtp"
) )
@ -26,8 +26,7 @@ func SendValidationEmail(username string, email string, token string) error {
} }
// TODO: Refactor this // TODO: Refactor this
// TODO: Add support for TLS func SendEmail(email string, subject string, msg string) (err error) {
func SendEmail(email string, subject string, msg string) error {
// This hook is useful for plugin_sendmail or for testing tools. Possibly to hook it into some sort of mail server? // This hook is useful for plugin_sendmail or for testing tools. Possibly to hook it into some sort of mail server?
ret, hasHook := GetHookTable().VhookNeedHook("email_send_intercept", email, subject, msg) ret, hasHook := GetHookTable().VhookNeedHook("email_send_intercept", email, subject, msg)
if hasHook { if hasHook {
@ -35,40 +34,56 @@ func SendEmail(email string, subject string, msg string) error {
} }
body := "Subject: " + subject + "\n\n" + msg + "\n" body := "Subject: " + subject + "\n\n" + msg + "\n"
con, err := smtp.Dial(Config.SMTPServer + ":" + Config.SMTPPort) var c *smtp.Client
if err != nil { if Config.SMTPEnableTLS {
return err tlsconfig := &tls.Config{
} InsecureSkipVerify: true,
ServerName: Config.SMTPServer,
if Config.SMTPUsername != "" { }
auth := smtp.PlainAuth("", Config.SMTPUsername, Config.SMTPPassword, Config.SMTPServer) conn, err := tls.Dial("tcp", Config.SMTPServer+":"+Config.SMTPPort, tlsconfig)
err = con.Auth(auth) if err != nil {
return err
}
c, err = smtp.NewClient(conn, Config.SMTPServer)
if err != nil {
return err
}
} else {
c, err = smtp.Dial(Config.SMTPServer + ":" + Config.SMTPPort)
if err != nil { if err != nil {
return err return err
} }
} }
err = con.Mail(Site.Email) if Config.SMTPUsername != "" {
auth := smtp.PlainAuth("", Config.SMTPUsername, Config.SMTPPassword, Config.SMTPServer)
err = c.Auth(auth)
if err != nil {
return err
}
}
err = c.Mail(Site.Email)
if err != nil { if err != nil {
return err return err
} }
err = con.Rcpt(email) err = c.Rcpt(email)
if err != nil { if err != nil {
return err return err
} }
emailData, err := con.Data() w, err := c.Data()
if err != nil { if err != nil {
return err return err
} }
_, err = fmt.Fprintf(emailData, body) _, err = w.Write([]byte(body))
if err != nil { if err != nil {
return err return err
} }
err = emailData.Close() err = w.Close()
if err != nil { if err != nil {
return err return err
} }
return con.Quit() return c.Quit()
} }

View File

@ -66,11 +66,11 @@ type config struct {
TopicCache string TopicCache string
TopicCacheCapacity int TopicCacheCapacity int
SMTPServer string SMTPServer string
SMTPUsername string SMTPUsername string
SMTPPassword string SMTPPassword string
SMTPPort string SMTPPort string
//SMTPEnableTLS bool SMTPEnableTLS bool
Search string Search string