musala/cmd/musala-mail/main.go

84 lines
2.2 KiB
Go
Raw Normal View History

2018-08-21 08:36:54 +00:00
package main
import (
2019-08-23 10:23:33 +00:00
"crypto/tls"
2018-08-22 16:57:50 +00:00
"fmt"
2019-08-23 10:23:33 +00:00
"github.com/go-mail/mail"
2019-09-04 05:35:49 +00:00
"os"
2018-08-21 08:36:54 +00:00
)
func main() {
// Setup config
2019-08-23 10:23:33 +00:00
fmt.Println("d: parsing config")
2018-08-21 08:36:54 +00:00
config := NewConfig()
config.Parse()
2019-09-04 05:35:49 +00:00
fmt.Printf("%+v\n", config)
2018-08-21 08:36:54 +00:00
// Get task list as markdown
2019-08-23 10:23:33 +00:00
fmt.Println("d: configuring trello")
2019-09-04 05:35:49 +00:00
trelloCtx := NewTrello(config.TrelloApiKey, config.TrelloToken)
if trelloCtx == nil {
fmt.Println("ERROR: Unable to initialize trello context")
os.Exit(1)
}
2019-08-23 10:23:33 +00:00
fmt.Println("d: getting trello boards")
var trelloBoardsList []TrelloBoard
if len(config.TrelloUrl) > 0 {
2019-08-23 10:23:33 +00:00
fmt.Printf("d: using given url %s\n", config.TrelloUrl)
trelloBoard := trelloCtx.GetBoard(config.TrelloUrl)
trelloBoardsList = append(trelloBoardsList, trelloBoard)
} else {
2019-08-23 10:23:33 +00:00
fmt.Println("d: fetching boards")
trelloBoardsList = trelloCtx.GetBoards()
}
for _, trelloBoard := range trelloBoardsList {
2019-08-23 10:23:33 +00:00
fmt.Printf("d: loading board %s\n", trelloBoard.Name)
2018-11-23 12:22:03 +00:00
if !trelloBoard.Starred || trelloBoard.Closed {
fmt.Printf("d: skipping %s\n", trelloBoard.Name)
2018-11-23 12:22:03 +00:00
continue
}
fmt.Printf("d: exporting content of %s\n", trelloBoard.Name)
2018-11-23 12:22:03 +00:00
trelloMarkdown := trelloBoard.ExportToMarkdown()
trelloHtml := trelloBoard.ExportToHtml()
config.EmailSubject = fmt.Sprintf("Daily mail for %s", trelloBoard.Name)
// Create email enveloppe
2019-08-23 10:23:33 +00:00
email := mail.NewMessage()
email.SetHeader("To", config.EmailTo[0])
if len(config.EmailTo) > 0 {
email.SetHeader("Cc", config.EmailTo[1:]...)
}
email.SetHeader("From", config.EmailFrom)
email.SetHeader("Subject", config.EmailSubject)
email.SetBody("text/plain", trelloMarkdown)
email.AddAlternative("text/html", trelloHtml)
2018-11-23 12:22:03 +00:00
// Connect and send email
2019-08-23 10:23:33 +00:00
var transport *mail.Dialer
if len(config.SmtpUsername) > 0 {
fmt.Println("d: transport w/ username")
2019-08-23 10:23:33 +00:00
transport = mail.NewDialer(
config.SmtpHostname,
int(config.SmtpPort),
config.SmtpUsername,
config.SmtpPassword,
)
// disable cert verification
transport.TLSConfig = &tls.Config{InsecureSkipVerify: true}
} else {
fmt.Println("d: transport w/out username")
2019-08-23 10:23:33 +00:00
transport = &mail.Dialer{
Host: config.SmtpHostname,
Port: int(config.SmtpPort),
}
}
2018-11-23 12:22:03 +00:00
2019-08-23 10:23:33 +00:00
if err := transport.DialAndSend(email); err != nil {
panic(err)
}
2018-11-23 12:22:03 +00:00
}
2018-08-21 08:36:54 +00:00
}