-
Notifications
You must be signed in to change notification settings - Fork 61
[FEAT] implement email service send emails for workspace invitations #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
martian56
merged 4 commits into
main
from
20-implement-email-service-send-emails-for-workspace-invitations
Mar 9, 2026
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| package handler | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "strings" | ||
|
|
||
| "github.com/Devlaner/devlane/api/internal/store" | ||
| "github.com/gin-gonic/gin" | ||
| ) | ||
|
|
||
| // InvitationHandler serves public invite-by-token endpoints (no auth). | ||
| type InvitationHandler struct { | ||
| Winv *store.WorkspaceInviteStore | ||
| Ws *store.WorkspaceStore | ||
| } | ||
|
|
||
| // GetInviteByToken returns workspace invite details by token for the invite landing page. | ||
| // GET /api/invitations/by-token/?token=... | ||
| func (h *InvitationHandler) GetInviteByToken(c *gin.Context) { | ||
| token := strings.TrimSpace(c.Query("token")) | ||
| if token == "" { | ||
| c.JSON(http.StatusBadRequest, gin.H{"error": "token required"}) | ||
| return | ||
| } | ||
| inv, err := h.Winv.GetByToken(c.Request.Context(), token) | ||
| if err != nil || inv == nil { | ||
| c.JSON(http.StatusNotFound, gin.H{"error": "Invite not found or expired"}) | ||
| return | ||
| } | ||
| w, err := h.Ws.GetByID(c.Request.Context(), inv.WorkspaceID) | ||
| if err != nil || w == nil { | ||
| c.JSON(http.StatusNotFound, gin.H{"error": "Workspace not found"}) | ||
| return | ||
| } | ||
| c.JSON(http.StatusOK, gin.H{ | ||
| "workspace_name": w.Name, | ||
| "workspace_slug": w.Slug, | ||
| "email": inv.Email, | ||
| "invitation_id": inv.ID.String(), | ||
| }) | ||
| } | ||
|
|
||
| // DeclineInviteByToken removes the invitation (Ignore flow). No auth required. | ||
| // POST /api/invitations/decline/ body: { "token": "..." } | ||
| func (h *InvitationHandler) DeclineInviteByToken(c *gin.Context) { | ||
| var body struct { | ||
| Token string `json:"token" binding:"required"` | ||
| } | ||
| if err := c.ShouldBindJSON(&body); err != nil { | ||
| c.JSON(http.StatusBadRequest, gin.H{"error": "token required"}) | ||
| return | ||
| } | ||
| token := strings.TrimSpace(body.Token) | ||
| if token == "" { | ||
| c.JSON(http.StatusBadRequest, gin.H{"error": "token required"}) | ||
| return | ||
| } | ||
| inv, err := h.Winv.GetByToken(c.Request.Context(), token) | ||
| if err != nil || inv == nil { | ||
| c.Status(http.StatusNoContent) | ||
| return | ||
| } | ||
| if err := h.Winv.Delete(c.Request.Context(), inv.ID); err != nil { | ||
| c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to decline invite"}) | ||
| return | ||
| } | ||
|
martian56 marked this conversation as resolved.
|
||
| c.Status(http.StatusNoContent) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| package mail | ||
|
|
||
| import "log/slog" | ||
|
|
||
| // LogSendAttempt logs when an email send is about to be attempted. | ||
| // inviteURL is optional (e.g. workspace invite link); empty string is omitted from logs. | ||
| func LogSendAttempt(log *slog.Logger, to, subject, kind, inviteURL string) { | ||
| if log == nil { | ||
| return | ||
| } | ||
| attrs := []any{"to", to, "subject", subject, "kind", kind} | ||
| if inviteURL != "" { | ||
| attrs = append(attrs, "invite_url", inviteURL) | ||
| } | ||
| log.Info("mail send attempt", attrs...) | ||
|
martian56 marked this conversation as resolved.
|
||
| } | ||
|
|
||
| // LogSent logs successful email delivery. | ||
| func LogSent(log *slog.Logger, to, subject, inviteURL string) { | ||
| if log == nil { | ||
| return | ||
| } | ||
| attrs := []any{"to", to, "subject", subject} | ||
| if inviteURL != "" { | ||
| attrs = append(attrs, "invite_url", inviteURL) | ||
| } | ||
| log.Info("mail sent", attrs...) | ||
| } | ||
|
|
||
| // LogFailed logs a failed email send with error and optional invite URL. | ||
| func LogFailed(log *slog.Logger, to, subject, inviteURL string, err error) { | ||
| if log == nil { | ||
| return | ||
| } | ||
| attrs := []any{"to", to, "subject", subject, "error", err} | ||
| if inviteURL != "" { | ||
| attrs = append(attrs, "invite_url", inviteURL) | ||
| } | ||
| log.Error("mail send failed", attrs...) | ||
| } | ||
|
|
||
| // LogSkip logs when mail is skipped (e.g. not configured). | ||
| func LogSkip(log *slog.Logger, reason, to string, err error) { | ||
| if log == nil { | ||
| return | ||
| } | ||
| log.Warn("mail skip", "reason", reason, "to", to, "error", err) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| package mail | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "log/slog" | ||
| "net/smtp" | ||
| "strconv" | ||
| "strings" | ||
|
|
||
| "github.com/Devlaner/devlane/api/internal/crypto" | ||
| "github.com/Devlaner/devlane/api/internal/store" | ||
| ) | ||
|
|
||
| type smtpSettings struct { | ||
| Host string | ||
| Port int | ||
| SenderEmail string | ||
| Security string | ||
| Username string | ||
| Password string | ||
| } | ||
|
|
||
| func getEmailSettings(ctx context.Context, s *store.InstanceSettingStore) (*smtpSettings, error) { | ||
| row, err := s.Get(ctx, "email") | ||
| if err != nil || row == nil { | ||
| return nil, fmt.Errorf("email settings not found") | ||
| } | ||
| v := row.Value | ||
| if v == nil { | ||
| return nil, fmt.Errorf("email settings empty") | ||
| } | ||
| host, _ := v["host"].(string) | ||
| port := 587 | ||
| if p, ok := v["port"].(string); ok && p != "" { | ||
| if n, err := strconv.Atoi(p); err == nil { | ||
| port = n | ||
| } | ||
| } | ||
| if p, ok := v["port"].(float64); ok { | ||
| port = int(p) | ||
| } | ||
| sender, _ := v["sender_email"].(string) | ||
| security, _ := v["security"].(string) | ||
| username, _ := v["username"].(string) | ||
| passRaw, _ := v["password"].(string) | ||
| password := crypto.DecryptOrPlain(passRaw) | ||
| host = strings.TrimSpace(host) | ||
| if host == "" { | ||
| return nil, fmt.Errorf("email host not configured") | ||
| } | ||
| return &smtpSettings{ | ||
| Host: host, | ||
| Port: port, | ||
| SenderEmail: strings.TrimSpace(sender), | ||
| Security: strings.TrimSpace(security), | ||
| Username: strings.TrimSpace(username), | ||
| Password: password, | ||
| }, nil | ||
| } | ||
|
|
||
| // NewSMTPEmailSender returns a sender that loads SMTP config from instance "email" | ||
| // settings and sends mail. If not configured or send fails, logs and returns error. | ||
| func NewSMTPEmailSender(instanceSettings *store.InstanceSettingStore, log *slog.Logger) func(ctx context.Context, to, subject, body string) error { | ||
| return func(ctx context.Context, to, subject, body string) error { | ||
| cfg, err := getEmailSettings(ctx, instanceSettings) | ||
| if err != nil { | ||
| LogSkip(log, "instance email not configured", to, err) | ||
| return err | ||
| } | ||
| from := cfg.SenderEmail | ||
| if from == "" { | ||
| from = cfg.Username | ||
| } | ||
| if from == "" { | ||
| LogSkip(log, "sender_email and username empty", to, fmt.Errorf("sender not set")) | ||
| return fmt.Errorf("sender email not configured") | ||
| } | ||
| addr := fmt.Sprintf("%s:%d", cfg.Host, cfg.Port) | ||
| auth := smtp.PlainAuth("", cfg.Username, cfg.Password, cfg.Host) | ||
| msg := buildMessage(to, from, subject, body) | ||
| if err := smtp.SendMail(addr, auth, from, []string{to}, msg); err != nil { | ||
| LogFailed(log, to, subject, "", err) | ||
| return err | ||
| } | ||
| LogSent(log, to, subject, "") | ||
| return nil | ||
|
martian56 marked this conversation as resolved.
Outdated
|
||
| } | ||
|
martian56 marked this conversation as resolved.
|
||
| } | ||
|
|
||
| func buildMessage(to, from, subject, body string) []byte { | ||
| const crlf = "\r\n" | ||
| h := "To: " + to + crlf + | ||
| "From: " + from + crlf + | ||
| "Subject: " + subject + crlf + | ||
| "Content-Type: text/plain; charset=UTF-8" + crlf + | ||
| "MIME-Version: 1.0" + crlf + | ||
| crlf | ||
| return []byte(h + body) | ||
|
martian56 marked this conversation as resolved.
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.