-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
74 lines (57 loc) · 1.56 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main
import (
"crypto/rand"
"crypto/tls"
"fmt"
"log"
"os"
"github.com/joho/godotenv"
gomail "gopkg.in/mail.v2"
)
func genCaptchaCode() (string, error) {
codes := make([]byte, 6)
if _, err := rand.Read(codes); err != nil {
return "", err
}
for i := 0; i < 6; i++ {
codes[i] = uint8(48 + (codes[i] % 10))
}
return string(codes), nil
}
func main() {
//initializing env variables
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
// get the email from the user :
fmt.Println("Enter The Email Address : ")
var Emmail string
fmt.Scan(&Emmail)
m := gomail.NewMessage()
// Set E-Mail sender
m.SetHeader("From", Emmail)
// Set E-Mail receivers
m.SetHeader("To", os.Getenv("RECIEVEREMAIL"))
// Set E-Mail subject
m.SetHeader("Subject", "OTP to verify your Gmail")
//otp generation
onetimepassword, err := genCaptchaCode()
if err != nil {
fmt.Println(err)
}
// Set E-Mail body. You can set plain text or html with text/html
m.SetBody("text/plain", onetimepassword+" is your OTP to register to our site. Thank you registering to our site. Happy Shopping :)")
// Settings for SMTP server
d := gomail.NewDialer("smtp.gmail.com", 587, os.Getenv("SENDEREMAIL"), os.Getenv("PASSWORD"))
// This is only needed when SSL/TLS certificate is not valid on server.
// In production this should be set to false.
d.TLSConfig = &tls.Config{InsecureSkipVerify: true}
// Now send E-Mail
if err := d.DialAndSend(m); err != nil {
fmt.Println(err)
panic(err)
} else {
fmt.Println("OTP has been sent successfully")
}
}