├── .env ├── go.mod ├── go.sum └── main.go /.env: -------------------------------------------------------------------------------- 1 | EMAIL="footballestore@gmail.com" 2 | PASSWORD="clgfjplxzbfnkcrn" -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/afthab/gmailauthentication 2 | 3 | go 1.19 4 | 5 | require ( 6 | github.com/joho/godotenv v1.4.0 // indirect 7 | gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect 8 | gopkg.in/mail.v2 v2.3.1 // indirect 9 | ) 10 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg= 2 | github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= 3 | gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc h1:2gGKlE2+asNV9m7xrywl36YYNnBG5ZQ0r/BOOxqPpmk= 4 | gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc/go.mod h1:m7x9LTH6d71AHyAX77c9yqWCCa3UKHcVEj9y7hAtKDk= 5 | gopkg.in/mail.v2 v2.3.1 h1:WYFn/oANrAGP2C0dcV6/pbkPzv8yGzqTjPmTeO7qoXk= 6 | gopkg.in/mail.v2 v2.3.1/go.mod h1:htwXN1Qh09vZJ1NVKxQqHPBaCBbzKhp5GzuJEA4VJWw= 7 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "net/smtp" 6 | "os" 7 | 8 | "github.com/joho/godotenv" 9 | ) 10 | 11 | func main() { 12 | 13 | loaderror := godotenv.Load() 14 | if loaderror != nil { 15 | fmt.Println(loaderror) 16 | } 17 | 18 | // Sender data. 19 | 20 | //EMAIL and PASSWORD has been saved in the ENV file for the enhanced security 21 | //And the password of the email should be the app password. 22 | 23 | from := os.Getenv("EMAIL") //insert your from email here instead of os.Getenv("EMAIL") 24 | password := os.Getenv("PASSWORD") //insert your from email password here instead of os.Getenv("EMAIL") 25 | 26 | // Receiver email address. 27 | to := []string{ 28 | "afthab606@gmail.com", 29 | } 30 | 31 | // smtp server configuration. 32 | smtpHost := "smtp.gmail.com" 33 | smtpPort := "587" 34 | 35 | // Message. 36 | message := []byte("Enter your massage here") 37 | 38 | // Authentication. 39 | auth := smtp.PlainAuth("", from, password, smtpHost) 40 | 41 | // Sending email. 42 | err := smtp.SendMail(smtpHost+":"+smtpPort, auth, from, to, message) 43 | if err != nil { 44 | fmt.Println(err) 45 | return 46 | } 47 | fmt.Println("Email Sent Successfully!") 48 | } 49 | --------------------------------------------------------------------------------