forked from BinodKafle/gomail
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
103 lines (86 loc) · 2.14 KB
/
main.go
File metadata and controls
103 lines (86 loc) · 2.14 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package main
import (
"log"
"os"
"github.com/joho/godotenv"
"github.com/BinodKafle/gomail/gomail"
)
func main() {
err := godotenv.Load()
if err != nil {
log.Fatalf("Error getting env, %v", err)
}
params := os.Args
paramsLength := len(params)
if paramsLength < 2 {
log.Println("Please add SMTP or OAUTH along with go run main.go command")
log.Println("Eg: go run main.go SMTP or go run main.go OAUTH")
os.Exit(1)
}
inputMethod := os.Args[1]
valid := IsValidInputMethod(inputMethod)
emailTo := os.Getenv("EMAIL_TO")
if valid {
data := struct {
ReceiverName string
SenderName string
}{
ReceiverName: "David Gilmour",
SenderName: "Binod Kafle",
}
if inputMethod == "SMTP" {
status, err := gomail.SendEmailSMTP([]string{emailTo}, data, "sample_template.txt")
if err != nil {
log.Println(err)
}
if status {
log.Println("Email sent successfully using SMTP")
}
}
if inputMethod == "OAUTH" {
gomail.OAuthGmailService()
status, err := gomail.SendEmailOAUTH2(emailTo, data, "sample_template.txt")
if err != nil {
log.Println(err)
}
if status {
log.Println("Email sent successfully using OAUTH")
}
}
if inputMethod == "LSR" {
gomail.OAuthGmailService()
status, err := gomail.SendEmail("TARGA-MEZZO", "https://rp.lasernavigation.it:6014", "Camera Recorder START", emailTo)
if err != nil {
log.Println(err)
}
if status {
log.Println("Email sent successfully using LSR")
}
}
if inputMethod == "LSRATT" {
gomail.OAuthGmailService()
status, err := gomail.SendEmailAttachment("TARGA-MEZZO", "https://rp.lasernavigation.it:6014", "Invasione GEOFENCE", emailTo, "/home/laser/gomail/", "2022_06_09_08_19_43.jpg")
if err != nil {
log.Println(err)
}
if status {
log.Println("Email sent successfully using LSRATT")
}
}
} else {
log.Println("Please add SMTP or OAUTH along with go run main.go command")
log.Println("Eg: go run main.go SMTP or go run main.go OAUTH")
os.Exit(1)
}
}
func IsValidInputMethod(method string) bool {
switch method {
case
"SMTP",
"OAUTH",
"LSR",
"LSRATT":
return true
}
return false
}