浏览代码

Logic bug fixed
Implemented basic headerToBytes function

Jordan 11 年之前
父节点
当前提交
73ac30d14d
共有 2 个文件被更改,包括 21 次插入3 次删除
  1. 3 0
      README.md
  2. 18 3
      email.go

+ 3 - 0
README.md

@@ -44,3 +44,6 @@ e.Attach("test.txt")
 
 
 ### Documentation
 ### Documentation
 [http://godoc.org/github.com/jordan-wright/email](http://godoc.org/github.com/jordan-wright/email)
 [http://godoc.org/github.com/jordan-wright/email](http://godoc.org/github.com/jordan-wright/email)
+
+### Other Sources
+Sections inspired by the handy [gophermail](https://github.com/jpoehls/gophermail) project.

+ 18 - 3
email.go

@@ -75,7 +75,9 @@ func (e *Email) Bytes() ([]byte, error) {
 	//Leave out Bcc (only included in envelope headers)
 	//Leave out Bcc (only included in envelope headers)
 	//TODO: Support wrapping on 76 characters (ref: MIME RFC)
 	//TODO: Support wrapping on 76 characters (ref: MIME RFC)
 	e.Headers.Set("To", strings.Join(e.To, ","))
 	e.Headers.Set("To", strings.Join(e.To, ","))
-	e.Headers.Set("Cc", strings.Join(e.Cc, ","))
+	if e.Cc != nil {
+		e.Headers.Set("Cc", strings.Join(e.Cc, ","))
+	}
 	e.Headers.Set("From", e.From)
 	e.Headers.Set("From", e.From)
 	e.Headers.Set("Subject", e.Subject)
 	e.Headers.Set("Subject", e.Subject)
 	if len(e.ReadReceipt) != 0 {
 	if len(e.ReadReceipt) != 0 {
@@ -117,7 +119,6 @@ func (e *Email) Bytes() ([]byte, error) {
 	}
 	}
 	//Create attachment part, if necessary
 	//Create attachment part, if necessary
 	if e.Attachments != nil {
 	if e.Attachments != nil {
-
 	}
 	}
 	return buff.Bytes(), nil
 	return buff.Bytes(), nil
 }
 }
@@ -126,7 +127,7 @@ func (e *Email) Bytes() ([]byte, error) {
 //This function merges the To, Cc, and Bcc fields and calls the smtp.SendMail function using the Email.Bytes() output as the message
 //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 {
 func (e *Email) Send(addr string, a smtp.Auth) error {
 	//Check to make sure there is at least one recipient and one "From" address
 	//Check to make sure there is at least one recipient and one "From" address
-	if e.From == "" || (len(e.To) == 0 || len(e.Cc) == 0 || len(e.Bcc) == 0) {
+	if e.From == "" || (len(e.To) == 0 && len(e.Cc) == 0 && len(e.Bcc) == 0) {
 		return errors.New("Must specify at least one From address and one To address")
 		return errors.New("Must specify at least one From address and one To address")
 	}
 	}
 	// Merge the To, Cc, and Bcc fields
 	// Merge the To, Cc, and Bcc fields
@@ -149,6 +150,20 @@ func writeMIME(w io.Writer, t string) error {
 
 
 //headerToBytes enumerates the key and values in the header, and writes the results to the IO Writer
 //headerToBytes enumerates the key and values in the header, and writes the results to the IO Writer
 func headerToBytes(w io.Writer, t textproto.MIMEHeader) error {
 func headerToBytes(w io.Writer, t textproto.MIMEHeader) error {
+	for k, v := range t {
+		//Write the header key
+		_, err := fmt.Fprintf(w, "%s: ", k)
+		if err != nil {
+			return err
+		}
+		//Write each value in the header
+		for _, c := range v {
+			_, err := fmt.Fprintf(w, "%s\r\n", c)
+			if err != nil {
+				return err
+			}
+		}
+	}
 	return nil
 	return nil
 }
 }