Prashant Shahi a363b98657
feat(query-service): support for SMTP service (#4258)
* feat(query-service): support for SMTP service

Signed-off-by: Prashant Shahi <prashant@signoz.io>

* feat(query-service): smtp minor fixes

Signed-off-by: Prashant Shahi <prashant@signoz.io>

* chore: fix smtp and add email template

* chore: update template

* chore(smpt-service): configurable invite email template path

Signed-off-by: Prashant Shahi <prashant@signoz.io>

---------

Signed-off-by: Prashant Shahi <prashant@signoz.io>
Co-authored-by: Vishal Sharma <makeavish786@gmail.com>
2023-12-21 18:27:30 +05:30

58 lines
1.1 KiB
Go

package smtpservice
import (
"net/smtp"
"os"
"strings"
"sync"
)
type SMTP struct {
Host string
Port string
Username string
Password string
From string
}
var smtpInstance *SMTP
var once sync.Once
func New() *SMTP {
return &SMTP{
Host: os.Getenv("SMTP_HOST"),
Port: os.Getenv("SMTP_PORT"),
Username: os.Getenv("SMTP_USERNAME"),
Password: os.Getenv("SMTP_PASSWORD"),
From: os.Getenv("SMTP_FROM"),
}
}
func GetInstance() *SMTP {
once.Do(func() {
smtpInstance = New()
})
return smtpInstance
}
func (s *SMTP) SendEmail(to, subject, body string) error {
msgString := "From: " + s.From + "\r\n" +
"To: " + to + "\r\n" +
"Subject: " + subject + "\r\n" +
"MIME-Version: 1.0\r\n" +
"Content-Type: text/html; charset=UTF-8\r\n" +
"\r\n" +
body
msg := []byte(msgString)
addr := s.Host + ":" + s.Port
if s.Password == "" || s.Username == "" {
return smtp.SendMail(addr, nil, s.From, strings.Split(to, ","), msg)
} else {
auth := smtp.PlainAuth("", s.Username, s.Password, s.Host)
return smtp.SendMail(addr, auth, s.From, strings.Split(to, ","), msg)
}
}