mirror of
https://github.com/astaxie/beego.git
synced 2024-11-22 18:50:54 +00:00
支持发送邮件内嵌附件
为*Email.AttachFile和Email.Attach增加了一个参数"id". 当id不为空时,设置头部信息Content-Disposition为inline,并添加Content-ID头的值为id
This commit is contained in:
parent
8d20ea04b0
commit
e5134873be
@ -157,19 +157,19 @@ func (e *Email) Bytes() ([]byte, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Add attach file to the send mail
|
// 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)
|
f, err := os.Open(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ct := mime.TypeByExtension(filepath.Ext(filename))
|
ct := mime.TypeByExtension(filepath.Ext(filename))
|
||||||
basename := path.Base(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.
|
// 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.
|
// 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
|
var buffer bytes.Buffer
|
||||||
if _, err = io.Copy(&buffer, r); err != nil {
|
if _, err = io.Copy(&buffer, r); err != nil {
|
||||||
return
|
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"
|
// 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-Type", "application/octet-stream")
|
||||||
}
|
}
|
||||||
|
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-Disposition", fmt.Sprintf("attachment;\r\n filename=\"%s\"", filename))
|
||||||
|
}
|
||||||
at.Header.Set("Content-Transfer-Encoding", "base64")
|
at.Header.Set("Content-Transfer-Encoding", "base64")
|
||||||
e.Attachments = append(e.Attachments, at)
|
e.Attachments = append(e.Attachments, at)
|
||||||
return at, nil
|
return at, nil
|
||||||
|
Loading…
Reference in New Issue
Block a user