-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add the capability to send mail notifications
- Loading branch information
1 parent
ace6224
commit df85055
Showing
4 changed files
with
246 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,5 +7,19 @@ | |
"backupdir": "C:", | ||
"namespace": "", | ||
"backup-id": "", | ||
"pxarout": "" | ||
"pxarout": "", | ||
"smtp": { | ||
"host": "smtp.example.com", | ||
"port": "465", | ||
"username": "[email protected]", | ||
"password": "my-password", | ||
"mails": [{ | ||
"from": "[email protected]", | ||
"to": "[email protected] | ||
}, { | ||
"from": "[email protected]", | ||
"to": "[email protected] | ||
}] | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package main | ||
|
||
import ( | ||
"crypto/tls" | ||
"errors" | ||
"fmt" | ||
"net/smtp" | ||
) | ||
|
||
type unencryptedAuth struct { | ||
smtp.Auth | ||
} | ||
|
||
func (a unencryptedAuth) Start(server *smtp.ServerInfo) (string, []byte, error) { | ||
s := *server | ||
s.TLS = true | ||
return a.Auth.Start(&s) | ||
} | ||
|
||
func setupClient(host, port, username, password string, allowInsecure bool) (*smtp.Client, error) { | ||
var auth smtp.Auth | ||
auth = smtp.PlainAuth("", username, password, host) | ||
if port == "25" { | ||
if !allowInsecure { | ||
return nil, errors.New("sending plain password over unencrypted connection") | ||
} | ||
auth = unencryptedAuth{auth} | ||
} | ||
|
||
var tlsconfig *tls.Config | ||
if port != "25" { | ||
// TLS config | ||
tlsconfig = &tls.Config{ | ||
InsecureSkipVerify: allowInsecure, | ||
ServerName: host, | ||
} | ||
} | ||
|
||
servername := host + ":" + port | ||
|
||
var c *smtp.Client | ||
var err error | ||
if port == "465" { | ||
// Here is the key, you need to call tls.Dial instead of smtp.Dial | ||
// for smtp servers running on 465 that require an ssl connection | ||
// from the very beginning (no starttls) | ||
conn, err := tls.Dial("tcp", servername, tlsconfig) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
c, err = smtp.NewClient(conn, host) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} else { | ||
c, err = smtp.Dial(servername) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if port == "587" { | ||
c.StartTLS(tlsconfig) | ||
} | ||
} | ||
|
||
// Auth | ||
if err = c.Auth(auth); err != nil { | ||
fmt.Println("here", err) | ||
return nil, err | ||
} | ||
|
||
return c, nil | ||
} | ||
|
||
func sendMail(from, to, subject, body string, c *smtp.Client) error { | ||
// Setup headers | ||
headers := make(map[string]string) | ||
headers["From"] = from | ||
headers["To"] = to | ||
headers["Subject"] = subject | ||
|
||
// Setup message | ||
message := "" | ||
for k, v := range headers { | ||
message += fmt.Sprintf("%s: %s\r\n", k, v) | ||
} | ||
message += "\r\n" + body | ||
|
||
// To && From | ||
if err := c.Mail(from); err != nil { | ||
return err | ||
} | ||
|
||
if err := c.Rcpt(to); err != nil { | ||
return err | ||
} | ||
|
||
// Data | ||
w, err := c.Data() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, err = w.Write([]byte(message)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = w.Close() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters