example_test.go 5.0 KB

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