2014-10-15 23:47:07 +08:00
|
|
|
package gomail
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
2015-07-03 05:08:18 +08:00
|
|
|
"errors"
|
2014-10-15 23:47:07 +08:00
|
|
|
"io"
|
2015-07-09 01:53:11 +08:00
|
|
|
"mime"
|
2014-10-15 23:47:07 +08:00
|
|
|
"mime/multipart"
|
2015-07-09 01:53:11 +08:00
|
|
|
"path/filepath"
|
2014-10-15 23:47:07 +08:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2015-07-03 05:15:42 +08:00
|
|
|
// WriteTo implements io.WriterTo. It dumps the whole message into w.
|
2015-07-15 05:34:21 +08:00
|
|
|
func (m *Message) WriteTo(w io.Writer) (int64, error) {
|
2015-07-03 05:08:18 +08:00
|
|
|
mw := &messageWriter{w: w}
|
2015-07-15 05:34:21 +08:00
|
|
|
mw.writeMessage(m)
|
2015-07-03 05:08:18 +08:00
|
|
|
return mw.n, mw.err
|
|
|
|
}
|
|
|
|
|
2015-07-15 05:34:21 +08:00
|
|
|
func (w *messageWriter) writeMessage(m *Message) {
|
|
|
|
if _, ok := m.header["Mime-Version"]; !ok {
|
2015-07-03 05:08:18 +08:00
|
|
|
w.writeString("Mime-Version: 1.0\r\n")
|
|
|
|
}
|
2015-07-15 05:34:21 +08:00
|
|
|
if _, ok := m.header["Date"]; !ok {
|
|
|
|
w.writeHeader("Date", m.FormatDate(now()))
|
2015-07-03 05:08:18 +08:00
|
|
|
}
|
2015-07-15 05:34:21 +08:00
|
|
|
w.writeHeaders(m.header)
|
2014-10-15 23:47:07 +08:00
|
|
|
|
2015-07-15 05:34:21 +08:00
|
|
|
if m.hasMixedPart() {
|
2014-10-15 23:47:07 +08:00
|
|
|
w.openMultipart("mixed")
|
|
|
|
}
|
|
|
|
|
2015-07-15 05:34:21 +08:00
|
|
|
if m.hasRelatedPart() {
|
2014-10-15 23:47:07 +08:00
|
|
|
w.openMultipart("related")
|
|
|
|
}
|
|
|
|
|
2015-07-15 05:34:21 +08:00
|
|
|
if m.hasAlternativePart() {
|
2014-10-15 23:47:07 +08:00
|
|
|
w.openMultipart("alternative")
|
|
|
|
}
|
2015-07-15 05:34:21 +08:00
|
|
|
for _, part := range m.parts {
|
2015-12-12 23:40:02 +08:00
|
|
|
w.writePart(part, m.charset)
|
2014-10-15 23:47:07 +08:00
|
|
|
}
|
2015-07-15 05:34:21 +08:00
|
|
|
if m.hasAlternativePart() {
|
2014-10-15 23:47:07 +08:00
|
|
|
w.closeMultipart()
|
|
|
|
}
|
|
|
|
|
2015-07-15 05:34:21 +08:00
|
|
|
w.addFiles(m.embedded, false)
|
|
|
|
if m.hasRelatedPart() {
|
2014-10-15 23:47:07 +08:00
|
|
|
w.closeMultipart()
|
|
|
|
}
|
|
|
|
|
2015-07-15 05:34:21 +08:00
|
|
|
w.addFiles(m.attachments, true)
|
|
|
|
if m.hasMixedPart() {
|
2014-10-15 23:47:07 +08:00
|
|
|
w.closeMultipart()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-15 05:34:21 +08:00
|
|
|
func (m *Message) hasMixedPart() bool {
|
|
|
|
return (len(m.parts) > 0 && len(m.attachments) > 0) || len(m.attachments) > 1
|
2014-10-15 23:47:07 +08:00
|
|
|
}
|
|
|
|
|
2015-07-15 05:34:21 +08:00
|
|
|
func (m *Message) hasRelatedPart() bool {
|
|
|
|
return (len(m.parts) > 0 && len(m.embedded) > 0) || len(m.embedded) > 1
|
2014-10-15 23:47:07 +08:00
|
|
|
}
|
|
|
|
|
2015-07-15 05:34:21 +08:00
|
|
|
func (m *Message) hasAlternativePart() bool {
|
|
|
|
return len(m.parts) > 1
|
2014-10-15 23:47:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type messageWriter struct {
|
2015-07-03 05:08:18 +08:00
|
|
|
w io.Writer
|
|
|
|
n int64
|
2014-10-15 23:47:07 +08:00
|
|
|
writers [3]*multipart.Writer
|
|
|
|
partWriter io.Writer
|
|
|
|
depth uint8
|
2015-07-03 05:08:18 +08:00
|
|
|
err error
|
2014-10-15 23:47:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *messageWriter) openMultipart(mimeType string) {
|
2015-07-03 05:08:18 +08:00
|
|
|
mw := multipart.NewWriter(w)
|
|
|
|
contentType := "multipart/" + mimeType + "; boundary=" + mw.Boundary()
|
|
|
|
w.writers[w.depth] = mw
|
2014-10-15 23:47:07 +08:00
|
|
|
|
|
|
|
if w.depth == 0 {
|
2015-07-03 05:08:18 +08:00
|
|
|
w.writeHeader("Content-Type", contentType)
|
|
|
|
w.writeString("\r\n")
|
2014-10-15 23:47:07 +08:00
|
|
|
} else {
|
2015-07-03 05:08:18 +08:00
|
|
|
w.createPart(map[string][]string{
|
2015-07-23 04:38:02 +08:00
|
|
|
"Content-Type": {contentType},
|
2015-07-03 05:08:18 +08:00
|
|
|
})
|
2014-10-15 23:47:07 +08:00
|
|
|
}
|
|
|
|
w.depth++
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *messageWriter) createPart(h map[string][]string) {
|
2015-07-03 05:08:18 +08:00
|
|
|
w.partWriter, w.err = w.writers[w.depth-1].CreatePart(h)
|
2014-10-15 23:47:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *messageWriter) closeMultipart() {
|
|
|
|
if w.depth > 0 {
|
|
|
|
w.writers[w.depth-1].Close()
|
|
|
|
w.depth--
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-12 23:40:02 +08:00
|
|
|
func (w *messageWriter) writePart(p *part, charset string) {
|
|
|
|
w.writeHeaders(map[string][]string{
|
|
|
|
"Content-Type": {p.contentType + "; charset=" + charset},
|
|
|
|
"Content-Transfer-Encoding": {string(p.encoding)},
|
|
|
|
})
|
|
|
|
w.writeBody(p.copier, p.encoding)
|
|
|
|
}
|
|
|
|
|
2015-08-04 04:57:38 +08:00
|
|
|
func (w *messageWriter) addFiles(files []*file, isAttachment bool) {
|
2014-10-15 23:47:07 +08:00
|
|
|
for _, f := range files {
|
2015-07-09 01:53:11 +08:00
|
|
|
if _, ok := f.Header["Content-Type"]; !ok {
|
|
|
|
mediaType := mime.TypeByExtension(filepath.Ext(f.Name))
|
|
|
|
if mediaType == "" {
|
|
|
|
mediaType = "application/octet-stream"
|
|
|
|
}
|
|
|
|
f.setHeader("Content-Type", mediaType+`; name="`+f.Name+`"`)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := f.Header["Content-Transfer-Encoding"]; !ok {
|
|
|
|
f.setHeader("Content-Transfer-Encoding", string(Base64))
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := f.Header["Content-Disposition"]; !ok {
|
|
|
|
var disp string
|
|
|
|
if isAttachment {
|
|
|
|
disp = "attachment"
|
2015-02-19 03:46:05 +08:00
|
|
|
} else {
|
2015-07-09 01:53:11 +08:00
|
|
|
disp = "inline"
|
|
|
|
}
|
|
|
|
f.setHeader("Content-Disposition", disp+`; filename="`+f.Name+`"`)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !isAttachment {
|
|
|
|
if _, ok := f.Header["Content-ID"]; !ok {
|
|
|
|
f.setHeader("Content-ID", "<"+f.Name+">")
|
2015-02-19 03:46:05 +08:00
|
|
|
}
|
2014-10-15 23:47:07 +08:00
|
|
|
}
|
2015-07-09 01:53:11 +08:00
|
|
|
w.writeHeaders(f.Header)
|
2015-08-04 04:57:38 +08:00
|
|
|
w.writeBody(f.CopyFunc, Base64)
|
2015-07-03 05:08:18 +08:00
|
|
|
}
|
|
|
|
}
|
2014-10-15 23:47:07 +08:00
|
|
|
|
2015-07-03 05:08:18 +08:00
|
|
|
func (w *messageWriter) Write(p []byte) (int, error) {
|
|
|
|
if w.err != nil {
|
|
|
|
return 0, errors.New("gomail: cannot write as writer is in error")
|
2014-10-15 23:47:07 +08:00
|
|
|
}
|
2015-07-03 05:08:18 +08:00
|
|
|
|
|
|
|
var n int
|
|
|
|
n, w.err = w.w.Write(p)
|
|
|
|
w.n += int64(n)
|
|
|
|
return n, w.err
|
2014-10-15 23:47:07 +08:00
|
|
|
}
|
|
|
|
|
2015-07-03 05:08:18 +08:00
|
|
|
func (w *messageWriter) writeString(s string) {
|
|
|
|
n, _ := io.WriteString(w.w, s)
|
|
|
|
w.n += int64(n)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *messageWriter) writeHeader(k string, v ...string) {
|
|
|
|
w.writeString(k)
|
|
|
|
w.writeString(": ")
|
2015-10-22 16:58:06 +08:00
|
|
|
if len(v) > 0 {
|
|
|
|
w.writeString(v[0])
|
|
|
|
for _, s := range v[1:] {
|
|
|
|
w.writeString(", ")
|
|
|
|
w.writeString(s)
|
|
|
|
}
|
|
|
|
}
|
2015-07-03 05:08:18 +08:00
|
|
|
w.writeString("\r\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *messageWriter) writeHeaders(h map[string][]string) {
|
2014-10-15 23:47:07 +08:00
|
|
|
if w.depth == 0 {
|
2015-07-03 05:08:18 +08:00
|
|
|
for k, v := range h {
|
|
|
|
if k != "Bcc" {
|
|
|
|
w.writeHeader(k, v...)
|
|
|
|
}
|
2014-10-15 23:47:07 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
w.createPart(h)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-08 06:26:19 +08:00
|
|
|
func (w *messageWriter) writeBody(f func(io.Writer) error, enc Encoding) {
|
2014-10-15 23:47:07 +08:00
|
|
|
var subWriter io.Writer
|
|
|
|
if w.depth == 0 {
|
2015-07-03 05:08:18 +08:00
|
|
|
w.writeString("\r\n")
|
|
|
|
subWriter = w.w
|
2014-10-15 23:47:07 +08:00
|
|
|
} else {
|
|
|
|
subWriter = w.partWriter
|
|
|
|
}
|
|
|
|
|
|
|
|
if enc == Base64 {
|
2015-07-03 05:08:18 +08:00
|
|
|
wc := base64.NewEncoder(base64.StdEncoding, newBase64LineWriter(subWriter))
|
2015-07-08 06:26:19 +08:00
|
|
|
w.err = f(wc)
|
2015-07-03 05:08:18 +08:00
|
|
|
wc.Close()
|
2014-10-28 15:35:50 +08:00
|
|
|
} else if enc == Unencoded {
|
2015-07-08 06:26:19 +08:00
|
|
|
w.err = f(subWriter)
|
2014-10-15 23:47:07 +08:00
|
|
|
} else {
|
2015-07-17 01:33:38 +08:00
|
|
|
wc := newQPWriter(subWriter)
|
2015-07-08 06:26:19 +08:00
|
|
|
w.err = f(wc)
|
2015-07-03 05:08:18 +08:00
|
|
|
wc.Close()
|
2014-10-15 23:47:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-20 20:15:52 +08:00
|
|
|
// As required by RFC 2045, 6.7. (page 21) for quoted-printable, and
|
|
|
|
// RFC 2045, 6.8. (page 25) for base64.
|
|
|
|
const maxLineLen = 76
|
2014-10-15 23:47:07 +08:00
|
|
|
|
2015-01-20 20:15:52 +08:00
|
|
|
// base64LineWriter limits text encoded in base64 to 76 characters per line
|
2014-10-15 23:47:07 +08:00
|
|
|
type base64LineWriter struct {
|
|
|
|
w io.Writer
|
|
|
|
lineLen int
|
|
|
|
}
|
|
|
|
|
|
|
|
func newBase64LineWriter(w io.Writer) *base64LineWriter {
|
|
|
|
return &base64LineWriter{w: w}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *base64LineWriter) Write(p []byte) (int, error) {
|
|
|
|
n := 0
|
|
|
|
for len(p)+w.lineLen > maxLineLen {
|
|
|
|
w.w.Write(p[:maxLineLen-w.lineLen])
|
|
|
|
w.w.Write([]byte("\r\n"))
|
|
|
|
p = p[maxLineLen-w.lineLen:]
|
|
|
|
n += maxLineLen - w.lineLen
|
|
|
|
w.lineLen = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
w.w.Write(p)
|
|
|
|
w.lineLen += len(p)
|
|
|
|
|
|
|
|
return n + len(p), nil
|
|
|
|
}
|
2015-07-03 05:08:18 +08:00
|
|
|
|
|
|
|
// Stubbed out for testing.
|
|
|
|
var now = time.Now
|