@ -67,22 +67,53 @@ func processMailQueue() {
}
}
// sendMail allows mail with self-signed certificates.
// sendMail allows mail with self-signed certificates.
func sendMail ( hostAddressWithPort string , auth smtp . Auth , from string , recipients [ ] string , msgContent [ ] byte ) error {
func sendMail ( settings * setting . Mailer , from string , recipients [ ] string , msgContent [ ] byte ) error {
clien t, err := smtp . Dial ( hostAddressWithPor t)
host , por t, err := net . SplitHostPort ( settings . Hos t)
if err != nil {
if err != nil {
return err
return err
}
}
host , _ , _ := net . SplitHostPort ( hostAddressWithPort )
if len ( port ) == 0 {
tlsConn := & tls . Config {
port = "587"
InsecureSkipVerify : true ,
}
tlsconfig := & tls . Config {
InsecureSkipVerify : settings . SkipVerify ,
ServerName : host ,
ServerName : host ,
}
}
if err = client . StartTLS ( tlsConn ) ; err != nil {
var conn net . Conn
if conn , err = net . Dial ( "tcp" , net . JoinHostPort ( host , port ) ) ; err != nil {
return err
}
defer conn . Close ( )
connection_secure := false
// Start TLS directly if the port ends with 465 (SMTPS protocol)
if strings . HasSuffix ( port , "465" ) {
conn = tls . Client ( conn , tlsconfig )
connection_secure = true
}
var client * smtp . Client
if client , err = smtp . NewClient ( conn , host ) ; err != nil {
return err
return err
}
}
if ok , _ := client . Extension ( "AUTH" ) ; ok && auth != nil {
// If not using SMTPS, alway use STARTTLS if available
has_starttls , _ := client . Extension ( "STARTTLS" )
if ! connection_secure && has_starttls {
if err = client . StartTLS ( tlsconfig ) ; err != nil {
return err
}
}
auth_available , _ := client . Extension ( "AUTH" )
// Possible improvement: only plain authentication is now available.
// Maybe in future CRAM MD5 as well?
if auth_available && len ( settings . User ) > 0 {
auth := smtp . PlainAuth ( "" , settings . User , settings . Passwd , host )
if err = client . Auth ( auth ) ; err != nil {
if err = client . Auth ( auth ) ; err != nil {
return err
return err
}
}
@ -116,7 +147,6 @@ func sendMail(hostAddressWithPort string, auth smtp.Auth, from string, recipient
// Direct Send mail message
// Direct Send mail message
func Send ( msg * Message ) ( int , error ) {
func Send ( msg * Message ) ( int , error ) {
log . Trace ( "Sending mails to: %s" , strings . Join ( msg . To , "; " ) )
log . Trace ( "Sending mails to: %s" , strings . Join ( msg . To , "; " ) )
host := strings . Split ( setting . MailService . Host , ":" )
// get message body
// get message body
content := msg . Content ( )
content := msg . Content ( )
@ -127,17 +157,12 @@ func Send(msg *Message) (int, error) {
return 0 , fmt . Errorf ( "empty email body" )
return 0 , fmt . Errorf ( "empty email body" )
}
}
var auth smtp . Auth
if len ( setting . MailService . Passwd ) > 0 {
auth = smtp . PlainAuth ( "" , setting . MailService . User , setting . MailService . Passwd , host [ 0 ] )
}
if msg . Massive {
if msg . Massive {
// send mail to multiple emails one by one
// send mail to multiple emails one by one
num := 0
num := 0
for _ , to := range msg . To {
for _ , to := range msg . To {
body := [ ] byte ( "To: " + to + "\r\n" + content )
body := [ ] byte ( "To: " + to + "\r\n" + content )
err := sendMail ( setting . MailService . Host , auth , msg . From , [ ] string { to } , body )
err := sendMail ( setting . MailService , msg . From , [ ] string { to } , body )
if err != nil {
if err != nil {
return num , err
return num , err
}
}
@ -148,7 +173,7 @@ func Send(msg *Message) (int, error) {
body := [ ] byte ( "To: " + strings . Join ( msg . To , ";" ) + "\r\n" + content )
body := [ ] byte ( "To: " + strings . Join ( msg . To , ";" ) + "\r\n" + content )
// send to multiple emails in one message
// send to multiple emails in one message
err := sendMail ( setting . MailService . Host , auth , msg . From , msg . To , body )
err := sendMail ( setting . MailService , msg . From , msg . To , body )
if err != nil {
if err != nil {
return 0 , err
return 0 , err
} else {
} else {