example_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. package mail
  2. import (
  3. "testing"
  4. "time"
  5. )
  6. //Some variables to connect and the body
  7. var (
  8. htmlBody = `<html>
  9. <head>
  10. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  11. <title>Hello Gophers!</title>
  12. </head>
  13. <body>
  14. <p>This is the <b>Go gopher</b>.</p>
  15. <p><img src="cid:Gopher.png" alt="Go gopher" /></p>
  16. <p>Image created by Renee French</p>
  17. </body>
  18. </html>`
  19. host = "localhost"
  20. port = 25
  21. username = "test@example.com"
  22. password = "santiago"
  23. encryptionType = EncryptionNone
  24. connectTimeout = 10 * time.Second
  25. sendTimeout = 10 * time.Second
  26. )
  27. //TestSendMailWithAttachment send a simple html email
  28. func TestSendMail(t *testing.T) {
  29. client := NewSMTPClient()
  30. //SMTP Client
  31. client.Host = host
  32. client.Port = port
  33. client.Username = username
  34. client.Password = password
  35. client.Encryption = encryptionType
  36. client.ConnectTimeout = connectTimeout
  37. client.SendTimeout = sendTimeout
  38. client.KeepAlive = false
  39. //Connect to client
  40. smtpClient, err := client.Connect()
  41. if err != nil {
  42. t.Error("Expected nil, got", err, "connecting to client")
  43. }
  44. //NOOP command, optional, used for avoid timeout when KeepAlive is true and you aren't sending mails.
  45. //Execute this command each 30 seconds is ideal for persistent connection
  46. err = smtpClient.Noop()
  47. if err != nil {
  48. t.Error("Expected nil, got", err, "noop to client")
  49. }
  50. //Create the email message
  51. email := NewMSG()
  52. email.SetFrom("From Example <test@example.com>").
  53. AddTo("admin@example.com").
  54. SetSubject("New Go Email")
  55. email.SetBody(TextHTML, htmlBody)
  56. email.AddAlternative(TextPlain, "Hello Gophers!")
  57. //Some additional options to send
  58. email.SetSender("xhit@test.com")
  59. email.SetReplyTo("replyto@reply.com")
  60. email.SetReturnPath("test@example.com")
  61. email.AddCc("cc@example1.com")
  62. email.AddBcc("bcccc@example2.com")
  63. //Add inline too!
  64. email.AddInline("C:/Users/sdelacruz/Pictures/Gopher.png")
  65. //Attach a file with path
  66. email.AddAttachment("C:/Users/sdelacruz/Pictures/Gopher.png")
  67. //Attach the file with a base64
  68. email.AddAttachmentBase64("base64string", "filename")
  69. //Set a different date in header email
  70. email.SetDate("2015-04-28 10:32:00 CDT")
  71. //Send with low priority
  72. email.SetPriority(PriorityLow)
  73. //Pass the client to the email message to send it
  74. err = email.Send(smtpClient)
  75. //Get first error
  76. email.GetError()
  77. if err != nil {
  78. t.Error("Expected nil, got", err, "sending email")
  79. }
  80. }
  81. //TestSendMultipleEmails send multiple emails in same connection
  82. func TestSendMultipleEmails(t *testing.T) {
  83. client := NewSMTPClient()
  84. //SMTP Client
  85. client.Host = host
  86. client.Port = port
  87. client.Username = username
  88. client.Password = password
  89. client.Encryption = encryptionType
  90. client.ConnectTimeout = connectTimeout
  91. client.SendTimeout = sendTimeout
  92. //For authentication you can use AuthPlain, AuthLogin or AuthCRAMMD5
  93. client.Authentication = AuthPlain
  94. //KeepAlive true because the connection need to be open for multiple emails
  95. //For avoid inactivity timeout, every 30 second you can send a NO OPERATION command to smtp client
  96. //use smtpClient.Client.Noop() after 30 second of inactivity in this example
  97. client.KeepAlive = true
  98. //Connect to client
  99. smtpClient, err := client.Connect()
  100. if err != nil {
  101. t.Error("Expected nil, got", err, "connecting to client")
  102. }
  103. toList := [3]string{"to1@example1.com", "to3@example2.com", "to4@example3.com"}
  104. for _, to := range toList {
  105. err = sendEmail(htmlBody, to, smtpClient)
  106. if err != nil {
  107. t.Error("Expected nil, got", err, "sending email")
  108. }
  109. }
  110. }
  111. func sendEmail(htmlBody string, to string, smtpClient *SMTPClient) error {
  112. //Create the email message
  113. email := NewMSG()
  114. email.SetFrom("From Example <from.email@example.com>").
  115. AddTo(to).
  116. SetSubject("New Go Email")
  117. //Get from each mail
  118. email.getFrom()
  119. email.SetBody(TextHTML, htmlBody)
  120. //Send with high priority
  121. email.SetPriority(PriorityHigh)
  122. //Pass the client to the email message to send it
  123. err := email.Send(smtpClient)
  124. return err
  125. }
  126. //TestWithTLS using gmail port 587
  127. func TestWithTLS(t *testing.T) {
  128. client := NewSMTPClient()
  129. //SMTP Client
  130. client.Host = "smtp.gmail.com"
  131. client.Port = 587
  132. client.Username = "aaa@gmail.com"
  133. client.Password = "asdfghh"
  134. client.Encryption = EncryptionTLS
  135. client.ConnectTimeout = 10 * time.Second
  136. client.SendTimeout = 10 * time.Second
  137. //KeepAlive is not settted because by default is false
  138. //Connect to client
  139. smtpClient, err := client.Connect()
  140. if err != nil {
  141. t.Error("Expected nil, got", err, "connecting to client")
  142. }
  143. err = sendEmail(htmlBody, "bbb@gmail.com", smtpClient)
  144. if err != nil {
  145. t.Error("Expected nil, got", err, "sending email")
  146. }
  147. }
  148. //TestWithTLS using gmail port 465
  149. func TestWithSSL(t *testing.T) {
  150. client := NewSMTPClient()
  151. //SMTP Client
  152. client.Host = "smtp.gmail.com"
  153. client.Port = 465
  154. client.Username = "aaa@gmail.com"
  155. client.Password = "asdfghh"
  156. client.Encryption = EncryptionSSL
  157. client.ConnectTimeout = 10 * time.Second
  158. client.SendTimeout = 10 * time.Second
  159. //KeepAlive is not settted because by default is false
  160. //Connect to client
  161. smtpClient, err := client.Connect()
  162. if err != nil {
  163. t.Error("Expected nil, got", err, "connecting to client")
  164. }
  165. err = sendEmail(htmlBody, "bbb@gmail.com", smtpClient)
  166. if err != nil {
  167. t.Error("Expected nil, got", err, "sending email")
  168. }
  169. }