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

@@ -4,6 +4,7 @@ import (
"crypto"
"crypto/x509"
"encoding/pem"
"regexp"
"strings"
"time"
@@ -13,6 +14,8 @@ import (
"maunium.net/go/mautrix/id"
)
var emailRegex = regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+\\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")
// MTA is mail transfer agent
type MTA interface {
Send(from, to, data string) error
@@ -46,6 +49,11 @@ type ContentOptions struct {
FromKey string
}
// AddressValid checks if email address is valid
func AddressValid(email string) bool {
return !emailRegex.MatchString(email)
}
// NewEmail constructs Email object
func NewEmail(messageID, inReplyTo, subject, from, to, text, html string, files []*File) *Email {
email := &Email{
@@ -71,6 +79,14 @@ func NewEmail(messageID, inReplyTo, subject, from, to, text, html string, files
return email
}
// Mailbox returns postmoogle's mailbox, parsing it from FROM (if local=false) or TO (local=true)
func (e *Email) Mailbox(local bool) string {
if local {
return Mailbox(e.To)
}
return Mailbox(e.From)
}
// Content converts the email object to a Matrix event content
func (e *Email) Content(threadID id.EventID, options *ContentOptions) *event.Content {
var text strings.Builder