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

add context-aware method to enable timeout + cancel for connections #4

Open
wants to merge 4 commits 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
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ keys from a third-party just to send something like an account recovery email.
package main

import (
"context"
"fmt"

"github.com/nilslice/email"
)

func main() {
func main() {
msg := email.Message{
To: "[email protected]", // do not add < > or name in quotes
From: "[email protected]", // do not add < > or name in quotes
Expand All @@ -28,6 +30,13 @@ func main() {
if err != nil {
fmt.Println(err)
}

// or with context-aware API
ctx := context.WithTimeout(context.Background(), time.Second * 3)
err = msg.SendWithContext(ctx)
if err != nil {
fmt.Println(err)
}
}

```
Expand Down
38 changes: 32 additions & 6 deletions email.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package email

import (
"context"
"fmt"
"net"
"net/smtp"
"strings"
)

// Message creates a email to be sent
// Message creates a email to be sent.
type Message struct {
To string
From string
Expand All @@ -19,8 +20,18 @@ var (
ports = []int{25, 2525, 587}
)

// Send sends a message to recipient(s) listed in the 'To' field of a Message
// Send sends a message to recipient(s) listed in the 'To' field of a Message.
func (m Message) Send() error {
return m.send(context.Background())
}

// SendWithContext sends sends a message to recipient(s) listed in the 'To'
// field of a Message, is context-aware.
func (m Message) SendWithContext(ctx context.Context) error {
return m.send(ctx)
}

func (m Message) send(ctx context.Context) error {
if !strings.Contains(m.To, "@") {
return fmt.Errorf("Invalid recipient address: <%s>", m.To)
}
Expand All @@ -31,7 +42,7 @@ func (m Message) Send() error {
return err
}

c, err := newClient(addrs, ports)
c, err := newClient(ctx, addrs, ports)
if err != nil {
return err
}
Expand All @@ -44,12 +55,27 @@ func (m Message) Send() error {
return nil
}

func newClient(mx []*net.MX, ports []int) (*smtp.Client, error) {
func dialTimeout(ctx context.Context, addr string) (*smtp.Client, error) {
var d net.Dialer

conn, err := d.DialContext(ctx, "tcp", addr)
if err != nil {
return nil, err
}
host, _, err := net.SplitHostPort(addr)
if err != nil {
return nil, err
}

return smtp.NewClient(conn, host)
}

func newClient(ctx context.Context, mx []*net.MX, ports []int) (*smtp.Client, error) {
for i := range mx {
for j := range ports {
server := strings.TrimSuffix(mx[i].Host, ".")
hostPort := fmt.Sprintf("%s:%d", server, ports[j])
client, err := smtp.Dial(hostPort)
client, err := dialTimeout(ctx, hostPort)
if err != nil {
if j == len(ports)-1 {
return nil, err
Expand All @@ -62,7 +88,7 @@ func newClient(mx []*net.MX, ports []int) (*smtp.Client, error) {
}
}

return nil, fmt.Errorf("Couldn't connect to servers %v on any common port.", mx)
return nil, fmt.Errorf("couldn't connect to servers %v on any common port", mx)
}

func send(m Message, c *smtp.Client) error {
Expand Down