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

refactor(server): refactored NewSMTPClient to NewSMTPServer #108

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
73 changes: 71 additions & 2 deletions email.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,14 +235,83 @@ func NewMSG() *Email {
}

// NewSMTPClient returns the client for send email
// Deprecated: Use NewSMTPServer instead
func NewSMTPClient() *SMTPServer {
return NewSMTPServer("", 0)
}

type ServerOption func(*SMTPServer)

func WithAuthentication(auth AuthType) ServerOption {
return func(server *SMTPServer) {
server.Authentication = auth
}
}

func WithEncryption(encryption Encryption) ServerOption {
return func(server *SMTPServer) {
server.Encryption = encryption
}
}

func WithUsername(username string) ServerOption {
return func(server *SMTPServer) {
server.Username = username
}
}

func WithPassword(password string) ServerOption {
return func(server *SMTPServer) {
server.Password = password
}
}

func WithHelo(helo string) ServerOption {
return func(server *SMTPServer) {
server.Helo = helo
}
}

func WithConnectTimeout(timeout time.Duration) ServerOption {
return func(server *SMTPServer) {
server.ConnectTimeout = timeout
}
}

func WithSendTimeout(timeout time.Duration) ServerOption {
return func(server *SMTPServer) {
server.SendTimeout = timeout
}
}

func WithKeepAlive(keepAlive bool) ServerOption {
return func(server *SMTPServer) {
server.KeepAlive = keepAlive
}
}

func WithTLSConfig(tlsConfig *tls.Config) ServerOption {
return func(server *SMTPServer) {
server.TLSConfig = tlsConfig
}
}

// NewSMTPServer returns a new SMTPServer
func NewSMTPServer(host string, port int, opts ...ServerOption) *SMTPServer {
server := &SMTPServer{
Authentication: AuthAuto,
Encryption: EncryptionNone,
ConnectTimeout: 10 * time.Second,
SendTimeout: 10 * time.Second,
Helo: "localhost",
Host: host,
Port: port,
}

for _, opt := range opts {
opt(server)
}

return server
}

