mirror of
https://github.com/containrrr/watchtower.git
synced 2024-12-15 09:14:13 +02:00
1b82da1ab7
* add tests to ensure function even after switching docker client api version * switch docker client api version to remove import of Sirupsen and get rid of the casing workaround * migrate from glide to dep to go modules * rewrite ci workflow * only run publish on version tags * only run build on branches * update goreleaser config * disable automated latest tag push * remove dependency to v2tec/docker-gobuilder * remove dead code and files * add golands .idea folder to gitignore * add label to released docker images * add test reporting, add some unit tests * change test output dir * fix goreleaser versions * add debug output for circleci and goreleaser * disable cgo
78 lines
2.1 KiB
Go
78 lines
2.1 KiB
Go
// Package notifications ...
|
|
// Copyright 2010 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license.
|
|
package notifications
|
|
|
|
import (
|
|
"crypto/tls"
|
|
"net"
|
|
"net/smtp"
|
|
)
|
|
|
|
// SendMail connects to the server at addr, switches to TLS if
|
|
// possible, authenticates with the optional mechanism a if possible,
|
|
// and then sends an email from address from, to addresses to, with
|
|
// message msg.
|
|
// The addr must include a port, as in "mail.example.com:smtp".
|
|
//
|
|
// The addresses in the to parameter are the SMTP RCPT addresses.
|
|
//
|
|
// The msg parameter should be an RFC 822-style email with headers
|
|
// first, a blank line, and then the message body. The lines of msg
|
|
// should be CRLF terminated. The msg headers should usually include
|
|
// fields such as "From", "To", "Subject", and "Cc". Sending "Bcc"
|
|
// messages is accomplished by including an email address in the to
|
|
// parameter but not including it in the msg headers.
|
|
//
|
|
// The SendMail function and the net/smtp package are low-level
|
|
// mechanisms and provide no support for DKIM signing, MIME
|
|
// attachments (see the mime/multipart package), or other mail
|
|
// functionality. Higher-level packages exist outside of the standard
|
|
// library.
|
|
func SendMail(addr string, insecureSkipVerify bool, a smtp.Auth, from string, to []string, msg []byte) error {
|
|
c, err := smtp.Dial(addr)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer c.Close()
|
|
if err = c.Hello("localHost"); err != nil {
|
|
return err
|
|
}
|
|
if ok, _ := c.Extension("STARTTLS"); ok {
|
|
serverName, _, _ := net.SplitHostPort(addr)
|
|
config := &tls.Config{ServerName: serverName, InsecureSkipVerify: insecureSkipVerify}
|
|
if err = c.StartTLS(config); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if a != nil {
|
|
if ok, _ := c.Extension("AUTH"); ok {
|
|
if err = c.Auth(a); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
if err = c.Mail(from); err != nil {
|
|
return err
|
|
}
|
|
for _, addr := range to {
|
|
if err = c.Rcpt(addr); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
w, err := c.Data()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
_, err = w.Write(msg)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = w.Close()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return c.Quit()
|
|
}
|