This commit is contained in:
谢小军
2020-08-27 22:47:29 +08:00
parent 45f53bd605
commit d6b92b5996
3 changed files with 110 additions and 0 deletions

BIN
.DS_Store vendored

Binary file not shown.

30
myemail/email.html Normal file
View File

@@ -0,0 +1,30 @@
<html>
<body>
<div style="background-color:#ECECEC; padding: 35px;">
<table cellpadding="0" align="center" style="width: 600px; margin: 0px; text-align: left; position: relative; border-top-left-radius: 5px; border-top-right-radius: 5px; border-bottom-right-radius: 5px; border-bottom-left-radius: 5px; font-size: 14px; font-family:Courier New,Bold; line-height: 1.5; box-shadow: rgb(153, 153, 153) 0px 0px 5px; border-collapse: collapse; background-position: initial initial; background-repeat: initial initial;background:#fff;">
<tbody>
<tr>
<th valign="middle" style="height: 25px; line-height: 25px; padding: 15px 35px; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #000000; background-color: #62c30b; border-top-left-radius: 5px; border-top-right-radius: 5px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px;">
<font face="Courier New" size="5" style="color: rgb(255, 255, 255); ">潞潮公社</font>
</th>
</tr>
<tr>
<td>
<div style="padding:25px 35px 40px; background-color:#fff;">
<h2 style="margin: 5px 0px; "><font color="#333333" style="line-height: 20px; ">
<font style="line-height: 22px; " size="4">新下单提醒</font></font></h2>
<p>&nbsp;</p>
<p>订单号 : %s </p>
<p>商品列表:</p>
%v
<p>请不要回复此邮件,谢谢!</p>
<p align="right">LUCHAO development center &nbsp;</p>
<p align="right">%s</p>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>

80
myemail/myemail.go Normal file
View File

@@ -0,0 +1,80 @@
package myemail
import (
"net/smtp"
"strings"
"github.com/xxjwxc/public/mylog"
"github.com/xxjwxc/public/message"
)
// myEmail ...
type myEmail struct {
user string
password string
host string
title string
}
// New 新建一个
func New(user, password, host, title string) *myEmail {
return &myEmail{
user: user,
password: password,
host: host,
title: title,
}
}
// SendMail 发送邮件
/*
to: 目的人
subject: 标题
body: 邮件内容
*/
func (e *myEmail) SendMail(to []string, subject, body string) (state bool, code int) {
// now := time.Now()
// year, mon, day := now.Date()
// hour, min, sec := now.Clock()
// dayString := fmt.Sprintf("%d-%02d-%02d %02d:%02d:%02d", year, mon, day, hour, min, sec)
// body = fmt.Sprintf(MailBodyCn, verification, dayString)
// index := strings.LastIndex(to, "@")
// if checkGbkMail(to[index+1 : len(to)]) {
// subject = mahonia.NewEncoder("gbk").ConvertString(fmt.Sprintf(MailSubjectCn, verification))
// } else {
// subject = fmt.Sprintf(MailSubjectCn, verification)
// }
err := SendToMail(e.user, e.password, e.host, e.title, subject, body, "html", to)
if err != nil {
mylog.Errorf("Send mail error:%s", err)
return false, int(message.MailSendFaild)
}
return true, int(message.NormalMessageID)
}
// SendToMail 发送邮件
/*
* user : example@example.com login smtp server user
* password: xxxxx login smtp server password
* host: smtp.example.com:port smtp.163.com:25
* to: example@example.com;example1@163.com;example2@sina.com.cn;...
* subject:The subject of mail
* body: The content of mail
* mailtyoe: mail type html or text
*/
func SendToMail(user, password, host, title, subject, body, mailtype string, to []string) error {
//d := mahonia.NewDecoder("UTF-8")
hp := strings.Split(host, ":")
auth := smtp.PlainAuth("xxj", user, password, hp[0])
contentType := "Content-Type:text/plain;charset=UTF-8"
if mailtype == "html" {
contentType = "Content-Type:text/html;charset=UTF-8"
}
msg := []byte("To:" + strings.Join(to, ";") + "\r\nFrom:" + title + "<" + user + ">\r\nSubject:" + subject + "\r\n" + contentType + "\r\n\r\n" + body)
return smtp.SendMail(host, auth, user, to, msg)
}