2022-03-05 16:27:09 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-03-14 10:29:53 +01:00
|
|
|
"flag"
|
2022-03-05 16:27:09 +01:00
|
|
|
"fmt"
|
|
|
|
"github.com/wneessen/go-mail"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2022-03-07 18:14:38 +01:00
|
|
|
th := os.Getenv("TEST_HOST")
|
2022-03-09 13:20:01 +01:00
|
|
|
if th == "" {
|
|
|
|
fmt.Printf("$TEST_HOST env variable cannot be empty\n")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2022-03-10 12:10:27 +01:00
|
|
|
|
2022-03-12 15:10:01 +01:00
|
|
|
fa := "Winni Neessen <wn@neessen.cloud>"
|
|
|
|
toa := "Winfried Neessen <wn@neessen.net>"
|
|
|
|
//toa = "Winfried Neessen <wneessen@arch-vm.fritz.box>"
|
|
|
|
|
2022-03-10 16:19:51 +01:00
|
|
|
m := mail.NewMsg()
|
2022-03-12 15:10:01 +01:00
|
|
|
if err := m.From(fa); err != nil {
|
2022-03-12 20:07:20 +01:00
|
|
|
fmt.Printf("failed to set FROM address: %s", err)
|
2022-03-10 16:19:51 +01:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2022-03-12 15:10:01 +01:00
|
|
|
if err := m.To(toa); err != nil {
|
2022-03-11 10:29:44 +01:00
|
|
|
fmt.Printf("failed to set TO address: %s", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2022-03-14 10:29:53 +01:00
|
|
|
m.Subject("This is a mail with attachments")
|
2022-03-10 16:19:51 +01:00
|
|
|
m.SetMessageID()
|
|
|
|
m.SetDate()
|
|
|
|
m.SetBulk()
|
2022-03-14 10:29:53 +01:00
|
|
|
m.SetBodyString(mail.TypeTextPlain, "This should have an attachment.")
|
2022-03-10 16:19:51 +01:00
|
|
|
|
2022-03-14 10:29:53 +01:00
|
|
|
f, err := os.Open("/home/wneessen/certs.csv")
|
2022-03-05 16:27:09 +01:00
|
|
|
if err != nil {
|
2022-03-14 10:29:53 +01:00
|
|
|
fmt.Printf("failed to open file for reading: %s\n", err)
|
2022-03-10 12:10:27 +01:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2022-03-14 10:29:53 +01:00
|
|
|
defer func() {
|
|
|
|
if err := f.Close(); err != nil {
|
|
|
|
fmt.Printf("failed to close file: %s\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
m.AttachReader("certs.csv", f, mail.WithFileName("test.txt"))
|
2022-03-13 17:15:23 +01:00
|
|
|
|
2022-03-14 10:29:53 +01:00
|
|
|
sendMail := flag.Bool("send", false, "wether to send mail or output to STDOUT")
|
|
|
|
flag.Parse()
|
|
|
|
if !*sendMail {
|
|
|
|
_, err := m.Write(os.Stdout)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("failed to write mail: %s\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
tu := os.Getenv("TEST_USER")
|
|
|
|
tp := os.Getenv("TEST_PASS")
|
|
|
|
c, err := mail.NewClient(th, mail.WithTLSPolicy(mail.TLSMandatory),
|
|
|
|
mail.WithSMTPAuth(mail.SMTPAuthLogin), mail.WithUsername(tu),
|
|
|
|
mail.WithPassword(tp))
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("failed to create new client: %s\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
if err := c.DialAndSend(m); err != nil {
|
|
|
|
fmt.Printf("failed to dial: %s\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
2022-03-05 16:27:09 +01:00
|
|
|
}
|