main.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package main
  2. import (
  3. "bufio"
  4. "flag"
  5. "fmt"
  6. "os"
  7. "strings"
  8. )
  9. var version = "0.1"
  10. func readFromConsole() string {
  11. var text, line string
  12. var err error
  13. counter := 0
  14. reader := bufio.NewReader(os.Stdin)
  15. for counter < 3 {
  16. line, err = reader.ReadString('\n')
  17. if line == "\n" {
  18. counter += 1
  19. } else {
  20. counter = 0
  21. }
  22. text += fmt.Sprintf("%s\n", line)
  23. }
  24. if err != nil {
  25. Error.F("Error in reading text from console\n%s", err)
  26. os.Exit(1)
  27. }
  28. return strings.TrimRight(text, "\n")
  29. }
  30. func toList(input string) []string {
  31. var result = []string{}
  32. list := strings.Split(input, ",")
  33. for _, element := range list {
  34. if element != "" {
  35. result = append(result, element)
  36. }
  37. }
  38. return result
  39. }
  40. func initializeConfig(configPath, section string) *Config {
  41. if configPath == "" {
  42. configPath = "/etc/sendmail.toml"
  43. // Ignore non existing config only if `-conf` not used on command line.
  44. if _, err := os.Stat(configPath); os.IsNotExist(err) {
  45. return NewConfig()
  46. }
  47. }
  48. config, err := readConfig(configPath, section)
  49. if err != nil {
  50. if os.IsNotExist(err) {
  51. Error.F("Error in parsing the configuration\n%s", err)
  52. os.Exit(-2)
  53. }
  54. }
  55. Debug.F("---\nConfig from %s\n%s", configPath, *config)
  56. return config
  57. }
  58. func main() {
  59. var err error
  60. var configPath, section, serverAddress, user, password, to, cc, bcc, from, subject, text string
  61. var encryption, dbg, versionFlag bool
  62. var serverPort_ int
  63. var serverPort int64
  64. flag.BoolVar(&versionFlag, "version", false, "Prints the version and exits")
  65. flag.StringVar(&configPath, "conf", "", "Path to a config file (defaults to /etc/sendmail.toml)")
  66. flag.StringVar(&section, "section", "default", "Section of the conf to read (defaults to \"default\")")
  67. flag.BoolVar(&dbg, "dbg", false, "Enable debugging output")
  68. flag.StringVar(&serverAddress, "server-address", "", "The SMTP server address")
  69. flag.IntVar(&serverPort_, "server-port", 0, "The SMTP server")
  70. flag.BoolVar(&encryption, "force-ssl", false, "Force the use of ssl (defalut: false)")
  71. flag.StringVar(&user, "user", "", "The user to authenticate with to the server")
  72. flag.StringVar(&password, "password", "", "The password to authenticate with to the server")
  73. flag.StringVar(&to, "to", "", "Comma-separated list of recipient(s)")
  74. flag.StringVar(&cc, "cc", "", "Comma-separated list of carbon-copy recipient(s)")
  75. flag.StringVar(&bcc, "bcc", "", "Comma-separated list of blind carbon-copy recipient(s)")
  76. flag.StringVar(&from, "from", "", "Sender of the mail (used as default account user to log in on the SMTP server)")
  77. flag.StringVar(&subject, "sub", "", "Subject of the mail")
  78. flag.Parse()
  79. LogInit(dbg)
  80. if versionFlag {
  81. Info.F("Version: %s", version)
  82. os.Exit(0)
  83. }
  84. if flag.NArg() == 0 {
  85. text = readFromConsole()
  86. } else {
  87. for _, arg := range flag.Args() {
  88. text += fmt.Sprintf("%s\n", arg)
  89. }
  90. }
  91. serverPort = int64(serverPort_)
  92. Debug.F(
  93. `
  94. ---
  95. parameters:
  96. conf: %s
  97. dbg: %t
  98. address: %s
  99. port: %d
  100. encryption: %t
  101. user: %s
  102. password: %s
  103. to: %s
  104. from: %s
  105. subject: %s`,
  106. configPath,
  107. dbg,
  108. serverAddress,
  109. serverPort,
  110. encryption,
  111. user,
  112. password,
  113. to,
  114. from,
  115. subject,
  116. )
  117. config := initializeConfig(configPath, section)
  118. config.Server.Merge("Address", serverAddress)
  119. config.Server.Merge("Port", serverPort)
  120. config.Server.Merge("Encryption", encryption)
  121. config.Server.Merge("User", user)
  122. config.Server.Merge("Password", password)
  123. config.Merge("From", from)
  124. config.Merge("To", toList(to))
  125. config.Merge("Cc", toList(cc))
  126. config.Merge("Bcc", toList(bcc))
  127. config.Merge("Subject", subject)
  128. config.Merge("Text", text)
  129. Debug.F("---\nPre-validation config\n%s", config)
  130. err = config.Validate()
  131. if err != nil {
  132. Error.F("Config validation failed:\n%s\n", err)
  133. os.Exit(1)
  134. }
  135. Info.F("Sending mail | to: %s", config.To)
  136. SendMail(config)
  137. }