1 Star 0 Fork 0

Burning/gopackage

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
sendMail.go 1.90 KB
一键复制 编辑 原始数据 按行查看 历史
Burning 提交于 2024-11-07 14:08 . 优化更新
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"})
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/jie_python/gopackage.git
git@gitee.com:jie_python/gopackage.git
jie_python
gopackage
gopackage
v1.0.89

搜索帮助

0d507c66 1850385 C8b1a773 1850385