client_mock.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package mocks
  2. import (
  3. "fmt"
  4. "math/rand"
  5. "net/smtp"
  6. "time"
  7. )
  8. func lastWords(message string, err error) {
  9. fmt.Println(message, err.Error())
  10. return
  11. // panic(err)
  12. }
  13. type Client struct {
  14. Helo string
  15. EmailAddress string
  16. }
  17. func (c *Client) SendMail(to, url string) {
  18. // fmt.Printf("Sending mail")
  19. sc, err := smtp.Dial(url)
  20. if err != nil {
  21. lastWords("Dial ", err)
  22. }
  23. defer sc.Close()
  24. // Introduce some artificial delay
  25. time.Sleep(time.Millisecond * (time.Duration(rand.Int() % 50)))
  26. if err = sc.Hello(c.Helo); err != nil {
  27. lastWords("Hello ", err)
  28. }
  29. if err = sc.Mail(c.EmailAddress); err != nil {
  30. lastWords("Mail ", err)
  31. }
  32. if err = sc.Rcpt(to); err != nil {
  33. lastWords("Rcpt ", err)
  34. }
  35. // Introduce some artificial delay
  36. time.Sleep(time.Millisecond * (time.Duration(rand.Int() % 50)))
  37. wr, err := sc.Data()
  38. if err != nil {
  39. lastWords("Data ", err)
  40. }
  41. defer wr.Close()
  42. msg := fmt.Sprint("Subject: something\n")
  43. msg += "From: " + c.EmailAddress + "\n"
  44. msg += "To: " + to + "\n"
  45. msg += "\n\n"
  46. msg += "hello\n"
  47. _, err = fmt.Fprint(wr, msg)
  48. if err != nil {
  49. lastWords("Send ", err)
  50. }
  51. // Introduce some artificial delay
  52. time.Sleep(time.Millisecond * (time.Duration(rand.Int() % 50)))
  53. err = sc.Quit()
  54. if err != nil {
  55. lastWords("Quit ", err)
  56. }
  57. }