Browse Source

README: Adjusted examples
email.go: Added first validity check on Send() function
email_test.go: Added examples

Jordan 11 năm trước cách đây
mục cha
commit
6c67352633
3 tập tin đã thay đổi với 32 bổ sung10 xóa
  1. 9 9
      README.md
  2. 5 1
      email.go
  3. 18 0
      email_test.go

+ 9 - 9
README.md

@@ -26,20 +26,20 @@ Plans for the ```email``` package currently include supporting:
 #### Sending email using Gmail
 ```
 e := NewEmail()
-	e.From = "Jordan Wright <test@gmail.com>"
-	e.To = []string{"test@example.com"}
-	e.Bcc = []string{"test_bcc@example.com"}
-	e.Cc = []string{"test_cc@example.com"}
-	e.Subject = "Awesome Subject"
-	e.Text = "Text Body is, of course, supported!"
-	e.Html = "<h1>Fancy Html is supported, too!</h1>"
-	e.Send("smtp.gmail.com:587", smtp.PlainAuth("", e.From, "password123", "smtp.gmail.com"))
+e.From = "Jordan Wright <test@gmail.com>"
+e.To = []string{"test@example.com"}
+e.Bcc = []string{"test_bcc@example.com"}
+e.Cc = []string{"test_cc@example.com"}
+e.Subject = "Awesome Subject"
+e.Text = "Text Body is, of course, supported!"
+e.Html = "<h1>Fancy Html is supported, too!</h1>"
+e.Send("smtp.gmail.com:587", smtp.PlainAuth("", e.From, "password123", "smtp.gmail.com"))
 ```
 
 #### Attaching a File
 ```
 e := NewEmail()
-e.Attach('test.txt')
+e.Attach("test.txt")
 ```
 
 ### Documentation

+ 5 - 1
email.go

@@ -4,6 +4,7 @@ package email
 
 import (
 	"bytes"
+	"errors"
 	"fmt"
 	"io/ioutil"
 	"log"
@@ -75,13 +76,16 @@ func (e *Email) Bytes() []byte {
 	e.Headers.Set("From", e.From)
 	e.Headers.Set("Subject", e.Subject)
 	//Write the envelope headers (including any custom headers)
-
 	return buff.Bytes()
 }
 
 //Send an email using the given host and SMTP auth (optional), returns any error thrown by smtp.SendMail
 //This function merges the To, Cc, and Bcc fields and calls the smtp.SendMail function using the Email.Bytes() output as the message
 func (e *Email) Send(addr string, a smtp.Auth) error {
+	//Check to make sure there is at least one recipient and one "From" address
+	if e.From == "" || len(e.To) == 0 {
+		return errors.New("Must specify at least one From address and one To address")
+	}
 	// Merge the To, Cc, and Bcc fields
 	to := append(append(e.To, e.Cc...), e.Bcc...)
 	from, err := mail.ParseAddress(e.From)

+ 18 - 0
email_test.go

@@ -1,6 +1,7 @@
 package email
 
 import (
+	"net/smtp"
 	"testing"
 )
 
@@ -14,3 +15,20 @@ func TestEmail(*testing.T) {
 	e.Text = "Text Body is, of course, supported!"
 	e.Html = "<h1>Fancy Html is supported, too!</h1>"
 }
+
+func ExampleGmail() {
+	e := NewEmail()
+	e.From = "Jordan Wright <test@gmail.com>"
+	e.To = []string{"test@example.com"}
+	e.Bcc = []string{"test_bcc@example.com"}
+	e.Cc = []string{"test_cc@example.com"}
+	e.Subject = "Awesome Subject"
+	e.Text = "Text Body is, of course, supported!"
+	e.Html = "<h1>Fancy Html is supported, too!</h1>"
+	e.Send("smtp.gmail.com:587", smtp.PlainAuth("", e.From, "password123", "smtp.gmail.com"))
+}
+
+func ExampleAttach() {
+	e := NewEmail()
+	e.Attach("test.txt")
+}