send emails to multiple addresses at once

This commit is contained in:
Aine
2022-10-24 16:50:00 +03:00
parent a1feaff350
commit 45ff7597ed
2 changed files with 24 additions and 16 deletions

View File

@@ -33,7 +33,7 @@ so you can use it to send emails from your apps and scripts as well.
- [x] SMTP client - [x] SMTP client
- [x] SMTP server (you can use Postmoogle as general purpose SMTP server to send emails from your scripts or apps) - [x] SMTP server (you can use Postmoogle as general purpose SMTP server to send emails from your scripts or apps)
- [x] Send a message to matrix room with special format to send a new email - [x] Send a message to matrix room with special format to send a new email, even to multiple email addresses at once
- [ ] Reply to matrix thread sends reply into email thread - [ ] Reply to matrix thread sends reply into email thread
## Configuration ## Configuration

View File

@@ -337,11 +337,6 @@ func (b *Bot) runSend(ctx context.Context) {
return return
} }
if !utils.AddressValid(to) {
b.Error(ctx, evt.RoomID, "email address is not valid")
return
}
cfg, err := b.getRoomSettings(evt.RoomID) cfg, err := b.getRoomSettings(evt.RoomID)
if err != nil { if err != nil {
b.Error(ctx, evt.RoomID, "failed to retrieve room settings: %v", err) b.Error(ctx, evt.RoomID, "failed to retrieve room settings: %v", err)
@@ -354,16 +349,29 @@ func (b *Bot) runSend(ctx context.Context) {
return return
} }
tos := strings.Split(to, ",")
// validate first
for _, to := range tos {
if !utils.AddressValid(to) {
b.Error(ctx, evt.RoomID, "email address is not valid")
return
}
}
from := mailbox + "@" + b.domain from := mailbox + "@" + b.domain
ID := fmt.Sprintf("<%s@%s>", evt.ID, b.domain) ID := fmt.Sprintf("<%s@%s>", evt.ID, b.domain)
for _, to := range tos {
data := utils. data := utils.
NewEmail(ID, "", subject, from, to, body, "", nil). NewEmail(ID, "", subject, from, to, body, "", nil).
Compose(b.getBotSettings().DKIMPrivateKey()) Compose(b.getBotSettings().DKIMPrivateKey())
err = b.mta.Send(from, to, data) err = b.mta.Send(from, to, data)
if err != nil { if err != nil {
b.Error(ctx, evt.RoomID, "cannot send email: %v", err) b.Error(ctx, evt.RoomID, "cannot send email to %s: %v", to, err)
return } else {
b.SendNotice(ctx, evt.RoomID, "Email has been sent to "+to)
}
}
if len(tos) > 1 {
b.SendNotice(ctx, evt.RoomID, "All emails were sent.")
} }
b.SendNotice(ctx, evt.RoomID, "Email has been sent")
} }