use correct list of recipients on thread reply and in 'email has been sent' messages
This commit is contained in:
@@ -438,6 +438,7 @@ func (b *Bot) runSend(ctx context.Context) {
|
||||
from := mailbox + "@" + domain
|
||||
ID := email.MessageID(evt.ID, domain)
|
||||
for _, to := range tos {
|
||||
recipients := []string{to}
|
||||
eml := email.New(ID, "", " "+ID, subject, from, to, to, "", body, htmlBody, nil)
|
||||
data := eml.Compose(b.getBotSettings().DKIMPrivateKey())
|
||||
if data == "" {
|
||||
@@ -447,14 +448,14 @@ func (b *Bot) runSend(ctx context.Context) {
|
||||
queued, err := b.Sendmail(evt.ID, from, to, data)
|
||||
if queued {
|
||||
b.log.Error("cannot send email: %v", err)
|
||||
b.saveSentMetadata(ctx, queued, evt.ID, eml, &cfg)
|
||||
b.saveSentMetadata(ctx, queued, evt.ID, recipients, eml, &cfg)
|
||||
continue
|
||||
}
|
||||
if err != nil {
|
||||
b.Error(ctx, evt.RoomID, "cannot send email to %s: %v", to, err)
|
||||
continue
|
||||
}
|
||||
b.saveSentMetadata(ctx, false, evt.ID, eml, &cfg)
|
||||
b.saveSentMetadata(ctx, false, evt.ID, recipients, eml, &cfg)
|
||||
}
|
||||
if len(tos) > 1 {
|
||||
b.SendNotice(ctx, evt.RoomID, "All emails were sent.")
|
||||
|
||||
20
bot/email.go
20
bot/email.go
@@ -183,12 +183,12 @@ func (b *Bot) SendEmailReply(ctx context.Context) {
|
||||
|
||||
var queued bool
|
||||
var hasErr bool
|
||||
tos := append(email.AddressList(meta.CC), meta.To)
|
||||
for _, to := range tos {
|
||||
recipients := meta.Recipients()
|
||||
for _, to := range recipients {
|
||||
queued, err = b.Sendmail(evt.ID, meta.From, to, data)
|
||||
if queued {
|
||||
b.log.Error("cannot send email: %v", err)
|
||||
b.saveSentMetadata(ctx, queued, meta.ThreadID, eml, &cfg)
|
||||
b.saveSentMetadata(ctx, queued, meta.ThreadID, recipients, eml, &cfg)
|
||||
hasErr = true
|
||||
continue
|
||||
}
|
||||
@@ -201,7 +201,7 @@ func (b *Bot) SendEmailReply(ctx context.Context) {
|
||||
}
|
||||
|
||||
if !hasErr {
|
||||
b.saveSentMetadata(ctx, queued, meta.ThreadID, eml, &cfg)
|
||||
b.saveSentMetadata(ctx, queued, meta.ThreadID, recipients, eml, &cfg)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -267,6 +267,11 @@ func (e *parentEmail) fixtofrom(newSenderMailbox string, domains []string) {
|
||||
}
|
||||
}
|
||||
|
||||
// Recipients returns list of recipients (to, cc)
|
||||
func (e parentEmail) Recipients() []string {
|
||||
return append(email.AddressList(e.CC), e.To)
|
||||
}
|
||||
|
||||
func (b *Bot) getParentEvent(evt *event.Event) (id.EventID, *event.Event) {
|
||||
content := evt.Content.AsMessage()
|
||||
threadID := utils.EventParent(evt.ID, content)
|
||||
@@ -341,10 +346,11 @@ func (b *Bot) getParentEmail(evt *event.Event, newFromMailbox string) *parentEma
|
||||
|
||||
// saveSentMetadata used to save metadata from !pm sent and thread reply events to a separate notice message
|
||||
// because that metadata is needed to determine email thread relations
|
||||
func (b *Bot) saveSentMetadata(ctx context.Context, queued bool, threadID id.EventID, eml *email.Email, cfg *roomSettings) {
|
||||
text := "Email has been sent to " + eml.RcptTo
|
||||
func (b *Bot) saveSentMetadata(ctx context.Context, queued bool, threadID id.EventID, recipients []string, eml *email.Email, cfg *roomSettings) {
|
||||
addrs := strings.Join(recipients, ", ")
|
||||
text := "Email has been sent to " + addrs
|
||||
if queued {
|
||||
text = "Email to " + eml.RcptTo + " has been queued"
|
||||
text = "Email to " + addrs + " has been queued"
|
||||
}
|
||||
|
||||
evt := eventFromContext(ctx)
|
||||
|
||||
@@ -129,6 +129,11 @@ func (e *Email) Content(threadID id.EventID, options *ContentOptions) *event.Con
|
||||
parsed := format.RenderMarkdown(text.String(), true, true)
|
||||
parsed.RelatesTo = utils.RelatesTo(options.Threads, threadID)
|
||||
|
||||
var cc string
|
||||
if len(e.CC) > 0 {
|
||||
cc = strings.Join(e.CC, ", ")
|
||||
}
|
||||
|
||||
content := event.Content{
|
||||
Raw: map[string]interface{}{
|
||||
options.MessageIDKey: e.MessageID,
|
||||
@@ -138,7 +143,7 @@ func (e *Email) Content(threadID id.EventID, options *ContentOptions) *event.Con
|
||||
options.RcptToKey: e.RcptTo,
|
||||
options.FromKey: e.From,
|
||||
options.ToKey: e.To,
|
||||
options.CcKey: e.CC,
|
||||
options.CcKey: cc,
|
||||
},
|
||||
Parsed: &parsed,
|
||||
}
|
||||
|
||||
@@ -50,7 +50,7 @@ func EventParent(currentID id.EventID, content *event.MessageEventContent) id.Ev
|
||||
}
|
||||
|
||||
// EventField returns field value from raw event content
|
||||
func EventField[T comparable](content *event.Content, field string) T {
|
||||
func EventField[T any](content *event.Content, field string) T {
|
||||
var zero T
|
||||
raw := content.Raw[field]
|
||||
if raw == nil {
|
||||
|
||||
Reference in New Issue
Block a user