Renamed SMTPDialer into Dialer

This commit is contained in:
Alexandre Cesaro 2015-06-30 17:08:26 +02:00
parent f01c0a3645
commit 4b313106fd
2 changed files with 18 additions and 18 deletions

22
smtp.go
View File

@ -8,8 +8,8 @@ import (
"net/smtp" "net/smtp"
) )
// An SMTPDialer is a dialer to an SMTP server. // A Dialer is a dialer to an SMTP server.
type SMTPDialer struct { type Dialer struct {
// Host represents the host of the SMTP server. // Host represents the host of the SMTP server.
Host string Host string
// Port represents the port of the SMTP server. // Port represents the port of the SMTP server.
@ -29,8 +29,8 @@ type SMTPDialer struct {
// NewPlainDialer returns a Dialer. The given parameters are used to connect to // NewPlainDialer returns a Dialer. The given parameters are used to connect to
// the SMTP server via a PLAIN authentication mechanism. It fallbacks to the // the SMTP server via a PLAIN authentication mechanism. It fallbacks to the
// LOGIN mechanism if it is the only mechanism advertised by the server. // LOGIN mechanism if it is the only mechanism advertised by the server.
func NewPlainDialer(host, username, password string, port int) *SMTPDialer { func NewPlainDialer(host, username, password string, port int) *Dialer {
return &SMTPDialer{ return &Dialer{
Host: host, Host: host,
Port: port, Port: port,
Auth: &plainAuth{ Auth: &plainAuth{
@ -44,7 +44,7 @@ func NewPlainDialer(host, username, password string, port int) *SMTPDialer {
// Dial dials and authenticates to an SMTP server. The returned SendCloser // Dial dials and authenticates to an SMTP server. The returned SendCloser
// should be closed when done using it. // should be closed when done using it.
func (d *SMTPDialer) Dial() (SendCloser, error) { func (d *Dialer) Dial() (SendCloser, error) {
c, err := d.dial() c, err := d.dial()
if err != nil { if err != nil {
return nil, err return nil, err
@ -62,14 +62,14 @@ func (d *SMTPDialer) Dial() (SendCloser, error) {
return &smtpSender{c}, nil return &smtpSender{c}, nil
} }
func (d *SMTPDialer) dial() (smtpClient, error) { func (d *Dialer) dial() (smtpClient, error) {
if d.SSL { if d.SSL {
return d.sslDial() return d.sslDial()
} }
return d.starttlsDial() return d.starttlsDial()
} }
func (d *SMTPDialer) starttlsDial() (smtpClient, error) { func (d *Dialer) starttlsDial() (smtpClient, error) {
c, err := smtpDial(addr(d.Host, d.Port)) c, err := smtpDial(addr(d.Host, d.Port))
if err != nil { if err != nil {
return nil, err return nil, err
@ -85,7 +85,7 @@ func (d *SMTPDialer) starttlsDial() (smtpClient, error) {
return c, nil return c, nil
} }
func (d *SMTPDialer) sslDial() (smtpClient, error) { func (d *Dialer) sslDial() (smtpClient, error) {
conn, err := tlsDial("tcp", addr(d.Host, d.Port), d.tlsConfig()) conn, err := tlsDial("tcp", addr(d.Host, d.Port), d.tlsConfig())
if err != nil { if err != nil {
return nil, err return nil, err
@ -94,7 +94,7 @@ func (d *SMTPDialer) sslDial() (smtpClient, error) {
return newClient(conn, d.Host) return newClient(conn, d.Host)
} }
func (d *SMTPDialer) tlsConfig() *tls.Config { func (d *Dialer) tlsConfig() *tls.Config {
if d.TLSConfig == nil { if d.TLSConfig == nil {
return &tls.Config{ServerName: d.Host} return &tls.Config{ServerName: d.Host}
} }
@ -106,9 +106,9 @@ func addr(host string, port int) string {
return fmt.Sprintf("%s:%d", host, port) return fmt.Sprintf("%s:%d", host, port)
} }
// DialAndSend opens a connection to an SMTP server, sends the given emails and // DialAndSend opens a connection to the SMTP server, sends the given emails and
// closes the connection. // closes the connection.
func (d *SMTPDialer) DialAndSend(msg ...*Message) error { func (d *Dialer) DialAndSend(msg ...*Message) error {
s, err := d.Dial() s, err := d.Dial()
if err != nil { if err != nil {
return err return err

View File

@ -19,7 +19,7 @@ var (
testConfig = &tls.Config{InsecureSkipVerify: true} testConfig = &tls.Config{InsecureSkipVerify: true}
) )
func TestSMTPDialer(t *testing.T) { func TestDialer(t *testing.T) {
d := NewPlainDialer(testHost, "user", "pwd", testPort) d := NewPlainDialer(testHost, "user", "pwd", testPort)
testSendMail(t, d, []string{ testSendMail(t, d, []string{
"Extension STARTTLS", "Extension STARTTLS",
@ -37,7 +37,7 @@ func TestSMTPDialer(t *testing.T) {
}) })
} }
func TestSMTPDialerSSL(t *testing.T) { func TestDialerSSL(t *testing.T) {
d := NewPlainDialer(testHost, "user", "pwd", testSSLPort) d := NewPlainDialer(testHost, "user", "pwd", testSSLPort)
testSendMail(t, d, []string{ testSendMail(t, d, []string{
"Extension AUTH", "Extension AUTH",
@ -53,7 +53,7 @@ func TestSMTPDialerSSL(t *testing.T) {
}) })
} }
func TestSMTPDialerConfig(t *testing.T) { func TestDialerConfig(t *testing.T) {
d := NewPlainDialer(testHost, "user", "pwd", testPort) d := NewPlainDialer(testHost, "user", "pwd", testPort)
d.TLSConfig = testConfig d.TLSConfig = testConfig
testSendMail(t, d, []string{ testSendMail(t, d, []string{
@ -72,7 +72,7 @@ func TestSMTPDialerConfig(t *testing.T) {
}) })
} }
func TestSMTPDialerSSLConfig(t *testing.T) { func TestDialerSSLConfig(t *testing.T) {
d := NewPlainDialer(testHost, "user", "pwd", testSSLPort) d := NewPlainDialer(testHost, "user", "pwd", testSSLPort)
d.TLSConfig = testConfig d.TLSConfig = testConfig
testSendMail(t, d, []string{ testSendMail(t, d, []string{
@ -89,8 +89,8 @@ func TestSMTPDialerSSLConfig(t *testing.T) {
}) })
} }
func TestSMTPDialerNoAuth(t *testing.T) { func TestDialerNoAuth(t *testing.T) {
d := &SMTPDialer{ d := &Dialer{
Host: testHost, Host: testHost,
Port: testPort, Port: testPort,
} }
@ -186,7 +186,7 @@ func (w *mockWriter) Close() error {
return nil return nil
} }
func testSendMail(t *testing.T, d *SMTPDialer, want []string) { func testSendMail(t *testing.T, d *Dialer, want []string) {
testClient := &mockClient{ testClient := &mockClient{
t: t, t: t,
want: want, want: want,