代码拉取完成,页面将自动刷新
package gmail
import (
"crypto/tls"
"fmt"
"log"
"time"
"gopkg.in/mail.v2"
)
type mailer struct {
dialer *mail.Dialer
}
func NewMail(host, user, password string, port int, smtpSSL bool) *mailer {
dialer := mail.NewDialer(host, port, user, password)
dialer.TLSConfig = &tls.Config{InsecureSkipVerify: true}
return &mailer{dialer: dialer}
}
func (m *mailer) log(content string) {
nowTime := time.Now().Format("15:04:05")
fmt.Printf("INFO [%s]: %s\n", nowTime, content)
}
func (m *mailer) SendMsgMail(title, msg string, receivers []string) {
for _, receiver := range receivers {
m.log(fmt.Sprintf("开始给 %s 发送邮件...", receiver))
message := mail.NewMessage()
message.SetHeader("From", m.dialer.Username)
message.SetHeader("To", receiver)
message.SetHeader("Subject", title)
message.SetBody("text/plain", msg)
if err := m.dialer.DialAndSend(message); err != nil {
log.Printf("Error: 发送邮件失败! %v\n", err)
} else {
m.log("发送成功!")
}
}
}
func (m *mailer) SendFileMail(title, msg, filePath string, receivers []string) {
for _, receiver := range receivers {
m.log(fmt.Sprintf("开始给 %s 发送邮件...", receiver))
message := mail.NewMessage()
message.SetHeader("From", m.dialer.Username)
message.SetHeader("To", receiver)
message.SetHeader("Subject", title)
message.SetBody("text/plain", msg)
message.Attach(filePath)
if err := m.dialer.DialAndSend(message); err != nil {
log.Printf("Error: 发送邮件失败! %v\n", err)
} else {
m.log("发送成功!")
}
}
}
func main() {
config := map[string]any{
"host": "smtp.1688.tech",
"user": "xxxx",
"password": "yy",
"port": 465,
"smtp_ssl": true,
}
mail := NewMail(
config["host"].(string),
config["user"].(string),
config["password"].(string),
config["port"].(int),
config["smtp_ssl"].(bool),
)
mail.SendMsgMail("test", "hello world", []string{"xx@126.com"})
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。