/
jobMailer.go
98 lines (86 loc) · 3.06 KB
/
jobMailer.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
package main
import (
"github.com/LDCS/sflag"
"fmt"
"os"
"os/user"
"os/exec"
"strings"
"io/ioutil"
"encoding/base64"
)
func usage() {
fmt.Println(" ")
fmt.Println("./jobMailer --Subject <subject> --To <address> --Cc <address> --Bcc <address> --Contents <text file> --Attachment <attachment> ")
fmt.Println(" --Subject: subject of the email ")
fmt.Println(" --To: receivers (comma separated list) ")
fmt.Println(" --Cc: receivers (comma separated list) ")
fmt.Println(" --Bcc: receivers (comma separated list) ")
fmt.Println(" --Contents: a file contains the body of the email ")
fmt.Println(" --Attachment: email attachment ")
}
var opt = struct {
Subject string "Email subject"
To string "To address"
Cc string "Cc address"
Bcc string "Bcc address"
Contents string "Contents of the email|/dev/null"
Attachment string "Email attachment|/dev/null"
}{}
func main() {
sflag.Parse(&opt)
usr, err := user.Current()
if err != nil {
fmt.Println("failed to get uid, exiting.....")
return
}
hostname,err1 := os.Hostname()
if err1 != nil {
fmt.Println("failed to get hostname, exiting.....")
return
}
if opt.To == "" && opt.Cc == "" && opt.Bcc == "" {
usage()
return
}
bodyStr := ""
uuid1,_ := exec.Command("uuidgen").Output()
uuid := strings.TrimSpace(string(uuid1))
mpartClose := false //true -> multipart message (i.e) msg, attachment
bodyStr += "Subject:" + opt.Subject + "\n"
if opt.To != "" { bodyStr += "To:" + opt.To + "\n" }
if opt.Cc != "" { bodyStr += "Cc:" + opt.Cc + "\n" }
if opt.Bcc != "" { bodyStr += "Bcc:" + opt.Bcc + "\n" }
if opt.Contents != "/dev/null" || opt.Attachment != "/dev/null" {
bodyStr += "Content-Type: multipart/mixed; boundary=\""+uuid+"\"\nMIME-Version: 1.0\n"
}
if opt.Contents != "/dev/null" {
bodyStr += "\n--" + uuid + "\nContent-Type: text/plain\nContent-Disposition: inline\n\n"
mpartClose = true
cStr, err := ioutil.ReadFile(opt.Contents)
if err != nil {
panic("Unable to read contents file: " + err.Error())
}
bodyStr += string(cStr)
}
if opt.Attachment != "/dev/null" {
bodyStr += ( "\n--" +
uuid +
"\nContent-Transfer-Encoding: base64\nContent-Type: application/octet-stream; name=" +
opt.Attachment +
"\nContent-Disposition: attachment; filename=" +
opt.Attachment + "\n\n" )
mpartClose = true
cStr, err := ioutil.ReadFile(opt.Attachment)
if err != nil {
panic("Unable to read attachment file: " + err.Error())
}
bodyStr += base64.StdEncoding.EncodeToString(cStr) + "\n"
}
if mpartClose {
bodyStr += "--" + uuid + "--"
}
cmd := exec.Command("/usr/sbin/sendmail", "-t", "-F", usr.Username+"@"+hostname)
cmd.Stdin = strings.NewReader(bodyStr)
cmd.Run()
}