forked from rahulsom/mailcli
/
mailcli.go
109 lines (88 loc) · 2.47 KB
/
mailcli.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
package main
import (
"flag"
"fmt"
"github.com/sendgrid/sendgrid-go"
"github.com/vanng822/go-premailer/premailer"
"io/ioutil"
"log"
"net/mail"
"os"
"regexp"
)
func loadEnvironment() (string, string, string) {
from := os.ExpandEnv("${SENDGRID_FROM}")
username := os.ExpandEnv("${SENDGRID_USER}")
password := os.ExpandEnv("${SENDGRID_PASS}")
if from == "" || username == "" || password == "" {
log.Println("You need to define SENDGRID_FROM, SENDGRID_USER, SENDGRID_PASS in the environment.")
os.Exit(1)
}
return from, username, password
}
func parseRecipients(input *string) []*mail.Address {
recipients, err := mail.ParseAddressList(*input)
if err != nil {
return []*mail.Address{}
}
log.Println("Recipients are", recipients)
return recipients
}
func main() {
from, username, password := loadEnvironment()
to := flag.String("to", "", "Recipients in To")
cc := flag.String("cc", "", "Recipients in CC")
bcc := flag.String("bcc", "", "Recipients in BCC")
subject := flag.String("s", "", "Subject")
isHtml := flag.Bool("html", false, "Send as HTML")
help := flag.Bool("h", false, "Display this help message")
flag.Parse()
if *help {
flag.Usage()
os.Exit(0)
}
if *to == "" && *cc == "" && *bcc == "" {
log.Println("Need atleast one of to,cc,bcc")
flag.Usage()
os.Exit(2)
}
if *subject == "" {
log.Println("Subject cannot be empty")
flag.Usage()
os.Exit(3)
}
sg := sendgrid.NewSendGridClient(username, password)
message := sendgrid.NewMail()
message.SetFrom(from)
message.AddRecipients(parseRecipients(to))
message.AddCcRecipients(parseRecipients(cc))
message.AddBccRecipients(parseRecipients(bcc))
message.SetSubject(*subject)
b, err := ioutil.ReadAll(os.Stdin)
if err != nil {
log.Panic("Could not read stdin")
}
inputString := string(b)
if *isHtml {
// This fixes a bug in andybalholm/cascadia in dealing with :: in css for somethings.
regex := regexp.MustCompile("(?m)^.*::.*$")
inputString = regex.ReplaceAllLiteralString(inputString, "")
// This turns stylesheets into inline styles so email clients respect the css.
prem := premailer.NewPremailerFromString(inputString, premailer.NewOptions())
htmlString, err := prem.Transform()
if err != nil {
log.Fatal(err)
}
message.SetHTML(htmlString)
log.Println("This is HTML")
} else {
message.SetText(inputString)
log.Println("This is Text")
}
// fmt.Println ("SG",sg)
if r := sg.Send(message); r == nil {
fmt.Println("Email sent!")
} else {
fmt.Println(r)
}
}