123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- // Copyright 2019 github.com. All rights reserved.
- // Use of this source code is governed by github.com.
- package thirdparty
- import (
- "gopkg.in/gomail.v2"
- "mime"
- "path"
- "strconv"
- "strings"
- "fmt"
- )
- func SendMail(user, password, host, to, subject string, files []string, content string) error {
- hostIp := ""
- hostPort := ""
- array := strings.Split(host, ":")
- if len(array) > 1 {
- hostIp = array[0]
- hostPort = array[1]
- } else {
- hostIp = host
- hostPort = "465"
- }
- mailConn := map[string]string{
- "user": user,
- "pass": password,
- "host": hostIp,
- "port": hostPort,
- }
- body := `
- <html>
- <body>
- <h3>
- content
- </h3>
- </body>
- </html>
- `
- body = strings.Replace(body, "content", content, -1)
- port, _ := strconv.Atoi(mailConn["port"])
- mailTo := strings.Split(to, ";")
- m := gomail.NewMessage()
- m.SetHeader("From", "sy"+"<"+mailConn["user"]+">")
- m.SetHeader("To", mailTo...)
- m.SetHeader("Subject", subject)
- m.SetBody("text/html", body)
- for _, v := range files {
- if v != "" {
- newname := path.Base(v)
- m.Attach(v, gomail.Rename(newname),
- gomail.SetHeader(map[string][]string{
- "Content-Disposition": []string{
- fmt.Sprintf(`attachment; filename="%s"`, mime.QEncoding.Encode("UTF-8", newname)),
- }}))
- }
- }
- d := gomail.NewDialer(mailConn["host"], port, mailConn["user"], mailConn["pass"])
- err := d.DialAndSend(m)
- return err
- }
|