wcj 6 年 前
コミット
c78ef977d9

+ 28 - 0
common/src/github.com/go-gomail/gomail/message.go

@@ -311,11 +311,39 @@ func (m *Message) appendFile(list []*file, name string, settings []FileSetting)
 	return append(list, f)
 }
 
+func (m *Message) appendFile_new(list []*file, fb []byte, settings []FileSetting) []*file {
+	f := &file{
+		Name:   filepath.Base(""),
+		Header: make(map[string][]string),
+		CopyFunc: func(w io.Writer) error {
+			h := bytes.NewReader(fb)
+			if _, err := io.Copy(w, h); err != nil {
+				return err
+			}
+			return nil
+		},
+	}
+
+	for _, s := range settings {
+		s(f)
+	}
+
+	if list == nil {
+		return []*file{f}
+	}
+
+	return append(list, f)
+}
+
 // Attach attaches the files to the email.
 func (m *Message) Attach(filename string, settings ...FileSetting) {
 	m.attachments = m.appendFile(m.attachments, filename, settings)
 }
 
+func (m *Message) Attach_new(fb []byte, settings ...FileSetting) {
+	m.attachments = m.appendFile_new(m.attachments, fb, settings)
+}
+
 // Embed embeds the images to the email.
 func (m *Message) Embed(filename string, settings ...FileSetting) {
 	m.embedded = m.appendFile(m.embedded, filename, settings)

+ 2 - 1
common/src/qfw/util/common.go

@@ -68,8 +68,9 @@ func DecodeArticleId2ByCheck(id string) []string {
 
 func Uuid(length int) string {
 	ret := []string{}
+	r := mathRand.New(mathRand.NewSource(time.Now().UnixNano()))
 	for i := 0; i < length; i++ {
-		index := mathRand.Intn(62)
+		index := r.Intn(62)
 		ret = append(ret, tmp[index:index+1])
 	}
 	return strings.Join(ret, "")

+ 21 - 0
common/src/qfw/util/mail/gmail.go

@@ -48,6 +48,27 @@ func GSendMail(from, to, cc, bcc, subject, body, fname, rename string, auth *Gma
 	return gSend(auth, m, to)
 }
 
+func GSendMail_B(from, to, cc, bcc, subject, body, fname string, fb []byte, auth *GmailAuth) bool {
+	m := gomail.NewMessage()
+	m.SetAddressHeader("From", auth.User, from) // 发件人
+	m.SetHeader("To",
+		m.FormatAddress(to, "收件人")) // 收件人
+	if cc != "" {
+		m.SetHeader("Cc", m.FormatAddress(cc, "收件人")) //抄送
+	}
+	if bcc != "" {
+		m.SetHeader("Bcc", m.FormatAddress(bcc, "收件人")) // 暗送
+	}
+	m.SetHeader("Subject", subject) // 主题
+	m.SetBody("text/html", body)    // 正文
+	if fname != "" {
+		h := map[string][]string{"Content-Type": {"text/plain; charset=UTF-8"}}
+		m.Attach_new(fb, gomail.Rename(fname), gomail.SetHeader(h)) //添加附件
+		//m.Attach(fname) //添加附件
+	}
+	return gSend(auth, m, to)
+}
+
 //
 func gSend(auth *GmailAuth, m *gomail.Message, to string) bool {
 	mailLock.Lock()