email.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. const (
  21. MAX_LINE_LENGTH = 76 //The maximum line length per RFC 2045
  22. )
  23. //Email is the type used for email messages
  24. type Email struct {
  25. From string
  26. To []string
  27. Bcc []string
  28. Cc []string
  29. Subject string
  30. Text string //Plaintext message (optional)
  31. Html string //Html message (optional)
  32. Headers textproto.MIMEHeader
  33. Attachments map[string]*Attachment
  34. ReadReceipt []string
  35. }
  36. //NewEmail creates an Email, and returns the pointer to it.
  37. func NewEmail() *Email {
  38. return &Email{Attachments: make(map[string]*Attachment), Headers: textproto.MIMEHeader{}}
  39. }
  40. //Attach is used to attach a file to the email.
  41. //It attempts to open the file referenced by filename and, if successful, creates an Attachment.
  42. //This Attachment is then appended to the slice of Email.Attachments.
  43. //The function will then return the Attachment for reference, as well as nil for the error, if successful.
  44. func (e *Email) Attach(filename string) (a *Attachment, err error) {
  45. //Check if the file exists, return any error
  46. if _, err := os.Stat(filename); os.IsNotExist(err) {
  47. return nil, err
  48. }
  49. //Read the file, and set the appropriate headers
  50. buffer, err := ioutil.ReadFile(filename)
  51. if err != nil {
  52. return nil, err
  53. }
  54. e.Attachments[filename] = &Attachment{
  55. Filename: filename,
  56. Header: textproto.MIMEHeader{},
  57. Content: buffer}
  58. at := e.Attachments[filename]
  59. //Get the Content-Type to be used in the MIMEHeader
  60. ct := mime.TypeByExtension(filepath.Ext(filename))
  61. if ct != "" {
  62. at.Header.Set("Content-Type", ct)
  63. } else {
  64. //If the Content-Type is blank, set the Content-Type to "application/octet-stream"
  65. at.Header.Set("Content-Type", "application/octet-stream")
  66. }
  67. at.Header.Set("Content-Disposition", fmt.Sprintf("attachment;\r\n filename=\"%s\"", filename))
  68. at.Header.Set("Content-Transfer-Encoding", "base64")
  69. return e.Attachments[filename], nil
  70. }
  71. //Bytes converts the Email object to a []byte representation, including all needed MIMEHeaders, boundaries, etc.
  72. func (e *Email) Bytes() ([]byte, error) {
  73. buff := &bytes.Buffer{}
  74. w := multipart.NewWriter(buff)
  75. //Set the appropriate headers (overwriting any conflicts)
  76. //Leave out Bcc (only included in envelope headers)
  77. e.Headers.Set("To", strings.Join(e.To, ","))
  78. if e.Cc != nil {
  79. e.Headers.Set("Cc", strings.Join(e.Cc, ","))
  80. }
  81. e.Headers.Set("From", e.From)
  82. e.Headers.Set("Subject", e.Subject)
  83. if len(e.ReadReceipt) != 0 {
  84. e.Headers.Set("Disposition-Notification-To", strings.Join(e.ReadReceipt, ","))
  85. }
  86. e.Headers.Set("MIME-Version", "1.0")
  87. e.Headers.Set("Content-Type", fmt.Sprintf("multipart/mixed;\r\n boundary=%s\r\n", w.Boundary()))
  88. //Write the envelope headers (including any custom headers)
  89. if err := headerToBytes(buff, e.Headers); err != nil {
  90. }
  91. //Start the multipart/mixed part
  92. fmt.Fprintf(buff, "--%s\r\n", w.Boundary())
  93. header := textproto.MIMEHeader{}
  94. //Check to see if there is a Text or HTML field
  95. if e.Text != "" || e.Html != "" {
  96. subWriter := multipart.NewWriter(buff)
  97. //Create the multipart alternative part
  98. header.Set("Content-Type", fmt.Sprintf("multipart/alternative;\r\n boundary=%s\r\n", subWriter.Boundary()))
  99. //Write the header
  100. if err := headerToBytes(buff, header); err != nil {
  101. }
  102. //Create the body sections
  103. if e.Text != "" {
  104. header.Set("Content-Type", fmt.Sprintf("text/plain; charset=UTF-8"))
  105. header.Set("Content-Transfer-Encoding", "quoted-printable")
  106. if _, err := subWriter.CreatePart(header); err != nil {
  107. return nil, err
  108. }
  109. // Write the text
  110. if err := quotePrintEncode(buff, e.Text); err != nil {
  111. return nil, err
  112. }
  113. }
  114. if e.Html != "" {
  115. header.Set("Content-Type", fmt.Sprintf("text/html; charset=UTF-8"))
  116. header.Set("Content-Transfer-Encoding", "quoted-printable")
  117. if _, err := subWriter.CreatePart(header); err != nil {
  118. return nil,err
  119. }
  120. // Write the text
  121. if err := quotePrintEncode(buff, e.Html); err != nil {
  122. return nil, err
  123. }
  124. }
  125. if err := subWriter.Close(); err != nil {
  126. return nil,err
  127. }
  128. }
  129. //Create attachment part, if necessary
  130. if e.Attachments != nil {
  131. for _, a := range e.Attachments {
  132. ap, err := w.CreatePart(a.Header)
  133. if err != nil {
  134. return nil, err
  135. }
  136. //Write the base64Wrapped content to the part
  137. base64Wrap(ap, a.Content)
  138. }
  139. }
  140. if err := w.Close(); err != nil {
  141. return nil,err
  142. }
  143. return buff.Bytes(), nil
  144. }
  145. //Send an email using the given host and SMTP auth (optional), returns any error thrown by smtp.SendMail
  146. //This function merges the To, Cc, and Bcc fields and calls the smtp.SendMail function using the Email.Bytes() output as the message
  147. func (e *Email) Send(addr string, a smtp.Auth) error {
  148. //Check to make sure there is at least one recipient and one "From" address
  149. if e.From == "" || (len(e.To) == 0 && len(e.Cc) == 0 && len(e.Bcc) == 0) {
  150. return errors.New("Must specify at least one From address and one To address")
  151. }
  152. // Merge the To, Cc, and Bcc fields
  153. to := append(append(e.To, e.Cc...), e.Bcc...)
  154. from, err := mail.ParseAddress(e.From)
  155. if err != nil {
  156. return err
  157. }
  158. raw, err := e.Bytes()
  159. if err != nil {
  160. return err
  161. }
  162. return smtp.SendMail(addr, a, from.Address, to, raw)
  163. }
  164. //Attachment is a struct representing an email attachment.
  165. //Based on the mime/multipart.FileHeader struct, Attachment contains the name, MIMEHeader, and content of the attachment in question
  166. type Attachment struct {
  167. Filename string
  168. Header textproto.MIMEHeader
  169. Content []byte
  170. }
  171. //quotePrintEncode writes the quoted-printable text to the IO Writer (according to RFC 2045)
  172. func quotePrintEncode(w io.Writer, s string) error {
  173. mc := 0
  174. for _, c := range s {
  175. // Handle the soft break for the EOL, if needed
  176. if mc == 75 || (!isPrintable(c) && mc+len(fmt.Sprintf("%s%X", "=", c)) > 75) {
  177. if _, err := fmt.Fprintf(w, "%s", "=\r\n"); err != nil {
  178. return err
  179. }
  180. mc = 0
  181. }
  182. //append the appropriate character
  183. if isPrintable(c) {
  184. //Printable character
  185. if _, err := fmt.Fprintf(w, "%s", string(c)); err != nil {
  186. return err
  187. }
  188. // Reset the counter if we wrote a newline
  189. if c == '\n' {
  190. mc = 0
  191. }
  192. mc++
  193. continue
  194. } else {
  195. //non-printable.. encode it (TODO)
  196. es := fmt.Sprintf("%s%X", "=", c)
  197. if _, err := fmt.Fprintf(w, "%s", es); err != nil {
  198. return err
  199. }
  200. //todo - increment correctly
  201. mc += len(es)
  202. }
  203. }
  204. return nil
  205. }
  206. //isPrintable returns true if the rune given is "printable" according to RFC 2045, false otherwise
  207. func isPrintable(c rune) bool {
  208. return (c >= '!' && c <= '<') || (c >= '>' && c <= '~') || (c == ' ' || c == '\n' || c == '\t')
  209. }
  210. //base64Wrap encodeds the attachment content, and wraps it according to RFC 2045 standards (every 76 chars)
  211. //The output is then written to the specified io.Writer
  212. func base64Wrap(w io.Writer, b []byte) {
  213. encoded := base64.StdEncoding.EncodeToString(b)
  214. for i := 0; i < len(encoded); i += 76 {
  215. //Do we need to print 76 characters, or the rest of the string?
  216. if len(encoded)-i < 76 {
  217. fmt.Fprintf(w, "%s\r\n", encoded[i:])
  218. } else {
  219. fmt.Fprintf(w, "%s\r\n", encoded[i:i+76])
  220. }
  221. }
  222. }
  223. //headerToBytes enumerates the key and values in the header, and writes the results to the IO Writer
  224. func headerToBytes(w io.Writer, t textproto.MIMEHeader) error {
  225. for k, v := range t {
  226. //Write the header key
  227. _, err := fmt.Fprintf(w, "%s:", k)
  228. if err != nil {
  229. return err
  230. }
  231. //Write each value in the header
  232. for _, c := range v {
  233. _, err := fmt.Fprintf(w, " %s\r\n", c)
  234. if err != nil {
  235. return err
  236. }
  237. }
  238. }
  239. return nil
  240. }