You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.4 KiB

2 years ago
package message
import (
"message-demo/global"
"fmt"
"github.com/jordan-wright/email"
"log"
"net/smtp"
)
type Mail struct {
Host string `json:"host" yaml:"host"`
Protocol string `json:"protocol" yaml:"protocol"`
Port int `json:"port" yaml:"port"`
Username string `json:"username" yaml:"username"`
Password string `json:"password" yaml:"password"`
FromName string `json:"from_name" yaml:"from_name"`
To []string `json:"to" yaml:"to"`
}
var m Mail
func initMail() {
//m = Mail{
// Host: "smtp.qq.com",
// Port: 587,
// Username: "zggsong@foxmail.com",
// Password: "qlsahrhvxinybaga",
// FromName: "song<zggsong@foxmail.com>",
// To: []string{"zggsong7@163.com", "zggwanjun@gmail.com"},
//}
m = Mail{
Host: global.GLO_CONF.MailHost,
Port: global.GLO_CONF.MailPort,
Username: global.GLO_CONF.MailUser,
Password: global.GLO_CONF.MailPwd,
FromName: global.GLO_CONF.MailFromName,
To: global.GLO_CONF.MailTo,
}
Register("mail", m)
}
func (m Mail) Send(message MessageBody) {
log.Println("[mail] Sending by mail...")
e := email.NewEmail()
e.From = m.FromName
e.To = m.To
e.Subject = message.Title
e.Text = []byte(message.Content)
addr := fmt.Sprintf("%v:%v", m.Host, m.Port)
err := e.Send(addr, smtp.PlainAuth("", m.Username, m.Password, m.Host))
if err != nil {
log.Fatalf("[mail] Send failed: %v\n", err)
}
log.Printf("[mail] Send successful")
}