Skip to content

Commit

Permalink
New constant iota for contentType html or plain
Browse files Browse the repository at this point in the history
Delete priorities array and string function, not used and unnecessary
SetBody and AddAlternative require contentType type contentType instead a string (this change break your code, update it)
Update readme
Update example_test.go
Update comment header.go
encoding and encryption constant have function string() private instead String. Changes made on other files like message.go
  • Loading branch information
xhit committed Oct 9, 2019
1 parent c949af3 commit 925a4f0
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 37 deletions.
26 changes: 22 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ The best way to send emails in Go with SMTP Keep Alive and Timeout for Connect a
[![Go Report](https://goreportcard.com/badge/github.com/xhit/go-simple-mail)](https://goreportcard.com/report/github.com/xhit/go-simple-mail)

**IMPORTANT**
This example is for version 2.1.3 and above, for v2.0.0 example go here https://gist.github.com/xhit/54516917473420a8db1b6fff68a21c99
This example is for version 2.2.0 and above.

Version 2.1.3 and below use "text/html" and "text/plain" in SetBody and AddAlternative
Also 2.0.0 and below go to this doc https://gist.github.com/xhit/54516917473420a8db1b6fff68a21c99

# Introduction

Expand All @@ -19,7 +22,13 @@ is easy to implement other methods for sending emails using a local Postfix, an
## Features

Go Simple Mail supports:
- Attachments
- Multiple Attachments with path
- Multiple Attachments in base64
- Multiple Recipients
- Priority
- Reply to
- Set other sender
- Set other from
- Embedded images
- HTML and text templates
- Automatic encoding of special characters
Expand All @@ -28,6 +37,15 @@ Go Simple Mail supports:
- Sending multiple emails with the same SMTP connection (Keep Alive or Persistent Connection)
- Timeout for connect to a SMTP Server
- Timeout for send an email
- Return Path
- Alternaive Email Body
- CC and BCC
- Add Custom Headers in Message
- Send NOOP, RESET, QUIT and CLOSE to SMTP client

## Documentation

https://godoc.org/github.com/xhit/go-simple-mail

## Download

Expand Down Expand Up @@ -93,7 +111,7 @@ func main() {
AddCc("otherto@example.com").
SetSubject("New Go Email")

email.SetBody("text/html", htmlBody)
email.SetBody(mail.TextHTML, htmlBody)

email.AddInline("/path/to/image.png", "Gopher.png")

Expand Down Expand Up @@ -124,7 +142,7 @@ func main() {
AddTo(to).
SetSubject("New Go Email")

email.SetBody("text/html", htmlBody)
email.SetBody(mail.TextHTML, htmlBody)

email.AddInline("/path/to/image.png", "Gopher.png")

Expand Down
35 changes: 22 additions & 13 deletions email.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ const (

var encryptionTypes = [...]string{"TLS", "SSL", "None"}

func (encryption encryption) String() string {
func (encryption encryption) string() string {
return encryptionTypes[encryption]
}

Expand All @@ -95,10 +95,25 @@ const (

var encodingTypes = [...]string{"quoted-printable", "base64", "binary"}

func (encoding encoding) String() string {
func (encoding encoding) string() string {
return encodingTypes[encoding]
}

type contentType int

const (
// TextHTML sets body type to text/html in message body
TextHTML contentType = iota
// TextPlain sets body type to text/plain in message body
TextPlain
)

var contentTypes = [...]string{"text/html", "text/plain"}

func (contentType contentType) string() string {
return contentTypes[contentType]
}

// NewMSG creates a new email. It uses UTF-8 by default.
func NewMSG() *Email {
email := &Email{
Expand Down Expand Up @@ -309,12 +324,6 @@ const (
PriorityLow
)

var priorities = [...]string{"High", "Low"}

func (priority priority) String() string {
return priorities[priority]
}

// SetPriority sets the email message priority. Use with
// either "High" or "Low".
func (email *Email) SetPriority(priority priority) *Email {
Expand Down Expand Up @@ -376,14 +385,14 @@ func (email *Email) SetSubject(subject string) *Email {
}

// SetBody sets the body of the email message.
func (email *Email) SetBody(contentType, body string) *Email {
func (email *Email) SetBody(contentType contentType, body string) *Email {
if email.Error != nil {
return email
}

email.parts = []part{
{
contentType: contentType,
contentType: contentType.string(),
body: bytes.NewBufferString(body),
},
}
Expand Down Expand Up @@ -448,14 +457,14 @@ func (email *Email) AddHeaders(headers textproto.MIMEHeader) *Email {
// of the email message. This is most commonly used to add an
// html version in addition to a plain text version that was
// already added with SetBody.
func (email *Email) AddAlternative(contentType, body string) *Email {
func (email *Email) AddAlternative(contentType contentType, body string) *Email {
if email.Error != nil {
return email
}

email.parts = append(email.parts,
part{
contentType: contentType,
contentType: contentType.string(),
body: bytes.NewBufferString(body),
},
)
Expand Down Expand Up @@ -675,7 +684,7 @@ func dial(host string, port string, encryption encryption, config *tls.Config) (
}

if err != nil {
return nil, errors.New("Mail Error on dailing with encryption type " + encryption.String() + ": " + err.Error())
return nil, errors.New("Mail Error on dailing with encryption type " + encryption.string() + ": " + err.Error())
}

c, err := newClient(conn, host)
Expand Down
20 changes: 3 additions & 17 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ func TestSendMail(t *testing.T) {
AddTo("admin@example.com").
SetSubject("New Go Email")

email.SetBody("text/html", htmlBody)
email.AddAlternative("text/plain", "Hello Gophers!")
email.SetBody(TextHTML, htmlBody)
email.AddAlternative(TextPlain, "Hello Gophers!")

//Some additional options to send
email.SetSender("xhit@test.com")
Expand Down Expand Up @@ -149,7 +149,7 @@ func sendEmail(htmlBody string, to string, smtpClient *SMTPClient) error {

//Get from each mail
email.getFrom()
email.SetBody("text/html", htmlBody)
email.SetBody(TextHTML, htmlBody)

//Send with high priority
email.SetPriority(PriorityHigh)
Expand All @@ -160,20 +160,6 @@ func sendEmail(htmlBody string, to string, smtpClient *SMTPClient) error {
return err
}

func TestPriority(t *testing.T) {
str := PriorityLow.String()

if len(str) < 1 {
t.Error("Expected Low, returned empty string")
}

str = PriorityHigh.String()

if len(str) < 1 {
t.Error("Expected High, returned empty string")
}
}

//TestWithTLS using gmail port 587
func TestWithTLS(t *testing.T) {
client := NewSMTPClient()
Expand Down
4 changes: 3 additions & 1 deletion header.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// Package mail implements "Q" encoding as specified by RFC 2047.
// headers.go implements "Q" encoding as specified by RFC 2047.
//Modified from https://github.com/joegrasse/mime to use with Go Simple Mail

package mail

import (
Expand Down
4 changes: 2 additions & 2 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ func (msg *message) addBody(contentType string, body []byte) {

header := make(textproto.MIMEHeader)
header.Set("Content-Type", contentType+"; charset="+msg.charset)
header.Set("Content-Transfer-Encoding", msg.encoding.String())
header.Set("Content-Transfer-Encoding", msg.encoding.string())
msg.write(header, body, msg.encoding)
}

Expand All @@ -235,7 +235,7 @@ func (msg *message) addFiles(files []*file, inline bool) {
for _, file := range files {
header := make(textproto.MIMEHeader)
header.Set("Content-Type", file.mimeType+";\n \tname=\""+encodeHeader(escapeQuotes(file.filename), msg.charset, 6)+`"`)
header.Set("Content-Transfer-Encoding", encoding.String())
header.Set("Content-Transfer-Encoding", encoding.string())
if inline {
header.Set("Content-Disposition", "inline;\n \tfilename=\""+encodeHeader(escapeQuotes(file.filename), msg.charset, 10)+`"`)
header.Set("Content-ID", "<"+msg.getCID(file.filename)+">")
Expand Down

0 comments on commit 925a4f0

Please sign in to comment.