Guest User

Untitled

a guest
May 11th, 2018
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.37 KB | None | 0 0
  1. func CheckMails(mails []string) []string {
  2. var existingMails []string
  3. fmt.Printf("!!!!!!!!!!!!!!STARTING!!!!!!!!!!!! %s nnn", mails[1])
  4. for i := 0; i < len(mails); i++ {
  5. err := validateHost(mails[i])
  6. if err != nil {
  7. fmt.Printf("Error validating host. %s", err)
  8. }
  9. smtpErr, ok := err.(checkmail.SmtpError)
  10. if ok {
  11. fmt.Printf("Code: %s, Msg: %s", smtpErr.Code(), smtpErr)
  12. if smtpErr.Code() == "dia" {
  13. break
  14. }
  15. } else {
  16. fmt.Println("Email exists")
  17. existingMails = append(existingMails, mails[i])
  18. }
  19. }
  20. fmt.Printf("!!!!!!!!!!!!!!ENDING!!!!!!!!!!!! %s nnn", mails[1])
  21. return existingMails
  22. }
  23.  
  24. func validateHost(email string) error {
  25. _, host := split(email)
  26. mx, err := net.LookupMX(host)
  27. if err != nil {
  28. fmt.Printf("Error, UnresolvableHost! %s", err)
  29. }
  30.  
  31. client, err := smtp.Dial(fmt.Sprintf("%s:%d", mx[0].Host, 25))
  32. // fmt.Println(client)
  33. defer client.Close()
  34. if err != nil {
  35. //fmt.Println(client)
  36. //log.Fatalln(err)
  37. fmt.Printf("SmtpError! %s n", err)
  38. }
  39.  
  40. t := time.AfterFunc(forceDisconnectAfter, func() { client.Close() })
  41. defer t.Stop()
  42.  
  43. // t := NewTimer(10, func() { client.Close() })
  44. // defer t.Stop()
  45.  
  46. err = client.Hello("checkmail.me")
  47. //err = client.Hello("gmail.com")
  48. // fmt.Println(client)
  49. if err != nil {
  50. //log.Fatalln(err)
  51. fmt.Printf("client.Hello SmtpError! %s n", err)
  52. }
  53.  
  54. err = client.Mail("lansome-cowboy@gmail.com")
  55. if err != nil {
  56. fmt.Printf("client.MailSmtpError! %s n", err)
  57. }
  58. err = client.Rcpt(email)
  59. if err != nil {
  60. fmt.Printf("client.Rcpt SmtpError! %s n", err)
  61. }
  62. return nil
  63. }
  64.  
  65. // CheckMailsWithExpectedInterval want to get expectected number of seconds which you are ready to wait while
  66. // the mail trys to be validated (this time is for ONE mail, slice could have a lot of mails to check)
  67. // and slice of mails which should be validated
  68. func CheckMailsWithExpectedInterval(expectedSec int, mails []string) (ok bool, existingMails []string) {
  69. done := make(chan struct{})
  70. t1 := time.Now()
  71. var eMails []string
  72. go func() {
  73. eMails = CheckMails(mails)
  74. close(done)
  75. }()
  76.  
  77. select {
  78. case <-done:
  79. if len(eMails) > 1 {
  80. ok = false
  81. } else {
  82. ok = true
  83. existingMails = eMails
  84. }
  85. case <-time.After(time.Duration(expectedSec) * time.Second):
  86. }
  87.  
  88. fmt.Printf("nTime since:")
  89. fmt.Println(time.Since(t1))
  90. return ok, existingMails
  91. }
  92.  
  93. func split(email string) (account, host string) {
  94. i := strings.LastIndexByte(email, '@')
  95. account = email[:i]
  96. host = email[i+1:]
  97. return
  98. }
  99.  
  100. const forceDisconnectAfter = time.Second * 10
  101.  
  102. email1 := []string{
  103. "andreasId@fromatob.com",
  104. "Andreas.Wolff@fromatob.com",
  105. "AndreasWolff@fromatob.com",
  106. "Wolff.Andreas@fromatob.com",
  107. "WolffAndreas@fromatob.com",
  108. "Andreas@fromatob.com,Wolff@fromatob.com",
  109. "A.Wolff@fromatob.com",
  110. "AWolff@fromatob.com",
  111. "Andreas.W@fromatob.com",
  112. "AndreasW@fromatob.com",
  113. "Wolff.A@fromatob.com",
  114. "WolffA@fromatob.com",
  115. "W.Andreas@fromatob.com",
  116. "WAndreas@fromatob.com",
  117. }
  118.  
  119. panic: runtime error: invalid memory address or nil pointer dereference
  120. [signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x84fefa]
  121. goroutine 260 [running]:
  122. net/smtp.(*Client).Close(0x0, 0x0, 0x0)
  123. /usr/lib/go-1.10/src/net/smtp/smtp.go:76 +0x2a
  124. panic(0x90a360, 0xb94ae0)
  125. /usr/lib/go-1.10/src/runtime/panic.go:502 +0x24a
  126. net/smtp.(*Client).Hello(0x0, 0x97d912, 0x9, 0x0, 0x0)
  127. /usr/lib/go-1.10/src/net/smtp/smtp.go:100 +0x78
  128. magictool/mailchecker.validateHost(0xc4204c7020, 0x23, 0x0, 0x0)
  129. /home/username/go/src/magictool/mailchecker/mailchecker.go:115 +0x749
  130. magictool/mailchecker.CheckMails(0xc4201c0000, 0xf, 0xf, 0x0, 0x0, 0x0)
  131. /home/username/go/src/magictool/mailchecker/mailchecker.go:20 +0x1d1
  132. magictool/mailchecker.CheckMailsWithExpectedInterval.func1(0xc4201c0000, 0xf, 0xf, 0xc42038f5c0, 0xc4203b7ec0)
  133. /home/username/go/src/magictool/mailchecker/mailchecker.go:49 +0x3f
  134. created by magictool/mailchecker.CheckMailsWithExpectedInterval
  135. /home/username/go/src/magictool/mailchecker/mailchecker.go:48 +0x12c
Add Comment
Please, Sign In to add comment