Replace mail send with ntfy

This commit is contained in:
Simon Rieger 2023-11-23 17:26:07 +01:00
parent a76f6bfb0c
commit 222fc32816
5 changed files with 33 additions and 69 deletions

View file

@ -10,7 +10,7 @@ RUN go mod download
COPY *.go ./ COPY *.go ./
RUN CGO_ENABLED=0 GOOS=linux go build -o /docker-mail-reminder RUN CGO_ENABLED=0 GOOS=linux go build -o /main
# Run the tests in the container # Run the tests in the container
FROM build-stage AS run-test-stage FROM build-stage AS run-test-stage
@ -21,10 +21,10 @@ FROM gcr.io/distroless/base-debian11 AS build-release-stage
WORKDIR / WORKDIR /
COPY --from=build-stage /docker-mail-reminder /docker-mail-reminder COPY --from=build-stage /main /main
#EXPOSE 8080 #EXPOSE 8080
USER nonroot:nonroot USER nonroot:nonroot
ENTRYPOINT ["/docker-mail-reminder"] ENTRYPOINT ["/main"]

View file

@ -1,6 +1,6 @@
# Your Go Application # GO ical ntfy reminder
This is a simple Go application that does iCalendar files (.ics) analyzes and sends notifications by email based on the events in these iCalendar files. This is a simple Go application that does iCalendar files (.ics) analyzes and sends notifications by ntfy based on the events in these iCalendar files.
## Prerequisites ## Prerequisites
@ -14,7 +14,7 @@ Before you begin, ensure you have the following installed:
1. **Clone the repository:** 1. **Clone the repository:**
```bash ```bash
git clone <repository-url> git clone https://code.brothertec.eu/simono41/go-ical-ntfy-reminder.git
``` ```
2. **Create an environment variables file:** 2. **Create an environment variables file:**
@ -22,11 +22,9 @@ Before you begin, ensure you have the following installed:
Create a file named `.env` in your project directory. Fill this file with the required environment variables: Create a file named `.env` in your project directory. Fill this file with the required environment variables:
```env ```env
FROM_EMAIL=user@example.com
TO_EMAIL=recipient@example.com TO_EMAIL=recipient@example.com
EMAIL_PASSWORD=your_password NTFY_AUTH=echo "Basic $(echo -n 'testuser:fakepassword' | base64)"
SMTP_HOST=smtp.example.com NTFY_HOST=https://ntfy.sh/alerts
SMTP_PORT=587
ICS_DIR=/path/to/ics/files ICS_DIR=/path/to/ics/files
``` ```

2
go.mod
View file

@ -5,12 +5,10 @@ go 1.21
require ( require (
github.com/apognu/gocal v0.9.0 github.com/apognu/gocal v0.9.0
github.com/sirupsen/logrus v1.9.3 github.com/sirupsen/logrus v1.9.3
gopkg.in/mail.v2 v2.3.1
) )
require ( require (
github.com/ChannelMeter/iso8601duration v0.0.0-20150204201828-8da3af7a2a61 // indirect github.com/ChannelMeter/iso8601duration v0.0.0-20150204201828-8da3af7a2a61 // indirect
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
gopkg.in/yaml.v3 v3.0.0 // indirect gopkg.in/yaml.v3 v3.0.0 // indirect
) )

4
go.sum
View file

@ -17,11 +17,7 @@ github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5Cc
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc h1:2gGKlE2+asNV9m7xrywl36YYNnBG5ZQ0r/BOOxqPpmk=
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc/go.mod h1:m7x9LTH6d71AHyAX77c9yqWCCa3UKHcVEj9y7hAtKDk=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/mail.v2 v2.3.1 h1:WYFn/oANrAGP2C0dcV6/pbkPzv8yGzqTjPmTeO7qoXk=
gopkg.in/mail.v2 v2.3.1/go.mod h1:htwXN1Qh09vZJ1NVKxQqHPBaCBbzKhp5GzuJEA4VJWw=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0 h1:hjy8E9ON/egN1tAYqKb61G10WtihqetD4sz2H+8nIeA= gopkg.in/yaml.v3 v3.0.0 h1:hjy8E9ON/egN1tAYqKb61G10WtihqetD4sz2H+8nIeA=
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

74
main.go
View file

