use gmail relay

alpha
alex 2024-03-19 21:48:23 +01:00
parent 20041347ad
commit 1ced19905d
2 changed files with 82 additions and 5 deletions

View File

@ -1,8 +1,6 @@
package main
import (
"fmt"
"clickandjoin.app/emailserver/mailer"
"clickandjoin.app/emailserver/modules/config"
"clickandjoin.app/emailserver/modules/rabbitmq"
@ -25,7 +23,6 @@ func init() {
}
func main() {
fmt.Println("Email server started")
gocnjhelper.LogInfo("Email server started")
rabbitmq.Init()

View File

@ -2,10 +2,12 @@ package structs
import (
"bytes"
"crypto/tls"
"encoding/base64"
"fmt"
"html/template"
"net/smtp"
"os"
"strings"
"time"
@ -49,10 +51,88 @@ func (m *Mail) Send(htmlBody, textBody string) error {
htmlBody + "\n\n" +
"--" + boundary + "--"
err := smtp.SendMail(cfg.Host+":"+cfg.Port, cache.SmtpAuth, cfg.FromEmail, m.To, []byte(msg))
/*
err := smtp.SendMail(cfg.Host+":"+cfg.Port, cache.SmtpAuth, cfg.FromEmail, m.To, []byte(msg))
if err != nil {
gocnjhelper.LogErrorf("smtp error: %s", err)
return err
} */
// create connection to SMTP-Server
// SMTP-Server-Adresse and Port
serverAddr := cfg.Host + ":" + cfg.Port
// TLS-Konfiguration mit Servernamen
tlsConfig := &tls.Config{
ServerName: cfg.Host,
}
conn, err := smtp.Dial(serverAddr)
if err != nil {
gocnjhelper.LogErrorf("smtp error: %s", err)
gocnjhelper.LogErrorf("Error connecting to server: %s", err)
return err
}
defer conn.Close()
hostname, err := os.Hostname()
if err != nil {
gocnjhelper.LogErrorf("Error getting hostname: %s", err)
return err
}
// send EHLO
err = conn.Hello(hostname)
if err != nil {
gocnjhelper.LogErrorf("Error sending EHLO: %s", err)
return err
}
// start tls connection
if err = conn.StartTLS(tlsConfig); err != nil {
gocnjhelper.LogErrorf("Error starting TLS: %s", err)
return err
}
// auth and send mail
if err = conn.Auth(cache.SmtpAuth); err != nil {
gocnjhelper.LogErrorf("Error authenticating: %s", err)
return err
}
if err = conn.Mail(cfg.FromEmail); err != nil {
gocnjhelper.LogErrorf("Error setting sender: %s", err)
return err
}
if err = conn.Rcpt(m.To[0]); err != nil {
gocnjhelper.LogErrorf("Error setting receiver: %s", err)
return err
}
w, err := conn.Data()
if err != nil {
gocnjhelper.LogErrorf("Error getting data writer: %s", err)
return err
}
_, err = w.Write([]byte(msg))
if err != nil {
gocnjhelper.LogErrorf("Error writing message: %s", err)
return err
}
err = w.Close()
if err != nil {
gocnjhelper.LogErrorf("Error closing data writer: %s", err)
return err
}