Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize: change manager email configuration to SMTP_* #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions configs/pigeon.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,7 @@ servers:
system.alert.expiration.days: 30
curveadm.service.address: 127.0.0.1:11000
db.sqlite.filepath: /curve-manager/db/curvebs.db
email.addr: example@163.com
email.auth: password or authCode
smtp_address: smtp.163.com
smtp_port: 465
smtp_username: username@163.com
smtp_password: password
35 changes: 20 additions & 15 deletions internal/email/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,54 +31,59 @@ import (
)

const (
EMAIL_ADDRESS = "email.addr"
EMAIL_AUTH = "email.auth"

EMAIL_SERVER_163 = "smtp.163.com"
EMAIL_SERVER_163_ENDPOINT = "smtp.163.com:25"
SMTP_ADDRESS = "smtp.address"
SMTP_PORT = "smtp.port"
SMTP_USERNAME = "smtp.username"
SMTP_PASSWORD = "smtp.password"
EMAIL_SUBJECT_RESET_PASSWORD = "Curve-Manager Reset Password"
EMAIL_SUBJECT_ALERT = "Curve-Manager Alert"
)

var (
email_addr string
email_auth string
smtp_address string
smtp_port string
smtp_username string
smtp_password string
)

func Init(cfg *pigeon.Configure) {
email_addr = cfg.GetConfig().GetString(EMAIL_ADDRESS)
email_auth = cfg.GetConfig().GetString(EMAIL_AUTH)
smtp_address = cfg.GetConfig().GetString(SMTP_ADDRESS)
smtp_port = cfg.GetConfig().GetString(SMTP_PORT)
smtp_username = cfg.GetConfig().GetString(SMTP_USERNAME)
smtp_password = cfg.GetConfig().GetString(SMTP_PASSWORD)
}

func SendNewPassWord(name, to, passwd string) error {
if email_addr == "" || email_auth == "" {
if smtp_address == "" || smtp_port == "" || smtp_username == "" || smtp_password == "" {
return fmt.Errorf("the manage email info not set")
}
content := fmt.Sprintf("The password of Curve-Manager has been reset successfully.\n"+
"UserName: %s\nNewPassWord: %s\n", name, passwd)

e := email.Email{
From: email_addr,
From: smtp_username,
To: []string{to},
Subject: EMAIL_SUBJECT_RESET_PASSWORD,
Text: []byte(content),
}
return e.Send(EMAIL_SERVER_163_ENDPOINT, smtp.PlainAuth("", email_addr, email_auth, EMAIL_SERVER_163))
return e.Send(fmt.Sprintf("%v:%v", smtp_address, smtp_port),
smtp.PlainAuth("", smtp_username, smtp_password, smtp_address))
}

func SendAlert2Users(content string, tos []string) error {
if email_addr == "" || email_auth == "" {
if smtp_address == "" || smtp_port == "" || smtp_username == "" || smtp_password == "" {
return fmt.Errorf("the manage email info not set")
}
var errors error
for _, to := range tos {
e := email.Email{
From: email_addr,
From: smtp_username,
To: []string{to},
Subject: EMAIL_SUBJECT_ALERT,
Text: []byte(content),
}
err := e.Send(EMAIL_SERVER_163_ENDPOINT, smtp.PlainAuth("", email_addr, email_auth, EMAIL_SERVER_163))
err := e.Send(fmt.Sprintf("%v:%v", smtp_address, smtp_port),
smtp.PlainAuth("", smtp_username, smtp_password, smtp_address))
if err != nil {
errors = fmt.Errorf("dest: %s, error: %s, %s", to, err.Error(), errors.Error())
}
Expand Down