diff --git a/message.go b/message.go index 58da3f3..20c3b9b 100644 --- a/message.go +++ b/message.go @@ -34,52 +34,52 @@ type part struct { // NewMessage creates a new message. It uses UTF-8 and quoted-printable encoding // by default. func NewMessage(settings ...MessageSetting) *Message { - msg := &Message{ + m := &Message{ header: make(header), charset: "UTF-8", encoding: QuotedPrintable, } - msg.applySettings(settings) + m.applySettings(settings) - if msg.encoding == Base64 { - msg.hEncoder = mime.BEncoding + if m.encoding == Base64 { + m.hEncoder = mime.BEncoding } else { - msg.hEncoder = mime.QEncoding + m.hEncoder = mime.QEncoding } - return msg + return m } // Reset resets the message so it can be reused. The message keeps its previous // settings so it is in the same state that after a call to NewMessage. -func (msg *Message) Reset() { - for k := range msg.header { - delete(msg.header, k) +func (m *Message) Reset() { + for k := range m.header { + delete(m.header, k) } - msg.parts = nil - msg.attachments = nil - msg.embedded = nil + m.parts = nil + m.attachments = nil + m.embedded = nil } -func (msg *Message) applySettings(settings []MessageSetting) { +func (m *Message) applySettings(settings []MessageSetting) { for _, s := range settings { - s(msg) + s(m) } } // A MessageSetting can be used as an argument in NewMessage to configure an // email. -type MessageSetting func(msg *Message) +type MessageSetting func(m *Message) // SetCharset is a message setting to set the charset of the email. // // Example: // -// msg := gomail.NewMessage(SetCharset("ISO-8859-1")) +// m := gomail.NewMessage(SetCharset("ISO-8859-1")) func SetCharset(charset string) MessageSetting { - return func(msg *Message) { - msg.charset = charset + return func(m *Message) { + m.charset = charset } } @@ -87,10 +87,10 @@ func SetCharset(charset string) MessageSetting { // // Example: // -// msg := gomail.NewMessage(SetEncoding(gomail.Base64)) +// m := gomail.NewMessage(SetEncoding(gomail.Base64)) func SetEncoding(enc Encoding) MessageSetting { - return func(msg *Message) { - msg.encoding = enc + return func(m *Message) { + m.encoding = enc } } @@ -109,57 +109,57 @@ const ( ) // SetHeader sets a value to the given header field. -func (msg *Message) SetHeader(field string, value ...string) { +func (m *Message) SetHeader(field string, value ...string) { for i := range value { - value[i] = msg.encodeHeader(value[i]) + value[i] = m.encodeHeader(value[i]) } - msg.header[field] = value + m.header[field] = value } // SetHeaders sets the message headers. // // Example: // -// msg.SetHeaders(map[string][]string{ +// m.SetHeaders(map[string][]string{ // "From": {"alex@example.com"}, // "To": {"bob@example.com", "cora@example.com"}, // "Subject": {"Hello"}, // }) -func (msg *Message) SetHeaders(h map[string][]string) { +func (m *Message) SetHeaders(h map[string][]string) { for k, v := range h { - msg.SetHeader(k, v...) + m.SetHeader(k, v...) } } // SetAddressHeader sets an address to the given header field. -func (msg *Message) SetAddressHeader(field, address, name string) { - msg.header[field] = []string{msg.FormatAddress(address, name)} +func (m *Message) SetAddressHeader(field, address, name string) { + m.header[field] = []string{m.FormatAddress(address, name)} } // FormatAddress formats an address and a name as a valid RFC 5322 address. -func (msg *Message) FormatAddress(address, name string) string { - enc := msg.encodeHeader(name) +func (m *Message) FormatAddress(address, name string) string { + enc := m.encodeHeader(name) if enc == name { - msg.buf.WriteByte('"') + m.buf.WriteByte('"') for i := 0; i < len(name); i++ { b := name[i] if b == '\\' || b == '"' { - msg.buf.WriteByte('\\') + m.buf.WriteByte('\\') } - msg.buf.WriteByte(b) + m.buf.WriteByte(b) } - msg.buf.WriteByte('"') + m.buf.WriteByte('"') } else if hasSpecials(name) { - msg.buf.WriteString(mime.BEncoding.Encode(msg.charset, name)) + m.buf.WriteString(mime.BEncoding.Encode(m.charset, name)) } else { - msg.buf.WriteString(enc) + m.buf.WriteString(enc) } - msg.buf.WriteString(" <") - msg.buf.WriteString(address) - msg.buf.WriteByte('>') + m.buf.WriteString(" <") + m.buf.WriteString(address) + m.buf.WriteByte('>') - addr := msg.buf.String() - msg.buf.Reset() + addr := m.buf.String() + m.buf.Reset() return addr } @@ -174,33 +174,33 @@ func hasSpecials(text string) bool { return false } -func (msg *Message) encodeHeader(value string) string { - return msg.hEncoder.Encode(msg.charset, value) +func (m *Message) encodeHeader(value string) string { + return m.hEncoder.Encode(m.charset, value) } // SetDateHeader sets a date to the given header field. -func (msg *Message) SetDateHeader(field string, date time.Time) { - msg.header[field] = []string{msg.FormatDate(date)} +func (m *Message) SetDateHeader(field string, date time.Time) { + m.header[field] = []string{m.FormatDate(date)} } // FormatDate formats a date as a valid RFC 5322 date. -func (msg *Message) FormatDate(date time.Time) string { +func (m *Message) FormatDate(date time.Time) string { return date.Format(time.RFC1123Z) } // GetHeader gets a header field. -func (msg *Message) GetHeader(field string) []string { - return msg.header[field] +func (m *Message) GetHeader(field string) []string { + return m.header[field] } // DelHeader deletes a header field. -func (msg *Message) DelHeader(field string) { - delete(msg.header, field) +func (m *Message) DelHeader(field string) { + delete(m.header, field) } // SetBody sets the body of the message. -func (msg *Message) SetBody(contentType, body string) { - msg.parts = []part{ +func (m *Message) SetBody(contentType, body string) { + m.parts = []part{ part{ contentType: contentType, copier: func(w io.Writer) error { @@ -217,12 +217,12 @@ func (msg *Message) SetBody(contentType, body string) { // // Example: // -// msg.SetBody("text/plain", "Hello!") -// msg.AddAlternative("text/html", "

Hello!

") +// m.SetBody("text/plain", "Hello!") +// m.AddAlternative("text/html", "

Hello!

") // // More info: http://en.wikipedia.org/wiki/MIME#Alternative -func (msg *Message) AddAlternative(contentType, body string) { - msg.parts = append(msg.parts, +func (m *Message) AddAlternative(contentType, body string) { + m.parts = append(m.parts, part{ contentType: contentType, copier: func(w io.Writer) error { @@ -239,11 +239,11 @@ func (msg *Message) AddAlternative(contentType, body string) { // Example: // // t := template.Must(template.New("example").Parse("Hello {{.}}!")) -// msg.AddAlternativeWriter("text/plain", func(w io.Writer) error { +// m.AddAlternativeWriter("text/plain", func(w io.Writer) error { // return t.Execute(w, "Bob") // }) -func (msg *Message) AddAlternativeWriter(contentType string, f func(io.Writer) error) { - msg.parts = []part{ +func (m *Message) AddAlternativeWriter(contentType string, f func(io.Writer) error) { + m.parts = []part{ part{ contentType: contentType, copier: f, @@ -288,11 +288,11 @@ func (f *File) setHeader(field string, value ...string) { } // Attach attaches the files to the email. -func (msg *Message) Attach(f ...*File) { - if msg.attachments == nil { - msg.attachments = f +func (m *Message) Attach(f ...*File) { + if m.attachments == nil { + m.attachments = f } else { - msg.attachments = append(msg.attachments, f...) + m.attachments = append(m.attachments, f...) } } @@ -304,12 +304,12 @@ func (msg *Message) Attach(f ...*File) { // if err != nil { // panic(err) // } -// msg.Embed(f) -// msg.SetBody("text/html", `My image`) -func (msg *Message) Embed(image ...*File) { - if msg.embedded == nil { - msg.embedded = image +// m.Embed(f) +// m.SetBody("text/html", `My image`) +func (m *Message) Embed(image ...*File) { + if m.embedded == nil { + m.embedded = image } else { - msg.embedded = append(msg.embedded, image...) + m.embedded = append(m.embedded, image...) } } diff --git a/message_test.go b/message_test.go index 760f4d3..4787db8 100644 --- a/message_test.go +++ b/message_test.go @@ -25,18 +25,18 @@ type message struct { } func TestMessage(t *testing.T) { - msg := NewMessage() - msg.SetAddressHeader("From", "from@example.com", "Señor From") - msg.SetHeader("To", msg.FormatAddress("to@example.com", "Señor To"), "tobis@example.com") - msg.SetAddressHeader("Cc", "cc@example.com", "A, B") - msg.SetAddressHeader("X-To", "ccbis@example.com", "à, b") - msg.SetDateHeader("X-Date", now()) - msg.SetHeader("X-Date-2", msg.FormatDate(now())) - msg.SetHeader("Subject", "¡Hola, señor!") - msg.SetHeaders(map[string][]string{ + m := NewMessage() + m.SetAddressHeader("From", "from@example.com", "Señor From") + m.SetHeader("To", m.FormatAddress("to@example.com", "Señor To"), "tobis@example.com") + m.SetAddressHeader("Cc", "cc@example.com", "A, B") + m.SetAddressHeader("X-To", "ccbis@example.com", "à, b") + m.SetDateHeader("X-Date", now()) + m.SetHeader("X-Date-2", m.FormatDate(now())) + m.SetHeader("Subject", "¡Hola, señor!") + m.SetHeaders(map[string][]string{ "X-Headers": {"Test", "Café"}, }) - msg.SetBody("text/plain", "¡Hola, señor!") + m.SetBody("text/plain", "¡Hola, señor!") want := &message{ from: "from@example.com", @@ -59,14 +59,14 @@ func TestMessage(t *testing.T) { "=C2=A1Hola, se=C3=B1or!", } - testMessage(t, msg, 0, want) + testMessage(t, m, 0, want) } func TestBodyWriter(t *testing.T) { - msg := NewMessage() - msg.SetHeader("From", "from@example.com") - msg.SetHeader("To", "to@example.com") - msg.AddAlternativeWriter("text/plain", func(w io.Writer) error { + m := NewMessage() + m.SetHeader("From", "from@example.com") + m.SetHeader("To", "to@example.com") + m.AddAlternativeWriter("text/plain", func(w io.Writer) error { _, err := w.Write([]byte("Test message")) return err }) @@ -82,17 +82,17 @@ func TestBodyWriter(t *testing.T) { "Test message", } - testMessage(t, msg, 0, want) + testMessage(t, m, 0, want) } func TestCustomMessage(t *testing.T) { - msg := NewMessage(SetCharset("ISO-8859-1"), SetEncoding(Base64)) - msg.SetHeaders(map[string][]string{ + m := NewMessage(SetCharset("ISO-8859-1"), SetEncoding(Base64)) + m.SetHeaders(map[string][]string{ "From": {"from@example.com"}, "To": {"to@example.com"}, "Subject": {"Café"}, }) - msg.SetBody("text/html", "¡Hola, señor!") + m.SetBody("text/html", "¡Hola, señor!") want := &message{ from: "from@example.com", @@ -106,17 +106,17 @@ func TestCustomMessage(t *testing.T) { "wqFIb2xhLCBzZcOxb3Ih", } - testMessage(t, msg, 0, want) + testMessage(t, m, 0, want) } func TestUnencodedMessage(t *testing.T) { - msg := NewMessage(SetEncoding(Unencoded)) - msg.SetHeaders(map[string][]string{ + m := NewMessage(SetEncoding(Unencoded)) + m.SetHeaders(map[string][]string{ "From": {"from@example.com"}, "To": {"to@example.com"}, "Subject": {"Café"}, }) - msg.SetBody("text/html", "¡Hola, señor!") + m.SetBody("text/html", "¡Hola, señor!") want := &message{ from: "from@example.com", @@ -130,19 +130,19 @@ func TestUnencodedMessage(t *testing.T) { "¡Hola, señor!", } - testMessage(t, msg, 0, want) + testMessage(t, m, 0, want) } func TestRecipients(t *testing.T) { - msg := NewMessage() - msg.SetHeaders(map[string][]string{ + m := NewMessage() + m.SetHeaders(map[string][]string{ "From": {"from@example.com"}, "To": {"to@example.com"}, "Cc": {"cc@example.com"}, "Bcc": {"bcc1@example.com", "bcc2@example.com"}, "Subject": {"Hello!"}, }) - msg.SetBody("text/plain", "Test message") + m.SetBody("text/plain", "Test message") want := &message{ from: "from@example.com", @@ -157,15 +157,15 @@ func TestRecipients(t *testing.T) { "Test message", } - testMessage(t, msg, 0, want) + testMessage(t, m, 0, want) } func TestAlternative(t *testing.T) { - msg := NewMessage() - msg.SetHeader("From", "from@example.com") - msg.SetHeader("To", "to@example.com") - msg.SetBody("text/plain", "¡Hola, señor!") - msg.AddAlternative("text/html", "¡Hola, señor!") + m := NewMessage() + m.SetHeader("From", "from@example.com") + m.SetHeader("To", "to@example.com") + m.SetBody("text/plain", "¡Hola, señor!") + m.AddAlternative("text/html", "¡Hola, señor!") want := &message{ from: "from@example.com", @@ -187,14 +187,14 @@ func TestAlternative(t *testing.T) { "--_BOUNDARY_1_--\r\n", } - testMessage(t, msg, 1, want) + testMessage(t, m, 1, want) } func TestAttachmentOnly(t *testing.T) { - msg := NewMessage() - msg.SetHeader("From", "from@example.com") - msg.SetHeader("To", "to@example.com") - msg.Attach(testFile("/tmp/test.pdf")) + m := NewMessage() + m.SetHeader("From", "from@example.com") + m.SetHeader("To", "to@example.com") + m.Attach(testFile("/tmp/test.pdf")) want := &message{ from: "from@example.com", @@ -208,15 +208,15 @@ func TestAttachmentOnly(t *testing.T) { base64.StdEncoding.EncodeToString([]byte("Content of test.pdf")), } - testMessage(t, msg, 0, want) + testMessage(t, m, 0, want) } func TestAttachment(t *testing.T) { - msg := NewMessage() - msg.SetHeader("From", "from@example.com") - msg.SetHeader("To", "to@example.com") - msg.SetBody("text/plain", "Test") - msg.Attach(testFile("/tmp/test.pdf")) + m := NewMessage() + m.SetHeader("From", "from@example.com") + m.SetHeader("To", "to@example.com") + m.SetBody("text/plain", "Test") + m.Attach(testFile("/tmp/test.pdf")) want := &message{ from: "from@example.com", @@ -239,15 +239,15 @@ func TestAttachment(t *testing.T) { "--_BOUNDARY_1_--\r\n", } - testMessage(t, msg, 1, want) + testMessage(t, m, 1, want) } func TestAttachmentsOnly(t *testing.T) { - msg := NewMessage() - msg.SetHeader("From", "from@example.com") - msg.SetHeader("To", "to@example.com") - msg.Attach(testFile("/tmp/test.pdf")) - msg.Attach(testFile("/tmp/test.zip")) + m := NewMessage() + m.SetHeader("From", "from@example.com") + m.SetHeader("To", "to@example.com") + m.Attach(testFile("/tmp/test.pdf")) + m.Attach(testFile("/tmp/test.zip")) want := &message{ from: "from@example.com", @@ -271,16 +271,16 @@ func TestAttachmentsOnly(t *testing.T) { "--_BOUNDARY_1_--\r\n", } - testMessage(t, msg, 1, want) + testMessage(t, m, 1, want) } func TestAttachments(t *testing.T) { - msg := NewMessage() - msg.SetHeader("From", "from@example.com") - msg.SetHeader("To", "to@example.com") - msg.SetBody("text/plain", "Test") - msg.Attach(testFile("/tmp/test.pdf")) - msg.Attach(testFile("/tmp/test.zip")) + m := NewMessage() + m.SetHeader("From", "from@example.com") + m.SetHeader("To", "to@example.com") + m.SetBody("text/plain", "Test") + m.Attach(testFile("/tmp/test.pdf")) + m.Attach(testFile("/tmp/test.zip")) want := &message{ from: "from@example.com", @@ -309,18 +309,18 @@ func TestAttachments(t *testing.T) { "--_BOUNDARY_1_--\r\n", } - testMessage(t, msg, 1, want) + testMessage(t, m, 1, want) } func TestEmbedded(t *testing.T) { - msg := NewMessage() - msg.SetHeader("From", "from@example.com") - msg.SetHeader("To", "to@example.com") + m := NewMessage() + m.SetHeader("From", "from@example.com") + m.SetHeader("To", "to@example.com") f := testFile("image1.jpg") f.Header["Content-ID"] = []string{""} - msg.Embed(f) - msg.Embed(testFile("image2.jpg")) - msg.SetBody("text/plain", "Test") + m.Embed(f) + m.Embed(testFile("image2.jpg")) + m.SetBody("text/plain", "Test") want := &message{ from: "from@example.com", @@ -351,17 +351,17 @@ func TestEmbedded(t *testing.T) { "--_BOUNDARY_1_--\r\n", } - testMessage(t, msg, 1, want) + testMessage(t, m, 1, want) } func TestFullMessage(t *testing.T) { - msg := NewMessage() - msg.SetHeader("From", "from@example.com") - msg.SetHeader("To", "to@example.com") - msg.SetBody("text/plain", "¡Hola, señor!") - msg.AddAlternative("text/html", "¡Hola, señor!") - msg.Attach(testFile("test.pdf")) - msg.Embed(testFile("image.jpg")) + m := NewMessage() + m.SetHeader("From", "from@example.com") + m.SetHeader("To", "to@example.com") + m.SetBody("text/plain", "¡Hola, señor!") + m.AddAlternative("text/html", "¡Hola, señor!") + m.Attach(testFile("test.pdf")) + m.Embed(testFile("image.jpg")) want := &message{ from: "from@example.com", @@ -406,7 +406,7 @@ func TestFullMessage(t *testing.T) { "--_BOUNDARY_1_--\r\n", } - testMessage(t, msg, 3, want) + testMessage(t, m, 3, want) want = &message{ from: "from@example.com", @@ -418,18 +418,18 @@ func TestFullMessage(t *testing.T) { "\r\n" + "Test reset", } - msg.Reset() - msg.SetHeader("From", "from@example.com") - msg.SetHeader("To", "to@example.com") - msg.SetBody("text/plain", "Test reset") - testMessage(t, msg, 0, want) + m.Reset() + m.SetHeader("From", "from@example.com") + m.SetHeader("To", "to@example.com") + m.SetBody("text/plain", "Test reset") + testMessage(t, m, 0, want) } func TestQpLineLength(t *testing.T) { - msg := NewMessage() - msg.SetHeader("From", "from@example.com") - msg.SetHeader("To", "to@example.com") - msg.SetBody("text/plain", + m := NewMessage() + m.SetHeader("From", "from@example.com") + m.SetHeader("To", "to@example.com") + m.SetBody("text/plain", strings.Repeat("0", 76)+"\r\n"+ strings.Repeat("0", 75)+"à\r\n"+ strings.Repeat("0", 74)+"à\r\n"+ @@ -455,14 +455,14 @@ func TestQpLineLength(t *testing.T) { strings.Repeat("0", 75) + "=\r\n0\r\n", } - testMessage(t, msg, 0, want) + testMessage(t, m, 0, want) } func TestBase64LineLength(t *testing.T) { - msg := NewMessage(SetCharset("UTF-8"), SetEncoding(Base64)) - msg.SetHeader("From", "from@example.com") - msg.SetHeader("To", "to@example.com") - msg.SetBody("text/plain", strings.Repeat("0", 58)) + m := NewMessage(SetCharset("UTF-8"), SetEncoding(Base64)) + m.SetHeader("From", "from@example.com") + m.SetHeader("To", "to@example.com") + m.SetBody("text/plain", strings.Repeat("0", 58)) want := &message{ from: "from@example.com", @@ -475,18 +475,18 @@ func TestBase64LineLength(t *testing.T) { strings.Repeat("MDAw", 19) + "\r\nMA==", } - testMessage(t, msg, 0, want) + testMessage(t, m, 0, want) } -func testMessage(t *testing.T, msg *Message, bCount int, want *message) { - err := Send(stubSendMail(t, bCount, want), msg) +func testMessage(t *testing.T, m *Message, bCount int, want *message) { + err := Send(stubSendMail(t, bCount, want), m) if err != nil { t.Error(err) } } func stubSendMail(t *testing.T, bCount int, want *message) SendFunc { - return func(from string, to []string, msg io.WriterTo) error { + return func(from string, to []string, m io.WriterTo) error { if from != want.from { t.Fatalf("Invalid from, got %q, want %q", from, want.from) } @@ -506,7 +506,7 @@ func stubSendMail(t *testing.T, bCount int, want *message) SendFunc { } buf := new(bytes.Buffer) - _, err := msg.WriteTo(buf) + _, err := m.WriteTo(buf) if err != nil { t.Error(err) } @@ -575,8 +575,8 @@ func missingLine(t *testing.T, line, got, want string) { t.Fatalf("Missing line %q\ngot:\n%s\nwant:\n%s", line, got, want) } -func getBoundaries(t *testing.T, count int, msg string) []string { - if matches := boundaryRegExp.FindAllStringSubmatch(msg, count); matches != nil { +func getBoundaries(t *testing.T, count int, m string) []string { + if matches := boundaryRegExp.FindAllStringSubmatch(m, count); matches != nil { boundaries := make([]string, count) for i, match := range matches { boundaries[i] = match[1] @@ -601,30 +601,30 @@ func testFile(name string) *File { func BenchmarkFull(b *testing.B) { buf := new(bytes.Buffer) - emptyFunc := func(from string, to []string, msg io.WriterTo) error { - msg.WriteTo(buf) + emptyFunc := func(from string, to []string, m io.WriterTo) error { + m.WriteTo(buf) buf.Reset() return nil } - msg := NewMessage() + m := NewMessage() b.ResetTimer() for n := 0; n < b.N; n++ { - msg.SetAddressHeader("From", "from@example.com", "Señor From") - msg.SetHeaders(map[string][]string{ + m.SetAddressHeader("From", "from@example.com", "Señor From") + m.SetHeaders(map[string][]string{ "To": {"to@example.com"}, "Cc": {"cc@example.com"}, "Bcc": {"bcc1@example.com", "bcc2@example.com"}, "Subject": {"¡Hola, señor!"}, }) - msg.SetBody("text/plain", "¡Hola, señor!") - msg.AddAlternative("text/html", "

¡Hola, señor!

") - msg.Attach(testFile("benchmark.txt")) - msg.Embed(testFile("benchmark.jpg")) + m.SetBody("text/plain", "¡Hola, señor!") + m.AddAlternative("text/html", "

¡Hola, señor!

") + m.Attach(testFile("benchmark.txt")) + m.Embed(testFile("benchmark.jpg")) - if err := Send(SendFunc(emptyFunc), msg); err != nil { + if err := Send(SendFunc(emptyFunc), m); err != nil { panic(err) } - msg.Reset() + m.Reset() } } diff --git a/send.go b/send.go index 22f3513..82c96d6 100644 --- a/send.go +++ b/send.go @@ -42,28 +42,28 @@ func Send(s Sender, msg ...*Message) error { return nil } -func send(s Sender, msg *Message) error { - from, err := msg.getFrom() +func send(s Sender, m *Message) error { + from, err := m.getFrom() if err != nil { return err } - to, err := msg.getRecipients() + to, err := m.getRecipients() if err != nil { return err } - if err := s.Send(from, to, msg); err != nil { + if err := s.Send(from, to, m); err != nil { return err } return nil } -func (msg *Message) getFrom() (string, error) { - from := msg.header["Sender"] +func (m *Message) getFrom() (string, error) { + from := m.header["Sender"] if len(from) == 0 { - from = msg.header["From"] + from = m.header["From"] if len(from) == 0 { return "", errors.New(`gomail: invalid message, "From" field is absent`) } @@ -72,10 +72,10 @@ func (msg *Message) getFrom() (string, error) { return parseAddress(from[0]) } -func (msg *Message) getRecipients() ([]string, error) { +func (m *Message) getRecipients() ([]string, error) { var list []string for _, field := range []string{"To", "Cc", "Bcc"} { - if addresses, ok := msg.header[field]; ok { + if addresses, ok := m.header[field]; ok { for _, a := range addresses { addr, err := parseAddress(a) if err != nil { diff --git a/smtp.go b/smtp.go index 9b5e494..70fd36c 100644 --- a/smtp.go +++ b/smtp.go @@ -108,14 +108,14 @@ func addr(host string, port int) string { // DialAndSend opens a connection to the SMTP server, sends the given emails and // closes the connection. -func (d *Dialer) DialAndSend(msg ...*Message) error { +func (d *Dialer) DialAndSend(m ...*Message) error { s, err := d.Dial() if err != nil { return err } defer s.Close() - return Send(s, msg...) + return Send(s, m...) } type smtpSender struct { diff --git a/writeto.go b/writeto.go index 7a584bb..68213a3 100644 --- a/writeto.go +++ b/writeto.go @@ -12,65 +12,65 @@ import ( ) // WriteTo implements io.WriterTo. It dumps the whole message into w. -func (msg *Message) WriteTo(w io.Writer) (int64, error) { +func (m *Message) WriteTo(w io.Writer) (int64, error) { mw := &messageWriter{w: w} - mw.writeMessage(msg) + mw.writeMessage(m) return mw.n, mw.err } -func (w *messageWriter) writeMessage(msg *Message) { - if _, ok := msg.header["Mime-Version"]; !ok { +func (w *messageWriter) writeMessage(m *Message) { + if _, ok := m.header["Mime-Version"]; !ok { w.writeString("Mime-Version: 1.0\r\n") } - if _, ok := msg.header["Date"]; !ok { - w.writeHeader("Date", msg.FormatDate(now())) + if _, ok := m.header["Date"]; !ok { + w.writeHeader("Date", m.FormatDate(now())) } - w.writeHeaders(msg.header) + w.writeHeaders(m.header) - if msg.hasMixedPart() { + if m.hasMixedPart() { w.openMultipart("mixed") } - if msg.hasRelatedPart() { + if m.hasRelatedPart() { w.openMultipart("related") } - if msg.hasAlternativePart() { + if m.hasAlternativePart() { w.openMultipart("alternative") } - for _, part := range msg.parts { - contentType := part.contentType + "; charset=" + msg.charset + for _, part := range m.parts { + contentType := part.contentType + "; charset=" + m.charset w.writeHeaders(map[string][]string{ "Content-Type": []string{contentType}, - "Content-Transfer-Encoding": []string{string(msg.encoding)}, + "Content-Transfer-Encoding": []string{string(m.encoding)}, }) - w.writeBody(part.copier, msg.encoding) + w.writeBody(part.copier, m.encoding) } - if msg.hasAlternativePart() { + if m.hasAlternativePart() { w.closeMultipart() } - w.addFiles(msg.embedded, false) - if msg.hasRelatedPart() { + w.addFiles(m.embedded, false) + if m.hasRelatedPart() { w.closeMultipart() } - w.addFiles(msg.attachments, true) - if msg.hasMixedPart() { + w.addFiles(m.attachments, true) + if m.hasMixedPart() { w.closeMultipart() } } -func (msg *Message) hasMixedPart() bool { - return (len(msg.parts) > 0 && len(msg.attachments) > 0) || len(msg.attachments) > 1 +func (m *Message) hasMixedPart() bool { + return (len(m.parts) > 0 && len(m.attachments) > 0) || len(m.attachments) > 1 } -func (msg *Message) hasRelatedPart() bool { - return (len(msg.parts) > 0 && len(msg.embedded) > 0) || len(msg.embedded) > 1 +func (m *Message) hasRelatedPart() bool { + return (len(m.parts) > 0 && len(m.embedded) > 0) || len(m.embedded) > 1 } -func (msg *Message) hasAlternativePart() bool { - return len(msg.parts) > 1 +func (m *Message) hasAlternativePart() bool { + return len(m.parts) > 1 } type messageWriter struct {