-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
106 lines (91 loc) · 2.41 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
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
package main
import (
"bufio"
"fmt"
"net/url"
"os"
"strconv"
"strings"
"log"
"github.com/NoUseFreak/go-vembed"
"github.com/spf13/cobra"
"gopkg.in/gomail.v2"
)
var smtp string
var from string
var cc []string
var bcc []string
var subject string
var body string
func init() {
rootCmd.PersistentFlags().StringVar(&smtp, "smtp", "smtp://127.0.0.1:25", "Define the smtp server")
rootCmd.PersistentFlags().StringVarP(&subject, "subject", "s", "Test mail from go-mail", "Subject of the mail")
rootCmd.PersistentFlags().StringVarP(&from, "from", "f", "go-mail@example.com", "Send the mail from this address")
rootCmd.PersistentFlags().StringVar(&body, "body", "Hello from go-mail", "Define the message body")
rootCmd.PersistentFlags().StringArrayVarP(&cc, "cc-addr", "c", []string{}, "Send a carbon copy to this address")
rootCmd.PersistentFlags().StringArrayVarP(&bcc, "bcc-addr", "b", []string{}, "Send a blink carbon copy to this address")
rootCmd.Version = fmt.Sprintf(
"%s, build %s",
vembed.Version.GetGitSummary(),
vembed.Version.GetGitCommit(),
)
}
func main() {
rootCmd.Execute()
}
var rootCmd = &cobra.Command{
Use: "go-mail [-s subject] to-addr ...",
Short: "go-mail",
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
stdin, err := readStdin()
if err != nil {
log.Fatalf("Failed capturing input - %s", err.Error())
return err
}
if stdin != "" {
body = stdin
}
u, err := url.Parse(smtp)
if err != nil {
return err
}
var port int
if port, err = strconv.Atoi(u.Port()); err != nil {
return err
}
dailer := gomail.Dialer{Host: u.Hostname(), Port: port}
m := gomail.NewMessage()
m.SetHeader("To", args...)
if len(cc) > 0 {
m.SetHeader("Cc", cc...)
}
if len(bcc) > 0 {
m.SetHeader("Bcc", bcc...)
}
m.SetHeader("From", from)
m.SetHeader("Subject", subject)
m.SetBody("text/plain", body)
if err := dailer.DialAndSend(m); err != nil {
log.Fatalf("Failed to send message - %s", err.Error())
return err
}
log.Printf("Message sent to %s", strings.Join(args, ", "))
return nil
},
}
func readStdin() (string, error) {
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeCharDevice) != 0 {
return "", nil
}
var stdin []byte
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
stdin = append(stdin, scanner.Bytes()...)
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
return string(stdin), nil
}