mirror of
https://github.com/usual2970/certimate.git
synced 2025-06-08 21:49:52 +00:00
58 lines
1.2 KiB
Go
58 lines
1.2 KiB
Go
package notify
|
|
|
|
import (
|
|
"context"
|
|
"net/smtp"
|
|
)
|
|
|
|
type Mail struct {
|
|
senderAddress string
|
|
smtpHostAddr string
|
|
smtpHostPort string
|
|
smtpAuth smtp.Auth
|
|
receiverAddresses string
|
|
}
|
|
|
|
func NewMail(senderAddress, receiverAddresses, smtpHostAddr, smtpHostPort string) *Mail {
|
|
if(smtpHostPort == "") {
|
|
smtpHostPort = "25"
|
|
}
|
|
|
|
return &Mail{
|
|
senderAddress: senderAddress,
|
|
smtpHostAddr: smtpHostAddr,
|
|
smtpHostPort: smtpHostPort,
|
|
receiverAddresses: receiverAddresses,
|
|
}
|
|
}
|
|
|
|
func (m *Mail) SetAuth(username, password string) {
|
|
m.smtpAuth = smtp.PlainAuth("", username, password, m.smtpHostAddr)
|
|
}
|
|
|
|
func (m *Mail) Send(ctx context.Context, subject, message string) error {
|
|
// 构建邮件
|
|
from := m.senderAddress
|
|
to := []string{m.receiverAddresses}
|
|
msg := []byte(
|
|
"From: " + from + "\r\n" +
|
|
"To: " + m.receiverAddresses + "\r\n" +
|
|
"Subject: " + subject + "\r\n" +
|
|
"\r\n" +
|
|
message + "\r\n")
|
|
|
|
var smtpAddress string
|
|
// 组装邮箱服务器地址
|
|
if(m.smtpHostPort == "25"){
|
|
smtpAddress = m.smtpHostAddr
|
|
}else{
|
|
smtpAddress = m.smtpHostAddr + ":" + m.smtpHostPort
|
|
}
|
|
|
|
err := smtp.SendMail(smtpAddress, m.smtpAuth, from, to, msg)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
} |