Skip to content

SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.

feat(query-service): support for SMTP service (#4258)

Prashant Shahia363b9865724
diff --git a/frontend/src/container/OrganizationSettings/PendingInvitesContainer/index.tsx b/frontend/src/container/OrganizationSettings/PendingInvitesContainer/index.tsx
index e2b2d8e77c..871ce95564 100644
--- a/frontend/src/container/OrganizationSettings/PendingInvitesContainer/index.tsx
+++ b/frontend/src/container/OrganizationSettings/PendingInvitesContainer/index.tsx
@@ -201,6 +201,7 @@ function PendingInvitesContainer(): JSX.Element {
email: member.email,
name: member.name,
role: member.role,
+ frontendBaseUrl: window.location.origin,
});
if (statusCode !== 200) {
diff --git a/frontend/src/types/api/user/setInvite.ts b/frontend/src/types/api/user/setInvite.ts
index d9112e76b5..e29e487cb1 100644
--- a/frontend/src/types/api/user/setInvite.ts
+++ b/frontend/src/types/api/user/setInvite.ts
@@ -5,6 +5,7 @@ export interface Props {
name: User['name'];
email: User['email'];
role: ROLES;
+ frontendBaseUrl: string;
}
export interface PayloadProps {
diff --git a/pkg/query-service/Dockerfile b/pkg/query-service/Dockerfile
index e2abe2868f..61fa02d698 100644
--- a/pkg/query-service/Dockerfile
+++ b/pkg/query-service/Dockerfile
@@ -18,6 +18,7 @@ COPY pkg/query-service/bin/query-service-${TARGETOS}-${TARGETARCH} /root/query-s
# copy prometheus YAML config
COPY pkg/query-service/config/prometheus.yml /root/config/prometheus.yml
+COPY pkg/query-service/templates /root/templates
# Make query-service executable for non-root users
RUN chmod 755 /root /root/query-service
diff --git a/pkg/query-service/auth/auth.go b/pkg/query-service/auth/auth.go
index 375bc76888..6b96a6da85 100644
--- a/pkg/query-service/auth/auth.go
+++ b/pkg/query-service/auth/auth.go
@@ -1,8 +1,11 @@
package auth
import (
+ "bytes"
"context"
"fmt"
+ "os"
+ "text/template"
"time"
"github.com/golang-jwt/jwt"
@@ -14,6 +17,7 @@ import (
"go.signoz.io/signoz/pkg/query-service/model"
"go.signoz.io/signoz/pkg/query-service/telemetry"
"go.signoz.io/signoz/pkg/query-service/utils"
+ smtpservice "go.signoz.io/signoz/pkg/query-service/utils/smtpService"
"go.uber.org/zap"
"golang.org/x/crypto/bcrypt"
)
@@ -27,6 +31,13 @@ var (
ErrorInvalidCreds = fmt.Errorf("Invalid credentials")
)
+type InviteEmailData struct {
+ CustomerName string
+ InviterName string
+ InviterEmail string
+ Link string
+}
+
// The root user should be able to invite people to create account on SigNoz cluster.
func Invite(ctx context.Context, req *model.InviteRequest) (*model.InviteResponse, error) {
zap.S().Debugf("Got an invite request for email: %s\n", req.Email)
@@ -80,9 +91,47 @@ func Invite(ctx context.Context, req *model.InviteRequest) (*model.InviteRespons
"invited user email": req.Email,
}, au.Email)
+ // send email if SMTP is enabled
+ if os.Getenv("SMTP_ENABLED") == "true" && req.FrontendBaseUrl != "" {
+ inviteEmail(req, au, token)
+ }
+
return &model.InviteResponse{Email: inv.Email, InviteToken: inv.Token}, nil
}
+func inviteEmail(req *model.InviteRequest, au *model.UserPayload, token string) {
+ smtp := smtpservice.GetInstance()
+ data := InviteEmailData{
+ CustomerName: req.Name,
+ InviterName: au.Name,
+ InviterEmail: au.Email,
+ Link: fmt.Sprintf("%s/signup?token=%s", req.FrontendBaseUrl, token),
+ }
+
+ tmpl, err := template.ParseFiles(constants.InviteEmailTemplate)
+ if err != nil {
+ zap.S().Errorf("failed to send email", err)
+ return
+ }
+
+ var body bytes.Buffer
+ if err := tmpl.Execute(&body, data); err != nil {
+ zap.S().Errorf("failed to send email", err)
+ return
+ }
+
+ err = smtp.SendEmail(
+ req.Email,
+ au.Name+" has invited you to their team in SigNoz",
+ body.String(),
+ )
+ if err != nil {
+ zap.S().Errorf("failed to send email", err)
+ return
+ }
+ return
+}
+
// RevokeInvite is used to revoke the invitation for the given email.
func RevokeInvite(ctx context.Context, email string) error {
zap.S().Debugf("RevokeInvite method invoked for email: %s\n", email)
diff --git a/pkg/query-service/constants/constants.go b/pkg/query-service/constants/constants.go
index 7cdd502ac5..3e7b737ce2 100644
--- a/pkg/query-service/constants/constants.go
+++ b/pkg/query-service/constants/constants.go
@@ -53,6 +53,8 @@ func GetAlertManagerApiPrefix() string {
return "http://alertmanager:9093/api/"
}
+var InviteEmailTemplate = GetOrDefaultEnv("INVITE_EMAIL_TEMPLATE", "/root/templates/invitation_email_template.html")
+
// Alert manager channel subpath
var AmChannelApiPath = GetOrDefaultEnv("ALERTMANAGER_API_CHANNEL_PATH", "v1/routes")
diff --git a/pkg/query-service/model/auth.go b/pkg/query-service/model/auth.go
index de6927b252..69525def12 100644
--- a/pkg/query-service/model/auth.go
+++ b/pkg/query-service/model/auth.go
@@ -7,9 +7,10 @@ var (
)
type InviteRequest struct {
- Name string `json:"name"`
- Email string `json:"email"`
- Role string `json:"role"`
+ Name string `json:"name"`
+ Email string `json:"email"`
+ Role string `json:"role"`
+ FrontendBaseUrl string `json:"frontendBaseUrl"`
}
type InviteResponse struct {
diff --git a/pkg/query-service/templates/invitation_email_template.html b/pkg/query-service/templates/invitation_email_template.html
new file mode 100644
index 0000000000..75fa3a32e8
--- /dev/null
+++ b/pkg/query-service/templates/invitation_email_template.html
@@ -0,0 +1,14 @@
+<!DOCTYPE html>
+<html>
+<body>
+ <p>Hi {{.CustomerName}},</p>
+ <p>You have been invited to join SigNoz project by {{.InviterName}} ({{.InviterEmail}}).</p>
+ <p>Please click on the following button to accept the invitation:</p>
+ <a href="{{.Link}}" style="background-color: #000000; color: white; padding: 14px 20px; text-align: center; text-decoration: none; display: inline-block;">Accept Invitation</a>
+ <p>Button not working? Paste the following link into your browser:</p>
+ <p>{{.Link}}</p>
+ <p>Follow docs here 👉 to <a href="https://signoz.io/docs/cloud/">Get Started with SigNoz Cloud</a></p>
+ <p>Thanks,</p>
+ <p>SigNoz Team</p>
+</body>
+</html>
diff --git a/pkg/query-service/utils/smtpService/smtp.go b/pkg/query-service/utils/smtpService/smtp.go
new file mode 100644
index 0000000000..454c5f022a
--- /dev/null
+++ b/pkg/query-service/utils/smtpService/smtp.go
@@ -0,0 +1,57 @@
+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)
+ }
+}