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

Enable customization of localName for clients #127

Open
wants to merge 2 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
60 changes: 54 additions & 6 deletions email.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"mime"
"mime/multipart"
"mime/quotedprintable"
"net"
"net/mail"
"net/smtp"
"net/textproto"
Expand Down Expand Up @@ -51,6 +52,7 @@ type Email struct {
Headers textproto.MIMEHeader
Attachments []*Attachment
ReadReceipt []string
LocalName string // override localname for client hello negotiation
}

// part is a copyable representation of a multipart.Part
Expand All @@ -61,7 +63,10 @@ type part struct {

// NewEmail creates an Email, and returns the pointer to it.
func NewEmail() *Email {
return &Email{Headers: textproto.MIMEHeader{}}
return &Email{
Headers: textproto.MIMEHeader{},
LocalName: "localhost",
}
}

// trimReader is a custom io.Reader that will trim any leading
Expand Down Expand Up @@ -521,15 +526,58 @@ func (e *Email) Send(addr string, a smtp.Auth) error {
if e.From == "" || len(to) == 0 {
return errors.New("Must specify at least one From address and one To address")
}
sender, err := e.parseSender()
from, err := e.parseSender()
if err != nil {
return err
}
raw, err := e.Bytes()
msg, err := e.Bytes()
if err != nil {
return err
}
c, err := smtp.Dial(addr)
if err != nil {
return err
}
defer c.Close()
if err = c.Hello(e.LocalName); err != nil {
return err
}
if ok, _ := c.Extension("STARTTLS"); ok {
host, _, _ := net.SplitHostPort(addr)
config := &tls.Config{ServerName: host}
if err = c.StartTLS(config); err != nil {
return err
}
}
if a != nil {
if ok, _ := c.Extension("AUTH"); !ok {
return errors.New("smtp: server doesn't support AUTH")
}
if err = c.Auth(a); err != nil {
return err
}
}
if err = c.Mail(from); err != nil {
return err
}
for _, addr := range to {
if err = c.Rcpt(addr); err != nil {
return err
}
}
w, err := c.Data()
if err != nil {
return err
}
return smtp.SendMail(addr, a, sender, to, raw)
_, err = w.Write(msg)
if err != nil {
return err
}
err = w.Close()
if err != nil {
return err
}
return c.Quit()
}

// Select and parse an SMTP envelope sender address. Choose Email.Sender if set, or fallback to Email.From.
Expand Down Expand Up @@ -587,7 +635,7 @@ func (e *Email) SendWithTLS(addr string, a smtp.Auth, t *tls.Config) error {
return err
}
defer c.Close()
if err = c.Hello("localhost"); err != nil {
if err = c.Hello(e.LocalName); err != nil {
return err
}

Expand Down Expand Up @@ -656,7 +704,7 @@ func (e *Email) SendWithStartTLS(addr string, a smtp.Auth, t *tls.Config) error
return err
}
defer c.Close()
if err = c.Hello("localhost"); err != nil {
if err = c.Hello(e.LocalName); err != nil {
return err
}
// Use TLS if available
Expand Down
42 changes: 40 additions & 2 deletions email_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package email

import (
"fmt"
"log"
"strings"
"testing"

"bufio"
"bytes"
"crypto/rand"
"crypto/tls"
"io"
"io/ioutil"
"mime"
Expand Down Expand Up @@ -729,15 +731,51 @@ TGV0J3MganVzdCBwcmV0ZW5kIHRoaXMgaXMgcmF3IEpQRUcgZGF0YS4=
}

func ExampleGmail() {
gmUser := "[email protected]"
gmPass := "<password>"
gmHost := "smtp.gmail.com"
gmPort := "587"

e := NewEmail()
e.From = "Jordan Wright <[email protected]>"
e.From = "Jordan Wright <" + gmUser + ">"
e.To = []string{"[email protected]"}
e.Bcc = []string{"[email protected]"}
e.Cc = []string{"[email protected]"}
e.Subject = "Awesome Subject"
e.Text = []byte("Text Body is, of course, supported!\n")
e.HTML = []byte("<h1>Fancy Html is supported, too!</h1>\n")
e.Send("smtp.gmail.com:587", smtp.PlainAuth("", e.From, "password123", "smtp.gmail.com"))
e.LocalName = "localhost"
err := e.Send(
gmHost+":"+gmPort,
smtp.PlainAuth("", gmUser, gmPass, gmHost),
)
if err != nil {
log.Fatalf("Failed to send gmail mail: %v", err)
}
}

func ExampleGmailWithTLS() {
gmUser := "[email protected]"
gmPass := "<password>"
gmHost := "smtp.gmail.com"
gmPort := "465"

e := NewEmail()
e.From = "Jordan Wright <" + gmUser + ">"
e.To = []string{"[email protected]"}
e.Bcc = []string{"[email protected]"}
e.Cc = []string{"[email protected]"}
e.Subject = "Awesome Subject"
e.Text = []byte("Text Body is, of course, supported!\n")
e.HTML = []byte("<h1>Fancy Html is supported, too!</h1>\n")
e.LocalName = "localhost"
err := e.SendWithTLS(
gmHost+":"+gmPort,
smtp.PlainAuth("", gmUser, gmPass, gmHost),
&tls.Config{ServerName: gmHost})
if err != nil {
log.Fatalf("Failed to send gmail mail: %v", err)
}
}

func ExampleAttach() {
Expand Down