Skip to content

Commit

Permalink
I don't like Microsoft outlook can't be validated trough the normal w…
Browse files Browse the repository at this point in the history
…ay :)
  • Loading branch information
Siposattila committed May 26, 2024
1 parent 988a9e0 commit af07f3e
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 2 deletions.
106 changes: 106 additions & 0 deletions cmd/test/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package main

import (
"crypto/tls"
"errors"
"log"
"net"
"net/smtp"
"strings"
)

func main() {
log.Println(verifyEmail("gobulk2024@outlook.com"))
}

func verifyEmail(email string) uint8 {
domain := email[strings.LastIndex(email, "@")+1:]

mxRecords, err := net.LookupMX(domain)
if err != nil {
log.Println(err)

return 2
}

mxHost := mxRecords[0].Host
mxHost = "smtp-mail.outlook.com"

tlsconfig := &tls.Config{
ServerName: mxHost,
}

client, err := smtp.Dial(mxHost + ":25")
if err != nil {
log.Fatal(err)
}
defer client.Close()

//conn, err := net.Dial("tcp", mxHost+":25")
//if err != nil {
// log.Fatal(err)
//}
//defer conn.Close()

//client, err := smtp.NewClient(conn, mxHost)
//if err != nil {
// log.Fatal(err)
//}

// StartTLS to upgrade the connection to TLS
if err := client.StartTLS(tlsconfig); err != nil {
log.Fatal(err)
}

client.Hello("gobulk.com")
//auth := smtp.PlainAuth("", "gobulk2024@gmail.com", "xU!@'nWg85Ga-er", mxHost)
auth := LoginAuth("gobulk2024@outlook.com", "xU!@'nWg85Ga-er")
if err := client.Auth(auth); err != nil {
log.Fatal(err)
}

client.Mail("info@gobulk.com")
rcptErr := client.Rcpt(email)
vrfyErr := client.Verify(email)
client.Quit()

if rcptErr != nil {
log.Println(rcptErr)

return 2
}

if vrfyErr != nil {
log.Println(vrfyErr)

return 2
}

return 1
}

type loginAuth struct {
username, password string
}

func LoginAuth(username string, password string) smtp.Auth {
return &loginAuth{username, password}
}

func (a *loginAuth) Start(server *smtp.ServerInfo) (string, []byte, error) {
return "LOGIN", []byte{}, nil
}

func (a *loginAuth) Next(fromServer []byte, more bool) ([]byte, error) {
if more {
switch string(fromServer) {
case "Username:":
return []byte(a.username), nil
case "Password:":
return []byte(a.password), nil
default:
return nil, errors.New("Unknown from server")
}
}
return nil, nil
}
9 changes: 7 additions & 2 deletions internal/email/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/smtp"
"regexp"
"strings"
"time"

"github.com/Siposattila/gobulk/internal/interfaces"
"github.com/Siposattila/gobulk/internal/logger"
Expand All @@ -13,6 +14,10 @@ import (
func (e *Email) verifyEmail() uint8 {
domain := e.Email[strings.LastIndex(e.Email, "@")+1:]

if domain == "outlook.com" {
return interfaces.EMAIL_VALID
}

mxRecords, err := net.LookupMX(domain)
if err != nil {
logger.LogError(err)
Expand All @@ -22,14 +27,14 @@ func (e *Email) verifyEmail() uint8 {

mxHost := mxRecords[0].Host

client, err := smtp.Dial(mxHost + ":25")
connection, err := net.DialTimeout("tcp", mxHost+":25", 5*time.Second)
if err != nil {
logger.LogError(err)

return interfaces.EMAIL_INVALID
}
defer client.Close()

client, _ := smtp.NewClient(connection, mxHost)
client.Hello("gobulk.com")
client.Mail("info@gobulk.com")
rcptErr := client.Rcpt(e.Email)
Expand Down
9 changes: 9 additions & 0 deletions test/email_validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,12 @@ func TestValidatorOnFreemail(t *testing.T) {
t.Fatalf("e.Valid should be true on gobulk@fremail.hu")
}
}

func TestValidatorOnOutlook(t *testing.T) {
e := email.Email{Email: "gobulk2024@outlook.com"}
e.ValidateEmail()

if e.Valid == interfaces.EMAIL_INVALID {
t.Fatalf("e.Valid should be true on gobulk2024@outlook.com")
}
}

0 comments on commit af07f3e

Please sign in to comment.