use postmoogle as general purpose SMTP server and allow other apps or scripts to send emails through it

This commit is contained in:
Aine
2022-09-22 18:21:17 +03:00
parent c9c871287d
commit 070a6ffc76
11 changed files with 108 additions and 20 deletions

View File

@@ -2,10 +2,13 @@ package smtp
import (
"context"
"errors"
"github.com/emersion/go-smtp"
"github.com/getsentry/sentry-go"
"gitlab.com/etke.cc/go/logger"
"gitlab.com/etke.cc/postmoogle/utils"
)
// msa is mail submission agent, implements smtp.Backend
@@ -13,11 +16,15 @@ type msa struct {
log *logger.Logger
domain string
bot Bot
mta utils.MTA
}
func (m *msa) newSession() *msasession {
func (m *msa) newSession(from string, local bool) *msasession {
return &msasession{
ctx: sentry.SetHubOnContext(context.Background(), sentry.CurrentHub().Clone()),
mta: m.mta,
from: from,
local: local,
log: m.log,
bot: m.bot,
domain: m.domain,
@@ -25,9 +32,18 @@ func (m *msa) newSession() *msasession {
}
func (m *msa) Login(state *smtp.ConnectionState, username, password string) (smtp.Session, error) {
return nil, smtp.ErrAuthUnsupported
if !utils.AddressValid(username) {
return nil, errors.New("please, provide email address")
}
mailbox := utils.Mailbox(username)
if !m.bot.AllowAuth(mailbox, password) {
return nil, errors.New("email or password is invalid")
}
return m.newSession(username, false), nil
}
func (m *msa) AnonymousLogin(state *smtp.ConnectionState) (smtp.Session, error) {
return m.newSession(), nil
return m.newSession("", true), nil
}