Merge pull request #853 from tossp/email
支持发送邮件内嵌附件
Showing
1 changed file
with
27 additions
and
4 deletions
| ... | @@ -157,19 +157,37 @@ func (e *Email) Bytes() ([]byte, error) { | ... | @@ -157,19 +157,37 @@ 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(args ...string) (a *Attachment, err error) { |
| 161 | if len(args) < 1 && len(args) > 2 { | ||
| 162 | err = errors.New("Must specify a file name and number of parameters can not exceed at least two") | ||
| 163 | return | ||
| 164 | } | ||
| 165 | filename := args[0] | ||
| 166 | id := "" | ||
| 167 | if len(args) > 1 { | ||
| 168 | id = args[1] | ||
| 169 | } | ||
| 161 | f, err := os.Open(filename) | 170 | f, err := os.Open(filename) |
| 162 | if err != nil { | 171 | if err != nil { |
| 163 | return | 172 | return |
| 164 | } | 173 | } |
| 165 | ct := mime.TypeByExtension(filepath.Ext(filename)) | 174 | ct := mime.TypeByExtension(filepath.Ext(filename)) |
| 166 | basename := path.Base(filename) | 175 | basename := path.Base(filename) |
| 167 | return e.Attach(f, basename, ct) | 176 | return e.Attach(f, basename, ct, id) |
| 168 | } | 177 | } |
| 169 | 178 | ||
| 170 | // Attach is used to attach content from an io.Reader to the email. | 179 | // 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. | 180 | // 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) { | 181 | func (e *Email) Attach(r io.Reader, filename string, args ...string) (a *Attachment, err error) { |
| 182 | if len(args) < 1 && len(args) > 2 { | ||
| 183 | err = errors.New("Must specify the file type and number of parameters can not exceed at least two") | ||
| 184 | return | ||
| 185 | } | ||
| 186 | c := args[0] //Content-Type | ||
| 187 | id := "" | ||
| 188 | if len(args) > 1 { | ||
| 189 | id = args[1] //Content-ID | ||
| 190 | } | ||
| 173 | var buffer bytes.Buffer | 191 | var buffer bytes.Buffer |
| 174 | if _, err = io.Copy(&buffer, r); err != nil { | 192 | if _, err = io.Copy(&buffer, r); err != nil { |
| 175 | return | 193 | return |
| ... | @@ -186,7 +204,12 @@ func (e *Email) Attach(r io.Reader, filename string, c string) (a *Attachment, e | ... | @@ -186,7 +204,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" | 204 | // If the Content-Type is blank, set the Content-Type to "application/octet-stream" |
| 187 | at.Header.Set("Content-Type", "application/octet-stream") | 205 | at.Header.Set("Content-Type", "application/octet-stream") |
| 188 | } | 206 | } |
| 207 | if id != "" { | ||
| 208 | at.Header.Set("Content-Disposition", fmt.Sprintf("inline;\r\n filename=\"%s\"", filename)) | ||
| 209 | at.Header.Set("Content-ID", fmt.Sprintf("<%s>", id)) | ||
| 210 | } else { | ||
| 189 | at.Header.Set("Content-Disposition", fmt.Sprintf("attachment;\r\n filename=\"%s\"", filename)) | 211 | at.Header.Set("Content-Disposition", fmt.Sprintf("attachment;\r\n filename=\"%s\"", filename)) |
| 212 | } | ||
| 190 | at.Header.Set("Content-Transfer-Encoding", "base64") | 213 | at.Header.Set("Content-Transfer-Encoding", "base64") |
| 191 | e.Attachments = append(e.Attachments, at) | 214 | e.Attachments = append(e.Attachments, at) |
| 192 | return at, nil | 215 | return at, nil |
| ... | @@ -269,7 +292,7 @@ func qpEscape(dest []byte, c byte) { | ... | @@ -269,7 +292,7 @@ func qpEscape(dest []byte, c byte) { |
| 269 | const nums = "0123456789ABCDEF" | 292 | const nums = "0123456789ABCDEF" |
| 270 | dest[0] = '=' | 293 | dest[0] = '=' |
| 271 | dest[1] = nums[(c&0xf0)>>4] | 294 | dest[1] = nums[(c&0xf0)>>4] |
| 272 | dest[2] = nums[(c & 0xf)] | 295 | dest[2] = nums[(c&0xf)] |
| 273 | } | 296 | } |
| 274 | 297 | ||
| 275 | // headerToBytes enumerates the key and values in the header, and writes the results to the IO Writer | 298 | // 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