email.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. //Package email is designed to provide an "email interface for humans."
  2. //Designed to be robust and flexible, the email package aims to make sending email easy without getting in the way.
  3. package email
  4. import (
  5. "bytes"
  6. "encoding/base64"
  7. "errors"
  8. "fmt"
  9. "io"
  10. "io/ioutil"
  11. "mime"
  12. "mime/multipart"
  13. "net/mail"
  14. "net/smtp"
  15. "net/textproto"
  16. "os"
  17. "path/filepath"
  18. "strings"
  19. )
  20. //Email is the type used for email messages
  21. type Email struct {
  22. From string
  23. To []string
  24. Bcc []string
  25. Cc []string
  26. Subject string
  27. Text string //Plaintext message (optional)
  28. Html string //Html message (optional)
  29. Headers textproto.MIMEHeader
  30. Attachments map[string]*Attachment
  31. ReadReceipt []string
  32. }
  33. //NewEmail creates an Email, and returns the pointer to it.
  34. func NewEmail() *Email {
  35. return &Email{Attachments: make(map[string]*Attachment), Headers: textproto.MIMEHeader{}}
  36. }
  37. //Attach is used to attach a file to the email.
  38. //It attempts to open the file referenced by filename and, if successful, creates an Attachment.
  39. //This Attachment is then appended to the slice of Email.Attachments.
  40. //The function will then return the Attachment for reference, as well as nil for the error, if successful.
  41. func (e *Email) Attach(filename string) (a *Attachment, err error) {
  42. //Check if the file exists, return any error
  43. if _, err := os.Stat(filename); os.IsNotExist(err) {
  44. return nil, err
  45. }
  46. //Read the file, and set the appropriate headers
  47. buffer, _ := ioutil.ReadFile(filename)
  48. e.Attachments[filename] = &Attachment{
  49. Filename: filename,
  50. Header: textproto.MIMEHeader{},
  51. Content: buffer}
  52. at := e.Attachments[filename]
  53. //Get the Content-Type to be used in the MIMEHeader
  54. ct := mime.TypeByExtension(filepath.Ext(filename))
  55. if ct != "" {
  56. at.Header.Set("Content-Type", ct)
  57. } else {
  58. //If the Content-Type is blank, set the Content-Type to "application/octet-stream"
  59. at.Header.Set("Content-Type", "application/octet-stream")
  60. }
  61. at.Header.Set("Content-Disposition", fmt.Sprintf("attachment;\r\n filename=\"%s\"", filename))
  62. at.Header.Set("Content-Transfer-Encoding", "base64")
  63. return e.Attachments[filename], nil
  64. }
  65. //Bytes converts the Email object to a []byte representation, including all needed MIMEHeaders, boundaries, etc.
  66. func (e *Email) Bytes() ([]byte, error) {
  67. buff := &bytes.Buffer{}
  68. w := multipart.NewWriter(buff)
  69. //Set the appropriate headers (overwriting any conflicts)
  70. //Leave out Bcc (only included in envelope headers)
  71. //TODO: Support wrapping on 76 characters (ref: MIME RFC)
  72. e.Headers.Set("To", strings.Join(e.To, ","))
  73. if e.Cc != nil {
  74. e.Headers.Set("Cc", strings.Join(e.Cc, ","))
  75. }
  76. e.Headers.Set("From", e.From)
  77. e.Headers.Set("Subject", e.Subject)
  78. if len(e.ReadReceipt) != 0 {
  79. e.Headers.Set("Disposition-Notification-To", strings.Join(e.ReadReceipt, ","))
  80. }
  81. e.Headers.Set("MIME-Version", "1.0")
  82. e.Headers.Set("Content-Type", fmt.Sprintf("multipart/mixed;\r\n boundary=%s\r\n", w.Boundary()))
  83. //Write the envelope headers (including any custom headers)
  84. if err := headerToBytes(buff, e.Headers); err != nil {
  85. }
  86. //Start the multipart/mixed part
  87. fmt.Fprintf(buff, "--%s\r\n", w.Boundary())
  88. header := textproto.MIMEHeader{}
  89. //Check to see if there is a Text or HTML field
  90. if e.Text != "" || e.Html != "" {
  91. subWriter := multipart.NewWriter(buff)
  92. //Create the multipart alternative part
  93. header.Set("Content-Type", fmt.Sprintf("multipart/alternative;\r\n boundary=%s\r\n", subWriter.Boundary()))
  94. //Write the header
  95. if err := headerToBytes(buff, header); err != nil {
  96. }
  97. //Create the body sections
  98. if e.Text != "" {
  99. header.Set("Content-Type", fmt.Sprintf("text/plain; charset=UTF-8"))
  100. part, err := subWriter.CreatePart(header)
  101. if err != nil {
  102. }
  103. // Write the text
  104. if err := writeMIME(part, e.Text); err != nil {
  105. }
  106. }
  107. if e.Html != "" {
  108. header.Set("Content-Type", fmt.Sprintf("text/html; charset=UTF-8"))
  109. subWriter.CreatePart(header)
  110. // Write the text
  111. if err := writeMIME(buff, e.Html); err != nil {
  112. }
  113. }
  114. subWriter.Close()
  115. }
  116. //Create attachment part, if necessary
  117. if e.Attachments != nil {
  118. for _, a := range e.Attachments {
  119. ap, err := w.CreatePart(a.Header)
  120. if err != nil {
  121. return nil, err
  122. }
  123. //Write the base64Wrapped content to the part
  124. base64Wrap(ap, a.Content)
  125. }
  126. }
  127. w.Close()
  128. return buff.Bytes(), nil
  129. }
  130. //Send an email using the given host and SMTP auth (optional), returns any error thrown by smtp.SendMail
  131. //This function merges the To, Cc, and Bcc fields and calls the smtp.SendMail function using the Email.Bytes() output as the message
  132. func (e *Email) Send(addr string, a smtp.Auth) error {
  133. //Check to make sure there is at least one recipient and one "From" address
  134. if e.From == "" || (len(e.To) == 0 && len(e.Cc) == 0 && len(e.Bcc) == 0) {
  135. return errors.New("Must specify at least one From address and one To address")
  136. }
  137. // Merge the To, Cc, and Bcc fields
  138. to := append(append(e.To, e.Cc...), e.Bcc...)
  139. from, err := mail.ParseAddress(e.From)
  140. if err != nil {
  141. return err
  142. }
  143. raw, err := e.Bytes()
  144. if err != nil {
  145. return err
  146. }
  147. return smtp.SendMail(addr, a, from.Address, to, raw)
  148. }
  149. //writeMIME writes the quoted-printable text to the IO Writer
  150. func writeMIME(w io.Writer, s string) error {
  151. // Basic rules (comments to be removed once this function is fully implemented)
  152. // * If character is printable, it can be represented AS IS
  153. // * Lines must have a max of 76 characters
  154. // * Lines must not end with whitespace
  155. // - Rather, append a soft break (=) to the end of the line after the space for preservation
  156. // *
  157. _, err := fmt.Fprintf(w, "%s\r\n", s)
  158. for _, c := range s {
  159. // Check if we can just print the character
  160. if (c >= '!' && c < '=') || (c >= '>' && c < '~') {
  161. return nil
  162. }
  163. }
  164. if err != nil {
  165. return err
  166. }
  167. return nil
  168. }
  169. //base64Wrap encodeds the attachment content, and wraps it according to RFC 2045 standards (every 76 chars)
  170. //The output is then written to the specified io.Writer
  171. func base64Wrap(w io.Writer, b []byte) {
  172. encoded := base64.StdEncoding.EncodeToString(b)
  173. for i := 0; i < len(encoded); i += 76 {
  174. //Do we need to print 76 characters, or the rest of the string?
  175. if len(encoded)-i < 76 {
  176. fmt.Fprintf(w, "%s\r\n", encoded[i:])
  177. } else {
  178. fmt.Fprintf(w, "%s\r\n", encoded[i:i+76])
  179. }
  180. }
  181. }
  182. //headerToBytes enumerates the key and values in the header, and writes the results to the IO Writer
  183. func headerToBytes(w io.Writer, t textproto.MIMEHeader) error {
  184. for k, v := range t {
  185. //Write the header key
  186. _, err := fmt.Fprintf(w, "%s: ", k)
  187. if err != nil {
  188. return err
  189. }
  190. //Write each value in the header
  191. for _, c := range v {
  192. _, err := fmt.Fprintf(w, "%s\r\n", c)
  193. if err != nil {
  194. return err
  195. }
  196. }
  197. }
  198. return nil
  199. }
  200. //Attachment is a struct representing an email attachment.
  201. //Based on the mime/multipart.FileHeader struct, Attachment contains the name, MIMEHeader, and content of the attachment in question
  202. type Attachment struct {
  203. Filename string
  204. Header textproto.MIMEHeader
  205. Content []byte
  206. }