Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ea19b34

Browse files
committed
发邮件 go1.9 有问题,改用 gopkg.in/gomail.v2
1 parent 72b067c commit ea19b34

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

src/logic/email.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/polaris1119/email"
2121
"github.com/polaris1119/goutils"
2222
"github.com/polaris1119/logger"
23+
gomail "gopkg.in/gomail.v2"
2324

2425
. "db"
2526
"model"
@@ -30,10 +31,30 @@ type EmailLogic struct{}
3031

3132
var DefaultEmail = EmailLogic{}
3233

33-
// SendMail 发送电子邮件
34+
// SendMail 发送电子邮件(使用 gopkg.in/gomail.v2)
3435
func (EmailLogic) SendMail(subject, content string, tos []string) (err error) {
3536
emailConfig, _ := config.ConfigFile.GetSection("email")
3637

38+
m := gomail.NewMessage()
39+
m.SetHeader("From", WebsiteSetting.Name+` <`+emailConfig["from_email"]+`>`)
40+
m.SetHeader("To", tos...)
41+
m.SetHeader("Subject", subject)
42+
m.SetBody("text/html", content)
43+
44+
d := gomail.NewDialer(emailConfig["smtp_host"], goutils.MustInt(emailConfig["smtp_port"]), emailConfig["smtp_username"], emailConfig["smtp_password"])
45+
if err = d.DialAndSend(m); err != nil {
46+
logger.Errorln("Send Mail to", strings.Join(tos, ","), "error:", err)
47+
return
48+
}
49+
50+
logger.Infoln("Send Mail to", strings.Join(tos, ","), "Successfully")
51+
return
52+
}
53+
54+
// SendMail 发送电子邮件(使用 github.com/polaris1119/email),go 1.9 tls 有问题
55+
func (EmailLogic) SendMail1(subject, content string, tos []string) (err error) {
56+
emailConfig, _ := config.ConfigFile.GetSection("email")
57+
3758
e := email.NewEmail()
3859
e.From = WebsiteSetting.Name + ` <` + emailConfig["from_email"] + `>`
3960
e.To = tos

src/logic/email_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package logic_test
2+
3+
import (
4+
. "github.com/polaris1119/config"
5+
"github.com/polaris1119/logger"
6+
7+
"logic"
8+
"testing"
9+
)
10+
11+
func TestSendMail(t *testing.T) {
12+
logger.Init(ROOT+"/log", ConfigFile.MustValue("global", "log_level", "DEBUG"))
13+
14+
err := logic.DefaultEmail.SendMail("中文test", "内容test content", []string{"[email protected]"})
15+
if err != nil {
16+
t.Error(err)
17+
} else {
18+
t.Log("successful")
19+
}
20+
}

src/vendor/manifest

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@
311311
{
312312
"importpath": "github.com/polaris1119/email",
313313
"repository": "https://github.com/polaris1119/email",
314-
"revision": "fab6eb4568abf395404252a598ff32d7fe445dbf",
314+
"revision": "809494756a1ae301b702722e00b8584bdc7d68cc",
315315
"branch": "master"
316316
},
317317
{
@@ -522,6 +522,12 @@
522522
"revision": "dafb3384ad25363d928a9e97ce4ad3a2f0667e34",
523523
"branch": "master",
524524
"path": "/transform"
525+
},
526+
{
527+
"importpath": "gopkg.in/gomail.v2",
528+
"repository": "https://gopkg.in/gomail.v2",
529+
"revision": "81ebce5c23dfd25c6c67194b37d3dd3f338c98b1",
530+
"branch": "v2"
525531
}
526532
]
527533
}

0 commit comments

Comments
 (0)