From e5134873be1228868940c432c7c94f1e47fe556b Mon Sep 17 00:00:00 2001 From: TossPig Date: Fri, 10 Oct 2014 14:40:07 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=8F=91=E9=80=81?= =?UTF-8?q?=E9=82=AE=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 From 6a33647f3065f88e568138abb9c3fa43ad967974 Mon Sep 17 00:00:00 2001 From: TossPig Date: Fri, 10 Oct 2014 23:40:02 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=8F=82=E6=95=B0?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 为了保持向后兼容, --- utils/mail.go | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/utils/mail.go b/utils/mail.go index b598d6d1..35b4b950 100644 --- a/utils/mail.go +++ b/utils/mail.go @@ -157,7 +157,17 @@ func (e *Email) Bytes() ([]byte, error) { } // Add attach file to the send mail -func (e *Email) AttachFile(filename string, id string) (a *Attachment, err error) { +func (e *Email) AttachFile(args ...string) (a *Attachment, err error) { + argsLength := len(args) + if argsLength < 1 || argsLength > 2 { + return + } + filename := args[0] + id := "" + if argsLength > 1 { + id = args[1] + } + id = args[1] f, err := os.Open(filename) if err != nil { return @@ -169,7 +179,18 @@ func (e *Email) AttachFile(filename string, id string) (a *Attachment, err error // 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, id string) (a *Attachment, err error) { +func (e *Email) Attach(r io.Reader, filename string, ci ...string) (a *Attachment, err error) { + args := ci + argsLength := len(args) + if argsLength < 1 || argsLength > 2 { + return + } + c := args[0] + id := "" + if argsLength > 1 { + id = args[1] + } + id = args[1] var buffer bytes.Buffer if _, err = io.Copy(&buffer, r); err != nil { return @@ -189,7 +210,7 @@ func (e *Email) Attach(r io.Reader, filename string, c string, id string) (a *At 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 { + } else { at.Header.Set("Content-Disposition", fmt.Sprintf("attachment;\r\n filename=\"%s\"", filename)) } at.Header.Set("Content-Transfer-Encoding", "base64") From 41de7c7db672f4f7f2d794dce608a4af83edc33b Mon Sep 17 00:00:00 2001 From: TossPig Date: Sat, 11 Oct 2014 00:02:36 +0800 Subject: [PATCH 3/5] fix MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改一个错误。 看到text/template包的写法,和你的想法是一致的。 --- utils/mail.go | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/utils/mail.go b/utils/mail.go index 35b4b950..492ad06f 100644 --- a/utils/mail.go +++ b/utils/mail.go @@ -158,16 +158,14 @@ func (e *Email) Bytes() ([]byte, error) { // Add attach file to the send mail func (e *Email) AttachFile(args ...string) (a *Attachment, err error) { - argsLength := len(args) - if argsLength < 1 || argsLength > 2 { + if len(args) < 1 || len(args) > 2 { return } filename := args[0] id := "" - if argsLength > 1 { + if len(args) > 1 { id = args[1] } - id = args[1] f, err := os.Open(filename) if err != nil { return @@ -179,18 +177,15 @@ func (e *Email) AttachFile(args ...string) (a *Attachment, err error) { // 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, ci ...string) (a *Attachment, err error) { - args := ci - argsLength := len(args) - if argsLength < 1 || argsLength > 2 { +func (e *Email) Attach(r io.Reader, filename string, args ...string) (a *Attachment, err error) { + if len(args) < 1 || len(args) > 2 { return } - c := args[0] + c := args[0] //Content-Type id := "" - if argsLength > 1 { - id = args[1] + if len(args) > 1 { + id = args[1] //Content-ID } - id = args[1] var buffer bytes.Buffer if _, err = io.Copy(&buffer, r); err != nil { return From d69eee23f016ea4f832e5e0e61355e739be07b48 Mon Sep 17 00:00:00 2001 From: TossPig Date: Sat, 11 Oct 2014 00:38:31 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E8=BF=94=E5=9B=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 不知道英文区的人能否看懂Cnglish。。。 --- utils/mail.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/utils/mail.go b/utils/mail.go index 492ad06f..e70e3586 100644 --- a/utils/mail.go +++ b/utils/mail.go @@ -158,7 +158,8 @@ func (e *Email) Bytes() ([]byte, error) { // Add attach file to the send mail func (e *Email) AttachFile(args ...string) (a *Attachment, err error) { - if len(args) < 1 || len(args) > 2 { + if len(args) < 1 && len(args) > 2 { + err = errors.New("Must specify a file name and number of parameters can not exceed at least two") return } filename := args[0] @@ -178,13 +179,14 @@ func (e *Email) AttachFile(args ...string) (a *Attachment, err error) { // 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, args ...string) (a *Attachment, err error) { - if len(args) < 1 || len(args) > 2 { + if len(args) < 1 && len(args) > 2 { + err = errors.New("Must specify a file type and number of parameters can not exceed at least two") return } - c := args[0] //Content-Type + c := args[0] //Content-Type id := "" if len(args) > 1 { - id = args[1] //Content-ID + id = args[1] //Content-ID } var buffer bytes.Buffer if _, err = io.Copy(&buffer, r); err != nil { From fc07419938e2a915fe310db6472ca98012ab34e3 Mon Sep 17 00:00:00 2001 From: TossPig Date: Sat, 11 Oct 2014 00:42:01 +0800 Subject: [PATCH 5/5] Update mail.go --- utils/mail.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/mail.go b/utils/mail.go index e70e3586..aa219626 100644 --- a/utils/mail.go +++ b/utils/mail.go @@ -180,7 +180,7 @@ func (e *Email) AttachFile(args ...string) (a *Attachment, err error) { // Parameters include an io.Reader, the desired filename for the attachment, and the Content-Type. func (e *Email) Attach(r io.Reader, filename string, args ...string) (a *Attachment, err error) { if len(args) < 1 && len(args) > 2 { - err = errors.New("Must specify a file type and number of parameters can not exceed at least two") + err = errors.New("Must specify the file type and number of parameters can not exceed at least two") return } c := args[0] //Content-Type