From e5134873be1228868940c432c7c94f1e47fe556b Mon Sep 17 00:00:00 2001 From: TossPig Date: Fri, 10 Oct 2014 14:40:07 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=8F=91=E9=80=81=E9=82=AE?= =?UTF-8?q?=E4=BB=B6=E5=86=85=E5=B5=8C=E9=99=84=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 为*Email.AttachFile和Email.Attach增加了一个参数"id". 当id不为空时,设置头部信息Content-Disposition为inline,并添加Content-ID头的值为id --- utils/mail.go | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/utils/mail.go b/utils/mail.go index c7ab73d8..b598d6d1 100644 --- a/utils/mail.go +++ b/utils/mail.go @@ -157,19 +157,19 @@ func (e *Email) Bytes() ([]byte, error) { } // Add attach file to the send mail -func (e *Email) AttachFile(filename string) (a *Attachment, err error) { +func (e *Email) AttachFile(filename string, id string) (a *Attachment, err error) { f, err := os.Open(filename) if err != nil { return } ct := mime.TypeByExtension(filepath.Ext(filename)) basename := path.Base(filename) - return e.Attach(f, basename, ct) + return e.Attach(f, basename, ct, id) } // Attach is used to attach content from an io.Reader to the email. // Parameters include an io.Reader, the desired filename for the attachment, and the Content-Type. -func (e *Email) Attach(r io.Reader, filename string, c string) (a *Attachment, err error) { +func (e *Email) Attach(r io.Reader, filename string, c string, id string) (a *Attachment, err error) { var buffer bytes.Buffer if _, err = io.Copy(&buffer, r); err != nil { return @@ -186,7 +186,12 @@ func (e *Email) Attach(r io.Reader, filename string, c string) (a *Attachment, e // If the Content-Type is blank, set the Content-Type to "application/octet-stream" at.Header.Set("Content-Type", "application/octet-stream") } - at.Header.Set("Content-Disposition", fmt.Sprintf("attachment;\r\n filename=\"%s\"", filename)) + if id != "" { + at.Header.Set("Content-Disposition", fmt.Sprintf("inline;\r\n filename=\"%s\"", filename)) + at.Header.Set("Content-ID", fmt.Sprintf("<%s>", id)) + }else { + at.Header.Set("Content-Disposition", fmt.Sprintf("attachment;\r\n filename=\"%s\"", filename)) + } at.Header.Set("Content-Transfer-Encoding", "base64") e.Attachments = append(e.Attachments, at) return at, nil @@ -269,7 +274,7 @@ func qpEscape(dest []byte, c byte) { const nums = "0123456789ABCDEF" dest[0] = '=' dest[1] = nums[(c&0xf0)>>4] - dest[2] = nums[(c & 0xf)] + dest[2] = nums[(c&0xf)] } // headerToBytes enumerates the key and values in the header, and writes the results to the IO Writer