Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use go-smtp for UTF8 support and LOGIN authentication implementation #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 0 additions & 49 deletions auth.go

This file was deleted.

100 changes: 0 additions & 100 deletions auth_test.go

This file was deleted.

6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ module github.com/gophish/gomail

go 1.13

require gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc
require (
github.com/emersion/go-sasl v0.0.0-20200509203442-7bfe0ed36a21
github.com/emersion/go-smtp v0.14.0
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
github.com/emersion/go-sasl v0.0.0-20200509203442-7bfe0ed36a21 h1:OJyUGMJTzHTd1XQp98QTaHernxMYzRaOasRir9hUlFQ=
github.com/emersion/go-sasl v0.0.0-20200509203442-7bfe0ed36a21/go.mod h1:iL2twTeMvZnrg54ZoPDNfJaJaqy0xIQFuBdrLsmspwQ=
github.com/emersion/go-smtp v0.14.0 h1:RYW203p+EcPjL8Z/ZpT9lZ6iOc8MG1MQzEx1UKEkXlA=
github.com/emersion/go-smtp v0.14.0/go.mod h1:qm27SGYgoIPRot6ubfQ/GpiPy/g3PaZAVRxiO/sDUgQ=
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc h1:2gGKlE2+asNV9m7xrywl36YYNnBG5ZQ0r/BOOxqPpmk=
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc/go.mod h1:m7x9LTH6d71AHyAX77c9yqWCCa3UKHcVEj9y7hAtKDk=
26 changes: 12 additions & 14 deletions smtp.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import (
"fmt"
"io"
"net"
"net/smtp"
"strings"
"time"

"github.com/emersion/go-sasl"
"github.com/emersion/go-smtp"
)

var defaultDialer = &net.Dialer{
Expand All @@ -27,7 +29,7 @@ type Dialer struct {
Password string
// Auth represents the authentication mechanism used to authenticate to the
// SMTP server.
Auth smtp.Auth
Auth sasl.Client
// SSL defines whether an SSL connection is used. It should be false in
// most cases since the authentication mechanism should use the STARTTLS
// extension instead.
Expand All @@ -38,6 +40,8 @@ type Dialer struct {
// LocalName is the hostname sent to the SMTP server with the HELO command.
// By default, "localhost" is sent.
LocalName string
// Options
MailOptions *smtp.MailOptions

dialer netDialer
}
Expand Down Expand Up @@ -112,17 +116,11 @@ func (d *Dialer) Dial() (SendCloser, error) {

if d.Auth == nil && d.Username != "" {
if ok, auths := c.Extension("AUTH"); ok {
if strings.Contains(auths, "CRAM-MD5") {
d.Auth = smtp.CRAMMD5Auth(d.Username, d.Password)
} else if strings.Contains(auths, "LOGIN") &&
if strings.Contains(auths, "LOGIN") &&
!strings.Contains(auths, "PLAIN") {
d.Auth = &loginAuth{
username: d.Username,
password: d.Password,
host: d.Host,
}
d.Auth = sasl.NewLoginClient(d.Username, d.Password)
} else {
d.Auth = smtp.PlainAuth("", d.Username, d.Password, d.Host)
d.Auth = sasl.NewPlainClient("", d.Username, d.Password)
}
}
}
Expand Down Expand Up @@ -166,7 +164,7 @@ type smtpSender struct {
}

func (c *smtpSender) Send(from string, to []string, msg io.WriterTo) error {
if err := c.Mail(from); err != nil {
if err := c.Mail(from, c.d.MailOptions); err != nil {
if err == io.EOF {
// This is probably due to a timeout, so reconnect and try again.
sc, derr := c.d.Dial()
Expand Down Expand Up @@ -223,8 +221,8 @@ type smtpClient interface {
Hello(string) error
Extension(string) (bool, string)
StartTLS(*tls.Config) error
Auth(smtp.Auth) error
Mail(string) error
Auth(sasl.Client) error
Mail(string, *smtp.MailOptions) error
Rcpt(string) error
Reset() error
Data() (io.WriteCloser, error)
Expand Down
17 changes: 13 additions & 4 deletions smtp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,18 @@ import (
"crypto/tls"
"io"
"net"
"net/smtp"
"reflect"
"testing"

"github.com/emersion/go-sasl"
"github.com/emersion/go-smtp"
)


const (
testUser = "user"
testPwd = "pwd"
testHost = "smtp.example.com"
)

const (
Expand All @@ -19,7 +28,7 @@ var (
testConn = &net.TCPConn{}
testTLSConn = &tls.Conn{}
testConfig = &tls.Config{InsecureSkipVerify: true}
testAuth = smtp.PlainAuth("", testUser, testPwd, testHost)
testAuth = sasl.NewPlainClient("", testUser, testPwd)
)

type mockNetDialer struct {
Expand Down Expand Up @@ -183,15 +192,15 @@ func (c *mockClient) StartTLS(config *tls.Config) error {
return nil
}

func (c *mockClient) Auth(a smtp.Auth) error {
func (c *mockClient) Auth(a sasl.Client) error {
if !reflect.DeepEqual(a, testAuth) {
c.t.Errorf("Invalid auth, got %#v, want %#v", a, testAuth)
}
c.do("Auth")
return nil
}

func (c *mockClient) Mail(from string) error {
func (c *mockClient) Mail(from string, opts *smtp.MailOptions) error {
c.do("Mail " + from)
if c.timeout {
c.timeout = false
Expand Down