package models
import (
+ "crypto/tls"
"encoding/json"
"errors"
"fmt"
}
func UpdateSource(source *LoginSource) error {
- _, err := orm.AllCols().Id(source.Id).Update(source)
+ _, err := orm.Id(source.Id).AllCols().Update(source)
return err
}
if err == nil {
return u, nil
} else {
- log.Warn("try ldap login", source.Name, "by", uname, "error:", err)
+ log.Warn("Fail to login(%s) by LDAP(%s): %v", uname, source.Name, err)
}
} else if source.Type == LT_SMTP {
u, err := LoginUserSMTPSource(nil, uname, passwd,
if err == nil {
return u, nil
} else {
- log.Warn("try smtp login", source.Name, "by", uname, "error:", err)
+ log.Warn("Fail to login(%s) by SMTP(%s): %v", uname, source.Name, err)
}
}
}
hasSource, err := orm.Id(u.LoginSource).Get(&source)
if err != nil {
return nil, err
- }
- if !hasSource {
+ } else if !hasSource {
return nil, ErrLoginSourceNotExist
- }
-
- if !source.IsActived {
+ } else if !source.IsActived {
return nil, ErrLoginSourceNotActived
}
SMTPAuths = []string{SMTP_PLAIN, SMTP_LOGIN}
)
-func SmtpAuth(addr string, a smtp.Auth, tls bool) error {
- c, err := smtp.Dial(addr)
+func SmtpAuth(host string, port int, a smtp.Auth, useTls bool) error {
+ c, err := smtp.Dial(fmt.Sprintf("%s:%d", host, port))
if err != nil {
return err
}
defer c.Close()
- if tls {
+ if err = c.Hello("gogs"); err != nil {
+ return err
+ }
+
+ if useTls {
if ok, _ := c.Extension("STARTTLS"); ok {
- if err = c.StartTLS(nil); err != nil {
+ config := &tls.Config{ServerName: host}
+ if err = c.StartTLS(config); err != nil {
return err
}
} else {
- return errors.New("smtp server unsupported tls")
+ return errors.New("SMTP server unsupported TLS")
}
}
} else if cfg.Auth == SMTP_LOGIN {
auth = LoginAuth(name, passwd)
} else {
- return nil, errors.New("Unsupported smtp auth type")
+ return nil, errors.New("Unsupported SMTP auth type")
}
- err := SmtpAuth(fmt.Sprintf("%s:%d", cfg.Host, cfg.Port), auth, cfg.TLS)
- if err != nil {
+ if err := SmtpAuth(cfg.Host, cfg.Port, auth, cfg.TLS); err != nil {
+ if strings.Contains(err.Error(), "Username and Password not accepted") {
+ return nil, ErrUserNotExist
+ }
return nil, err
}
// get message body
content := msg.Content()
- auth := smtp.PlainAuth("", base.MailService.User, base.MailService.Passwd, host[0])
-
if len(msg.To) == 0 {
return 0, fmt.Errorf("empty receive emails")
- }
-
- if len(msg.Body) == 0 {
+ } else if len(msg.Body) == 0 {
return 0, fmt.Errorf("empty email body")
}
+ auth := smtp.PlainAuth("", base.MailService.User, base.MailService.Passwd, host[0])
+
if msg.Massive {
// send mail to multiple emails one by one
num := 0
</div>
</div>
- <!-- <div class="form-group">
+ <div class="form-group">
<div class="col-md-offset-3 col-md-7">
<div class="checkbox">
<label>
<input name="tls" type="checkbox" {{if .tls}}checked{{end}}>
- <strong>Enable Register Confirmation</strong>
+ <strong>Enable TLS Encryption</strong>
</label>
</div>
</div>
- </div> -->
+ </div>
</div>
<div class="form-group">
</div>
</div>
+ <div class="panel panel-info">
+ <div class="panel-heading">
+ Tips
+ </div>
+
+ <div class="panel-body">
+ <h5>GMail Setting:</h5>
+ <p>Host: smtp.gmail.com, Post: 587, Enable TLS Encryption: true</p>
+ </div>
+ </div>
</div>
</div>
<script>