From 45ff7597ed87548ba54141c946ec2766f409ee5b Mon Sep 17 00:00:00 2001 From: Aine Date: Mon, 24 Oct 2022 16:50:00 +0300 Subject: [PATCH] send emails to multiple addresses at once --- README.md | 2 +- bot/command.go | 38 +++++++++++++++++++++++--------------- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index dcb1ef8..ea2098a 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ so you can use it to send emails from your apps and scripts as well. - [x] SMTP client - [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 ## Configuration diff --git a/bot/command.go b/bot/command.go index 2c48a1f..82888e2 100644 --- a/bot/command.go +++ b/bot/command.go @@ -337,11 +337,6 @@ func (b *Bot) runSend(ctx context.Context) { return } - if !utils.AddressValid(to) { - b.Error(ctx, evt.RoomID, "email address is not valid") - return - } - cfg, err := b.getRoomSettings(evt.RoomID) if err != nil { b.Error(ctx, evt.RoomID, "failed to retrieve room settings: %v", err) @@ -354,16 +349,29 @@ func (b *Bot) runSend(ctx context.Context) { return } - from := mailbox + "@" + b.domain - ID := fmt.Sprintf("<%s@%s>", evt.ID, b.domain) - data := utils. - NewEmail(ID, "", subject, from, to, body, "", nil). - Compose(b.getBotSettings().DKIMPrivateKey()) - err = b.mta.Send(from, to, data) - if err != nil { - b.Error(ctx, evt.RoomID, "cannot send email: %v", err) - 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 + } } - b.SendNotice(ctx, evt.RoomID, "Email has been sent") + from := mailbox + "@" + b.domain + ID := fmt.Sprintf("<%s@%s>", evt.ID, b.domain) + for _, to := range tos { + data := utils. + NewEmail(ID, "", subject, from, to, body, "", nil). + Compose(b.getBotSettings().DKIMPrivateKey()) + err = b.mta.Send(from, to, data) + if err != nil { + b.Error(ctx, evt.RoomID, "cannot send email to %s: %v", to, err) + } 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.") + } }