example_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. package email_test
  2. import (
  3. "dashoo.cn/opms_libary/plugin/email"
  4. "fmt"
  5. "html/template"
  6. "io"
  7. "log"
  8. "time"
  9. )
  10. func Example() {
  11. m := email.NewMessage()
  12. m.SetHeader("From", "alex@example.com")
  13. m.SetHeader("To", "bob@example.com", "cora@example.com")
  14. m.SetAddressHeader("Cc", "dan@example.com", "Dan")
  15. m.SetHeader("Subject", "Hello!")
  16. m.SetBody("text/html", "Hello <b>Bob</b> and <i>Cora</i>!")
  17. m.Attach("/home/Alex/lolcat.jpg")
  18. // Send the email to Bob, Cora and Dan.
  19. if err := email.Client.DialAndSend(m); err != nil {
  20. panic(err)
  21. }
  22. }
  23. // A daemon that listens to a channel and sends all incoming messages.
  24. func Example_daemon() {
  25. ch := make(chan *email.Message)
  26. go func() {
  27. d := email.NewClient()
  28. var s email.SendCloser
  29. var err error
  30. open := false
  31. for {
  32. select {
  33. case m, ok := <-ch:
  34. if !ok {
  35. return
  36. }
  37. if !open {
  38. if s, err = d.Dial(); err != nil {
  39. panic(err)
  40. }
  41. open = true
  42. }
  43. if err := email.Send(s, m); err != nil {
  44. log.Print(err)
  45. }
  46. // Close the connection to the SMTP server if no email was sent in
  47. // the last 30 seconds.
  48. case <-time.After(30 * time.Second):
  49. if open {
  50. if err := s.Close(); err != nil {
  51. panic(err)
  52. }
  53. open = false
  54. }
  55. }
  56. }
  57. }()
  58. // Use the channel in your program to send emails.
  59. // Close the channel to stop the mail daemon.
  60. close(ch)
  61. }
  62. // Efficiently send a customized newsletter to a list of recipients.
  63. func Example_newsletter() {
  64. // The list of recipients.
  65. var list []struct {
  66. Name string
  67. Address string
  68. }
  69. d := email.NewClient()
  70. s, err := d.Dial()
  71. if err != nil {
  72. panic(err)
  73. }
  74. m := email.NewMessage()
  75. for _, r := range list {
  76. m.SetHeader("From", "no-reply@example.com")
  77. m.SetAddressHeader("To", r.Address, r.Name)
  78. m.SetHeader("Subject", "Newsletter #1")
  79. m.SetBody("text/html", fmt.Sprintf("Hello %s!", r.Name))
  80. if err := email.Send(s, m); err != nil {
  81. log.Printf("Could not send email to %q: %v", r.Address, err)
  82. }
  83. m.Reset()
  84. }
  85. }
  86. // Send an email using a local SMTP server.
  87. func Example_noAuth() {
  88. m := email.NewMessage()
  89. m.SetHeader("From", "from@example.com")
  90. m.SetHeader("To", "to@example.com")
  91. m.SetHeader("Subject", "Hello!")
  92. m.SetBody("text/plain", "Hello!")
  93. d := email.ClientImpl{Host: "localhost", Port: 587}
  94. if err := d.DialAndSend(m); err != nil {
  95. panic(err)
  96. }
  97. }
  98. // Send an email using an API or postfix.
  99. func Example_noSMTP() {
  100. m := email.NewMessage()
  101. m.SetHeader("From", "from@example.com")
  102. m.SetHeader("To", "to@example.com")
  103. m.SetHeader("Subject", "Hello!")
  104. m.SetBody("text/plain", "Hello!")
  105. s := email.SendFunc(func(from string, to []string, msg io.WriterTo) error {
  106. // Implements you email-sending function, for example by calling
  107. // an API, or running postfix, etc.
  108. fmt.Println("From:", from)
  109. fmt.Println("To:", to)
  110. return nil
  111. })
  112. if err := email.Send(s, m); err != nil {
  113. panic(err)
  114. }
  115. // Output:
  116. // From: from@example.com
  117. // To: [to@example.com]
  118. }
  119. var m *email.Message
  120. func ExampleSetCopyFunc() {
  121. m.Attach("foo.txt", email.SetCopyFunc(func(w io.Writer) error {
  122. _, err := w.Write([]byte("Content of foo.txt"))
  123. return err
  124. }))
  125. }
  126. func ExampleSetHeader() {
  127. h := map[string][]string{"Content-ID": {"<foo@bar.mail>"}}
  128. m.Attach("foo.jpg", email.SetHeader(h))
  129. }
  130. func ExampleRename() {
  131. m.Attach("/tmp/0000146.jpg", email.Rename("picture.jpg"))
  132. }
  133. func ExampleMessage_AddAlternative() {
  134. m.SetBody("text/plain", "Hello!")
  135. m.AddAlternative("text/html", "<p>Hello!</p>")
  136. }
  137. func ExampleMessage_AddAlternativeWriter() {
  138. t := template.Must(template.New("example").Parse("Hello {{.}}!"))
  139. m.AddAlternativeWriter("text/plain", func(w io.Writer) error {
  140. return t.Execute(w, "Bob")
  141. })
  142. }
  143. func ExampleMessage_Attach() {
  144. m.Attach("/tmp/image.jpg")
  145. }
  146. func ExampleMessage_Embed() {
  147. m.Embed("/tmp/image.jpg")
  148. m.SetBody("text/html", `<img src="cid:image.jpg" alt="My image" />`)
  149. }
  150. func ExampleMessage_FormatAddress() {
  151. m.SetHeader("To", m.FormatAddress("bob@example.com", "Bob"), m.FormatAddress("cora@example.com", "Cora"))
  152. }
  153. func ExampleMessage_FormatDate() {
  154. m.SetHeaders(map[string][]string{
  155. "X-Date": {m.FormatDate(time.Now())},
  156. })
  157. }
  158. func ExampleMessage_SetAddressHeader() {
  159. m.SetAddressHeader("To", "bob@example.com", "Bob")
  160. }
  161. func ExampleMessage_SetBody() {
  162. m.SetBody("text/plain", "Hello!")
  163. }
  164. func ExampleMessage_SetDateHeader() {
  165. m.SetDateHeader("X-Date", time.Now())
  166. }
  167. func ExampleMessage_SetHeader() {
  168. m.SetHeader("Subject", "Hello!")
  169. }
  170. func ExampleMessage_SetHeaders() {
  171. m.SetHeaders(map[string][]string{
  172. "From": {m.FormatAddress("alex@example.com", "Alex")},
  173. "To": {"bob@example.com", "cora@example.com"},
  174. "Subject": {"Hello"},
  175. })
  176. }
  177. func ExampleSetCharset() {
  178. m = email.NewMessage(email.SetCharset("ISO-8859-1"))
  179. }
  180. func ExampleSetEncoding() {
  181. m = email.NewMessage(email.SetEncoding(email.Base64))
  182. }
  183. func ExampleSetPartEncoding() {
  184. m.SetBody("text/plain", "Hello!", email.SetPartEncoding(email.Unencoded))
  185. }