@ -1,23 +1,19 @@
package main package main
import ( import (
"crypto/tls"
"fmt" "fmt"
"github.com/apognu/gocal" "github.com/apognu/gocal"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
gomail "gopkg.in/mail.v2" "net/http"
"os" "os"
"strconv"
"strings" "strings"
"time" "time"
) )
var ( var (
fromString string
toAddresses []string toAddresses []string
passwordString string authString string
hostString string hostString string
hostPortString int
) )
func main() { func main() {
@ -28,22 +24,10 @@ func main() {
log.Info("Hello World!") log.Info("Hello World!")
// Initialisiere err mit einem Fehlerwert
var err error
// Read environment variables // Read environment variables
fromString = os.Getenv("FROM_EMAIL")
toAddresses = strings.Split(os.Getenv("TO_EMAIL"), ",") toAddresses = strings.Split(os.Getenv("TO_EMAIL"), ",")
passwordString = os.Getenv("EMAIL_PASSWORD") authString = os.Getenv("NTFY_AUTH")
hostString = os.Getenv("SMTP_HOST") hostString = os.Getenv("NTFY_HOST")
hostPortString, err = strconv.Atoi(os.Getenv("SMTP_PORT"))
// Überprüfung auf Fehler
if err != nil {
fmt.Println("Fehler bei der Umwandlung. Verwende Standardwert 587.")
// Setze einen Standardwert, zum Beispiel 0
hostPortString = 587
}
// Get the current working directory // Get the current working directory
dir, err := os.Getwd() dir, err := os.Getwd()
@ -54,13 +38,13 @@ func main() {
// Read and split multiple folders // Read and split multiple folders
folderList := os.Getenv("ICS_DIR") folderList := os.Getenv("ICS_DIR")
if folderList == "" || fromString == "" || len(toAddresses) == 0 || passwordString == "" || hostString == "" { if folderList == "" || len(toAddresses) == 0 || authString == "" || hostString == "" {
log.Fatal("Es fehlen noch einige Parameter!!!\nICS_DIR, FROM_EMAIL, TO_EMAIL, EMAIL_PASSWORD, SMTP_HOST") log.Fatal("Es fehlen noch einige Parameter!!!\nICS_DIR, TO_EMAIL, NTFY_AUTH, NTFY_HOST")
} }
folders := strings.Split(folderList, ",") folders := strings.Split(folderList, ",")
for _, folder := range folders { for _, folder := range folders {
log.Infof("folder: %s from: %s to: %s password: %s host: %s", folder, fromString, toAddresses, passwordString, hostString) log.Infof("folder: %s to: %s password: %s host: %s", folder, toAddresses, authString, hostString)
listFilesForFolder(folder) listFilesForFolder(folder)
} }
} }
@ -131,10 +115,10 @@ func getNotifications(file string) {
if len(e.Description) != 0 { if len(e.Description) != 0 {
messageText += fmt.Sprintf("\n\nFolgende Notiz existiert in diesen Eintrag: \n%s", e.Description) messageText += fmt.Sprintf("\n\nFolgende Notiz existiert in diesen Eintrag: \n%s", e.Description)
} }
messageText += "\n\n This email is a service from mail-reminder Version 1.0 written in Golang. \n Delivered by Simon Rieger" messageText += "\n\n This message is a service from go-ical-ntfy-reminder Version 1.0 written in Golang. \n Delivered by Simon Rieger"
for _, toAddress := range toAddresses { for _, toAddress := range toAddresses {
sendMail(messageSubject, messageText, toAddress) sendMessage(messageSubject, messageText, toAddress)
} }
} }
} }
@ -143,32 +127,20 @@ func truncateToDay(t time.Time) time.Time {
return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location()) return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
} }
func sendMail(messageSubject string, messageText string, toAddress string) { func sendMessage(messageSubject string, messageText string, toAddress string) {
m := gomail.NewMessage()
// Set E-Mail sender req, _ := http.NewRequest("POST", hostString,
m.SetHeader("From", fromString) strings.NewReader(messageText))
req.Header.Set("Title", messageSubject)
// Set E-Mail receivers req.Header.Set("Authorization", "Basic "+authString)
m.SetHeader("To", toAddress) req.Header.Set("Email", toAddress)
req.Header.Set("Tags", "date,octopus")
// Set E-Mail subject req.Header.Set("Priority", "high")
m.SetHeader("Subject", messageSubject) do, err := http.DefaultClient.Do(req)
if err != nil {
// Set E-Mail body. You can set plain text or html with text/html log.Fatal("Ntfy Message not Sent Successfully, Error: " + err.Error())
m.SetBody("text/plain", messageText) return
// Settings for SMTP server
d := gomail.NewDialer(hostString, hostPortString, fromString, passwordString)
// This is only needed when SSL/TLS certificate is not valid on server.
// In production this should be set to false.
d.TLSConfig = &tls.Config{InsecureSkipVerify: true}
// Now send E-Mail
if err := d.DialAndSend(m); err != nil {
log.Fatal(err)
panic(err)
} }
log.Infof("Email Message Sent Successfully")
log.Infof("Ntfy Message Sent Successfully, Status: " + do.Status)
} }