支持发送邮件内嵌附件
为*Email.AttachFile和Email.Attach增加了一个参数"id". 当id不为空时,设置头部信息Content-Disposition为inline,并添加Content-ID头的值为id
Showing
1 changed file
with
9 additions
and
4 deletions
| ... | @@ -157,19 +157,19 @@ func (e *Email) Bytes() ([]byte, error) { | ... | @@ -157,19 +157,19 @@ func (e *Email) Bytes() ([]byte, error) { |
| 157 | } | 157 | } |
| 158 | 158 | ||
| 159 | // Add attach file to the send mail | 159 | // Add attach file to the send mail |
| 160 | func (e *Email) AttachFile(filename string) (a *Attachment, err error) { | 160 | func (e *Email) AttachFile(filename string, id string) (a *Attachment, err error) { |
| 161 | f, err := os.Open(filename) | 161 | f, err := os.Open(filename) |
| 162 | if err != nil { | 162 | if err != nil { |
| 163 | return | 163 | return |
| 164 | } | 164 | } |
| 165 | ct := mime.TypeByExtension(filepath.Ext(filename)) | 165 | ct := mime.TypeByExtension(filepath.Ext(filename)) |
| 166 | basename := path.Base(filename) | 166 | basename := path.Base(filename) |
| 167 | return e.Attach(f, basename, ct) | 167 | return e.Attach(f, basename, ct, id) |
| 168 | } | 168 | } |
| 169 | 169 | ||
| 170 | // Attach is used to attach content from an io.Reader to the email. | 170 | // Attach is used to attach content from an io.Reader to the email. |
| 171 | // Parameters include an io.Reader, the desired filename for the attachment, and the Content-Type. | 171 | // Parameters include an io.Reader, the desired filename for the attachment, and the Content-Type. |
| 172 | func (e *Email) Attach(r io.Reader, filename string, c string) (a *Attachment, err error) { | 172 | func (e *Email) Attach(r io.Reader, filename string, c string, id string) (a *Attachment, err error) { |
| 173 | var buffer bytes.Buffer | 173 | var buffer bytes.Buffer |
| 174 | if _, err = io.Copy(&buffer, r); err != nil { | 174 | if _, err = io.Copy(&buffer, r); err != nil { |
| 175 | return | 175 | return |
| ... | @@ -186,7 +186,12 @@ func (e *Email) Attach(r io.Reader, filename string, c string) (a *Attachment, e | ... | @@ -186,7 +186,12 @@ func (e *Email) Attach(r io.Reader, filename string, c string) (a *Attachment, e |
| 186 | // If the Content-Type is blank, set the Content-Type to "application/octet-stream" | 186 | // If the Content-Type is blank, set the Content-Type to "application/octet-stream" |
| 187 | at.Header.Set("Content-Type", "application/octet-stream") | 187 | at.Header.Set("Content-Type", "application/octet-stream") |
| 188 | } | 188 | } |
| 189 | if id != "" { | ||
| 190 | at.Header.Set("Content-Disposition", fmt.Sprintf("inline;\r\n filename=\"%s\"", filename)) | ||
| 191 | at.Header.Set("Content-ID", fmt.Sprintf("<%s>", id)) | ||
| 192 | }else { | ||
| 189 | at.Header.Set("Content-Disposition", fmt.Sprintf("attachment;\r\n filename=\"%s\"", filename)) | 193 | at.Header.Set("Content-Disposition", fmt.Sprintf("attachment;\r\n filename=\"%s\"", filename)) |
| 194 | } | ||
| 190 | at.Header.Set("Content-Transfer-Encoding", "base64") | 195 | at.Header.Set("Content-Transfer-Encoding", "base64") |
| 191 | e.Attachments = append(e.Attachments, at) | 196 | e.Attachments = append(e.Attachments, at) |
| 192 | return at, nil | 197 | return at, nil |
| ... | @@ -269,7 +274,7 @@ func qpEscape(dest []byte, c byte) { | ... | @@ -269,7 +274,7 @@ func qpEscape(dest []byte, c byte) { |
| 269 | const nums = "0123456789ABCDEF" | 274 | const nums = "0123456789ABCDEF" |
| 270 | dest[0] = '=' | 275 | dest[0] = '=' |
| 271 | dest[1] = nums[(c&0xf0)>>4] | 276 | dest[1] = nums[(c&0xf0)>>4] |
| 272 | dest[2] = nums[(c & 0xf)] | 277 | dest[2] = nums[(c&0xf)] |
| 273 | } | 278 | } |
| 274 | 279 | ||
| 275 | // headerToBytes enumerates the key and values in the header, and writes the results to the IO Writer | 280 | // headerToBytes enumerates the key and values in the header, and writes the results to the IO Writer | ... | ... |
-
Please register or sign in to post a comment