/
app.go
165 lines (136 loc) · 4.6 KB
/
app.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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package main
import (
"bufio"
"fmt"
"github.com/kesselborn/go-getopt"
"github.com/linuzilla/go-gloria-mailer/config"
"github.com/linuzilla/go-gloria-mailer/excel"
"github.com/linuzilla/go-gloria-mailer/flags"
"github.com/linuzilla/go-gloria-mailer/mailer"
"github.com/linuzilla/go-gloria-mailer/merger"
mime_composer "github.com/linuzilla/go-gloria-mailer/mime-composer"
"github.com/linuzilla/go-gloria-mailer/mimemail"
"log"
"os"
"strconv"
"strings"
)
const VERSION = "gloria-mailer version 0.0.1"
const ConfigFileEnv = "MAILER_CONFIG"
const DefaultConfigFile = "settings.conf"
func main() {
optionDefinition := getopt.Options{
Description: VERSION,
Definitions: getopt.Definitions{
{"debug|d", "debug mode: on", getopt.Optional | getopt.Flag | getopt.NoEnvHelp, false},
{"no-debug", "debug mode: off", getopt.Optional | getopt.Flag | getopt.NoEnvHelp, false},
{"send-email", "send email: on", getopt.Optional | getopt.Flag | getopt.NoEnvHelp, false},
{"no-send-email", "send email: off", getopt.Optional | getopt.Flag | getopt.NoEnvHelp, false},
{"excel", "excel file", getopt.Optional | getopt.NoEnvHelp, ``},
{"template", "template file", getopt.Optional | getopt.NoEnvHelp, ``},
{"verbose|v", "verbose mode", getopt.Optional | getopt.Flag | getopt.NoEnvHelp, false},
{"config|c|" + ConfigFileEnv, "config file", getopt.IsConfigFile | getopt.ExampleIsDefault, DefaultConfigFile},
},
}
options, _, _, e := optionDefinition.ParseCommandLine()
help, wantsHelp := options["help"]
exitCode := 0
if e != nil || wantsHelp {
switch {
case wantsHelp && help.String == "usage":
fmt.Print(optionDefinition.Usage())
case wantsHelp && help.String == "help":
fmt.Print(optionDefinition.Help())
default:
fmt.Println("**** Error: ", e.Error(), "\n", optionDefinition.Help())
exitCode = e.ErrorCode
}
} else {
start(options["config"].String, options)
}
os.Exit(exitCode)
}
func start(configurationFile string, options map[string]getopt.OptionValue) {
settings := config.New(configurationFile)
flags.Initialize(settings, options)
fmt.Println(VERSION)
fmt.Printf("\nExcel: [ %s ], Email Template: [ %s ]\n", flags.ExcelFile, flags.TemplateFile)
fmt.Printf("Debugging: %s, Send REAL email: %s\n", strconv.FormatBool(flags.Debug), strconv.FormatBool(flags.SendEmail))
fmt.Printf("SMTP Server: %s, port: %d, authentication: %s\n\n", settings.Smtp.Host, settings.Smtp.Port, strconv.FormatBool(settings.Smtp.Auth))
reader, err := excel.New(settings.Excel.File)
if err != nil {
log.Fatal(err)
}
if flags.Verbose {
fmt.Printf("Excel file: [ %s ], SheetName: [ %s ]\n", flags.ExcelFile, reader.SheetName())
for _, fieldName := range reader.Fields() {
fmt.Printf(">> Field: [ %s ]\n", fieldName)
}
}
dataMerger := merger.New(reader.Fields())
parser := mimemail.New(flags.TemplateFile)
if flags.Verbose {
fmt.Println("Subject: [", parser.Subject(), "]")
fmt.Println("Media-Type: ", parser.MediaType())
for _, part := range parser.Parts() {
fmt.Println(" >>", part.ContentType)
//fmt.Println(part.Body)
}
}
//err = reader.Each(func(dataMap map[string]string) bool {
// fmt.Printf("Email: [ %s ], Name: [ %s ]\n",
// dataMap[settings.Excel.EmailColumn], dataMap[settings.Excel.NameColumn])
// return true
//})
//
//if err != nil {
// log.Fatal(err)
//}
client := mailer.New(settings.Smtp)
client.SetFrom(settings.Main.SenderEmail, settings.Main.SenderName)
all := false
err = reader.Each(func(dataMap map[string]string) bool {
if flags.Verbose {
for _, fieldName := range reader.Fields() {
fmt.Printf(">> %s : [ %s ]\n", fieldName, dataMap[fieldName])
}
fmt.Println()
}
fmt.Printf("Email: [ %s ], Name: [ %s ]\n",
dataMap[settings.Excel.EmailColumn], dataMap[settings.Excel.NameColumn])
recipient := dataMap[settings.Excel.EmailColumn]
if flags.Debug {
recipient = settings.Debug.SendTo
}
if !all {
fmt.Printf("\nDo you want to send to [ %s ] ? [y(es)/N(o)/a(ll)/q(uit)]: ", recipient)
reader := bufio.NewReader(os.Stdin)
answer, _ := reader.ReadString('\n')
fmt.Println()
switch strings.TrimSpace(answer) {
case "a", "A":
all = true
case "q", "Q":
return false
default:
return true
case "y", "Y":
}
}
err := client.SendMailTo(
recipient,
dataMap[settings.Excel.NameColumn],
parser.Subject(),
func(composer mime_composer.MimeComposer) {
for _, part := range parser.Parts() {
composer.AddMultipartAlternative(part.ContentType,
dataMerger.Merge(part.ContentType, part.Body, dataMap))
}
})
if err != nil {
log.Println(err)
}
return true
})
//reader.
}