Expand Down Expand Up @@ -1003,10 +1072,10 @@ func SendMessage(from string, recipients []string, msg string, client *SMTPClien

// send does the low level sending of the email
func send(from string, to []string, msg string, client *SMTPClient) error {
//Check if client struct is not nil
// Check if client struct is not nil
if client != nil {

//Check if client is not nil
// Check if client is not nil
if client.Client != nil {
var smtpSendChannel chan error

Expand Down
12 changes: 6 additions & 6 deletions email_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ func TestSendRace(t *testing.T) {
startService(port, responses, 5*time.Second)
startService(port2, responses, 0)

server := NewSMTPClient()
server.ConnectTimeout = timeout
server.SendTimeout = timeout
server.KeepAlive = false
server.Host = `127.0.0.1`
server.Port = port
server := NewSMTPServer(
`127.0.0.1`, port,
WithConnectTimeout(timeout),
WithSendTimeout(timeout),
WithKeepAlive(false),
)

smtpClient, err := server.Connect()
if err != nil {
Expand Down
150 changes: 72 additions & 78 deletions example/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,34 +32,33 @@ var (

// TestSendMailWithAttachment send a simple html email.
func TestSendMail(t *testing.T) {
client := mail.NewSMTPClient()

//SMTP Client
client.Host = host
client.Port = port
client.Username = username
client.Password = password
client.Encryption = encryptionType
client.ConnectTimeout = connectTimeout
client.SendTimeout = sendTimeout
client.KeepAlive = false

//Connect to client
smtpClient, err := client.Connect()
// SMTP Server
server := mail.NewSMTPServer(
host, port,
mail.WithUsername(username),
mail.WithPassword(password),
mail.WithEncryption(encryptionType),
mail.WithConnectTimeout(connectTimeout),
mail.WithSendTimeout(sendTimeout),
mail.WithKeepAlive(false),
)

// Connect to client
smtpClient, err := server.Connect()

if err != nil {
t.Error("Expected nil, got", err, "connecting to client")
}

//NOOP command, optional, used for avoid timeout when KeepAlive is true and you aren't sending mails.
//Execute this command each 30 seconds is ideal for persistent connection
// NOOP command, optional, used for avoid timeout when KeepAlive is true and you aren't sending mails.
// Execute this command each 30 seconds is ideal for persistent connection
err = smtpClient.Noop()

if err != nil {
t.Error("Expected nil, got", err, "noop to client")
}

//Create the email message
// Create the email message
email := mail.NewMSG()

email.SetFrom("From Example <[email protected]>").
Expand All @@ -69,37 +68,37 @@ func TestSendMail(t *testing.T) {
email.SetBody(mail.TextHTML, htmlBody)
email.AddAlternative(mail.TextPlain, "Hello Gophers!")

//Some additional options to send
// Some additional options to send
email.SetSender("[email protected]")
email.SetReplyTo("[email protected]")
email.SetReturnPath("[email protected]")
email.AddCc("[email protected]")
email.AddBcc("[email protected]")

//Add inline too!
// Add inline too!
email.Attach(&mail.File{FilePath: "C:/Users/sdelacruz/Pictures/Gopher.png", Inline: true})

//Attach a file with path
// Attach a file with path
email.Attach(&mail.File{FilePath: "C:/Users/sdelacruz/Pictures/Gopher.png"})

//Attach the file with a base64
// Attach the file with a base64
email.Attach(&mail.File{B64Data: "Zm9v", Name: "filename"})

//Set a different date in header email
// Set a different date in header email
email.SetDate("2015-04-28 10:32:00 CDT")

//Send with low priority
// Send with low priority
email.SetPriority(mail.PriorityLow)

// always check error after send
if email.Error != nil {
t.Error("Expected nil, got", email.Error, "generating email")
}

//Pass the client to the email message to send it
// Pass the client to the email message to send it
err = email.Send(smtpClient)

//Get first error
// Get first error
email.GetError()

if err != nil {
Expand All @@ -109,27 +108,26 @@ func TestSendMail(t *testing.T) {

// TestSendMultipleEmails send multiple emails in same connection.
func TestSendMultipleEmails(t *testing.T) {
client := mail.NewSMTPClient()

//SMTP Client
client.Host = host
client.Port = port
client.Username = username
client.Password = password
client.Encryption = encryptionType
client.ConnectTimeout = connectTimeout
client.SendTimeout = sendTimeout

//For authentication you can use AuthPlain, AuthLogin or AuthCRAMMD5
client.Authentication = mail.AuthPlain

//KeepAlive true because the connection need to be open for multiple emails
//For avoid inactivity timeout, every 30 second you can send a NO OPERATION command to smtp client
//use smtpClient.Client.Noop() after 30 second of inactivity in this example
client.KeepAlive = true

//Connect to client
smtpClient, err := client.Connect()
// SMTP Server
server := mail.NewSMTPServer(
host, port,
mail.WithUsername(username),
mail.WithPassword(password),
mail.WithEncryption(encryptionType),
mail.WithConnectTimeout(connectTimeout),
mail.WithSendTimeout(sendTimeout),

// For authentication you can use AuthPlain, AuthLogin or AuthCRAMMD5
mail.WithAuthentication(mail.AuthPlain),

// KeepAlive true because the connection need to be open for multiple emails
// For avoid inactivity timeout, every 30 second you can send a NO OPERATION command to smtp client
// use smtpClient.Client.Noop() after 30 second of inactivity in this example
mail.WithKeepAlive(true),
)

// Connect to client
smtpClient, err := server.Connect()

if err != nil {
t.Error("Expected nil, got", err, "connecting to client")
Expand All @@ -146,46 +144,45 @@ func TestSendMultipleEmails(t *testing.T) {
}

func sendEmail(htmlBody string, to string, smtpClient *mail.SMTPClient) error {
//Create the email message
// Create the email message
email := mail.NewMSG()

email.SetFrom("From Example <[email protected]>").
AddTo(to).
SetSubject("New Go Email")

//Get from each mail
// Get from each mail
email.GetFrom()
email.SetBody(mail.TextHTML, htmlBody)

//Send with high priority
// Send with high priority
email.SetPriority(mail.PriorityHigh)

// always check error after send
if email.Error != nil {
return email.Error
}

//Pass the client to the email message to send it
// Pass the client to the email message to send it
return email.Send(smtpClient)
}

// TestWithTLS using gmail port 587.
func TestWithTLS(t *testing.T) {
client := mail.NewSMTPClient()

//SMTP Client
client.Host = "smtp.gmail.com"
client.Port = 587
client.Username = "[email protected]"
client.Password = "asdfghh"
client.Encryption = mail.EncryptionSTARTTLS
client.ConnectTimeout = 10 * time.Second
client.SendTimeout = 10 * time.Second
// SMTP Server
server := mail.NewSMTPServer(
"smtp.gmail.com", 587,
mail.WithUsername("[email protected]"),
mail.WithPassword("asdfghh"),
mail.WithEncryption(mail.EncryptionSTARTTLS),
mail.WithConnectTimeout(10*time.Second),
mail.WithSendTimeout(10*time.Second),
)

//KeepAlive is not settted because by default is false
// KeepAlive is not settted because by default is false

//Connect to client
smtpClient, err := client.Connect()
// Connect to client
smtpClient, err := server.Connect()

if err != nil {
t.Error("Expected nil, got", err, "connecting to client")
Expand All @@ -199,21 +196,18 @@ func TestWithTLS(t *testing.T) {

// TestWithTLS using gmail port 465.
func TestWithSSL(t *testing.T) {
client := mail.NewSMTPClient()

//SMTP Client
client.Host = "smtp.gmail.com"
client.Port = 465
client.Username = "[email protected]"
client.Password = "asdfghh"
client.Encryption = mail.EncryptionSSLTLS
client.ConnectTimeout = 10 * time.Second
client.SendTimeout = 10 * time.Second

//KeepAlive is not settted because by default is false

//Connect to client
smtpClient, err := client.Connect()
// SMTP Server
server := mail.NewSMTPServer(
"smtp.gmail.com", 465,
mail.WithUsername("[email protected]"),
mail.WithPassword("asdfghh"),
mail.WithEncryption(mail.EncryptionSSLTLS),
mail.WithConnectTimeout(10*time.Second),
mail.WithSendTimeout(10*time.Second),
)

// Connect to client
smtpClient, err := server.Connect()

if err != nil {
t.Error("Expected nil, got", err, "connecting to client")
Expand Down