From d07b36788c56742e3365b5a6505f7d2f3c588eb2 Mon Sep 17 00:00:00 2001
From: Yege1893 <yannick.ege@web.de>
Date: Wed, 5 Jul 2023 14:03:07 +0200
Subject: [PATCH] email sender

---
 go.work                                     |  5 +-
 src/emailnotification/.env                  |  4 ++
 src/emailnotification/go.mod                |  5 ++
 src/emailnotification/go.sum                |  2 +
 src/emailnotification/main.go               |  7 +++
 src/emailnotification/service/send_email.go | 57 +++++++++++++++++++++
 6 files changed, 79 insertions(+), 1 deletion(-)
 create mode 100644 src/emailnotification/.env
 create mode 100644 src/emailnotification/go.mod
 create mode 100644 src/emailnotification/go.sum
 create mode 100644 src/emailnotification/main.go
 create mode 100644 src/emailnotification/service/send_email.go

diff --git a/go.work b/go.work
index f494a06..5ef3029 100644
--- a/go.work
+++ b/go.work
@@ -1,3 +1,6 @@
 go 1.20
 
-use ./src/highlanderticketing
+use (
+	./src/emailnotification
+	./src/highlanderticketing
+)
diff --git a/src/emailnotification/.env b/src/emailnotification/.env
new file mode 100644
index 0000000..2ac09cd
--- /dev/null
+++ b/src/emailnotification/.env
@@ -0,0 +1,4 @@
+# .env file
+
+EMAIL_PW=highlanderAPI
+EMAIL_ADRESS=highlanderAPI@web.de
\ No newline at end of file
diff --git a/src/emailnotification/go.mod b/src/emailnotification/go.mod
new file mode 100644
index 0000000..515173d
--- /dev/null
+++ b/src/emailnotification/go.mod
@@ -0,0 +1,5 @@
+module gitlab.reutlingen-university.de/ege/highlander-ticketing-go-ss2023/src/emailnotification
+
+go 1.20
+
+require github.com/joho/godotenv v1.5.1
diff --git a/src/emailnotification/go.sum b/src/emailnotification/go.sum
new file mode 100644
index 0000000..d61b19e
--- /dev/null
+++ b/src/emailnotification/go.sum
@@ -0,0 +1,2 @@
+github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
+github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
diff --git a/src/emailnotification/main.go b/src/emailnotification/main.go
new file mode 100644
index 0000000..e479753
--- /dev/null
+++ b/src/emailnotification/main.go
@@ -0,0 +1,7 @@
+package main
+
+import "gitlab.reutlingen-university.de/ege/highlander-ticketing-go-ss2023/src/emailnotification/service"
+
+func main() {
+	service.SendEmail()
+}
diff --git a/src/emailnotification/service/send_email.go b/src/emailnotification/service/send_email.go
new file mode 100644
index 0000000..a11f220
--- /dev/null
+++ b/src/emailnotification/service/send_email.go
@@ -0,0 +1,57 @@
+package service
+
+import (
+	"fmt"
+	"log"
+	"net/mail"
+	"net/smtp"
+	"os"
+	"strings"
+
+	"github.com/joho/godotenv"
+)
+
+func SendEmail( /*toList []string, subject string, body string*/ ) {
+	err := godotenv.Load(".env")
+
+	if err != nil {
+		log.Fatalf("Error loading .env file")
+	}
+
+	from := mail.Address{
+		Name:    "Highlander Ticketing",
+		Address: os.Getenv("EMAIL_ADRESS"),
+	}
+
+	fmt.Println(from)
+
+	toList := []string{"yannick.ege@web.de"}
+
+	header := make(map[string]string)
+	header["From"] = from.String()
+	header["To"] = strings.Join(toList, ", ")
+	header["Subject"] = "subject"
+
+	body := "test"
+
+	message := ""
+	for key, value := range header {
+		message += fmt.Sprintf("%s: %s\r\n", key, value)
+	}
+	message += "\r\n" + body
+
+	// über os variablen holen
+	smtpServer := "smtp.web.de"
+	smtpPort := "587"
+	password := os.Getenv("EMAIL_PW")
+
+	auth := smtp.PlainAuth("", from.Address, password, smtpServer)
+
+	err1 := smtp.SendMail(smtpServer+":"+smtpPort, auth, from.Address, toList, []byte(message))
+	if err1 != nil {
+		fmt.Println(err1)
+		os.Exit(1)
+	}
+
+	fmt.Println("E-Mail erfolgreich gesendet.")
+}
-- 
GitLab