Attach is used to attach content from an io.Reader to the email. Required parameters include an io.Reader, the desired filename for the attachment, and the Content-Type The function will return the created Attachment for reference, as well as nil for the error, if successful.
(r io.Reader, filename string, c string)
| 254 | // Required parameters include an io.Reader, the desired filename for the attachment, and the Content-Type |
| 255 | // The function will return the created Attachment for reference, as well as nil for the error, if successful. |
| 256 | func (e *Email) Attach(r io.Reader, filename string, c string) (a *Attachment, err error) { |
| 257 | var buffer bytes.Buffer |
| 258 | if _, err = io.Copy(&buffer, r); err != nil { |
| 259 | return |
| 260 | } |
| 261 | at := &Attachment{ |
| 262 | Filename: filename, |
| 263 | Header: textproto.MIMEHeader{}, |
| 264 | Content: buffer.Bytes(), |
| 265 | } |
| 266 | if c != "" { |
| 267 | at.Header.Set("Content-Type", c) |
| 268 | } else { |
| 269 | at.Header.Set("Content-Type", "application/octet-stream") |
| 270 | } |
| 271 | at.Header.Set("Content-Disposition", fmt.Sprintf("attachment;\r\n filename=\"%s\"", filename)) |
| 272 | at.Header.Set("Content-ID", fmt.Sprintf("<%s>", filename)) |
| 273 | at.Header.Set("Content-Transfer-Encoding", "base64") |
| 274 | e.Attachments = append(e.Attachments, at) |
| 275 | return at, nil |
| 276 | } |
| 277 | |
| 278 | // AttachFile is used to attach content to the email. |
| 279 | // It attempts to open the file referenced by filename and, if successful, creates an Attachment. |