diff --git a/bot/command.go b/bot/command.go index 74e352b..aebfcc9 100644 --- a/bot/command.go +++ b/bot/command.go @@ -430,6 +430,10 @@ func (b *Bot) runSend(ctx context.Context) { for _, to := range tos { email := utils.NewEmail(ID, "", " "+ID, subject, from, to, body, htmlBody, nil) data := email.Compose(b.getBotSettings().DKIMPrivateKey()) + if data == "" { + b.SendError(ctx, evt.RoomID, "email body is empty") + return + } queued, err := b.Sendmail(evt.ID, from, to, data) if queued { b.log.Error("cannot send email: %v", err) diff --git a/bot/email.go b/bot/email.go index a2c521d..bb9e772 100644 --- a/bot/email.go +++ b/bot/email.go @@ -180,6 +180,10 @@ func (b *Bot) SendEmailReply(ctx context.Context) { b.log.Debug("send email reply: %+v", meta) email := utils.NewEmail(meta.MessageID, meta.InReplyTo, meta.References, meta.Subject, meta.From, meta.To, body, htmlBody, nil) data := email.Compose(b.getBotSettings().DKIMPrivateKey()) + if data == "" { + b.SendError(ctx, evt.RoomID, "email body is empty") + return + } queued, err := b.Sendmail(evt.ID, meta.From, meta.To, data) if queued { diff --git a/utils/email.go b/utils/email.go index f90206f..8fe523b 100644 --- a/utils/email.go +++ b/utils/email.go @@ -143,13 +143,23 @@ func (e *Email) Content(threadID id.EventID, options *ContentOptions) *event.Con // Compose converts the email object to a string (to be used for delivery via SMTP) and possibly DKIM-signs it func (e *Email) Compose(privkey string) string { + textSize := len(e.Text) + htmlSize := len(e.HTML) + if textSize == 0 && htmlSize == 0 { + return "" + } + mail := enmime.Builder(). From("", e.From). To("", e.To). Header("Message-Id", e.MessageID). - Subject(e.Subject). - Text([]byte(e.Text)). - HTML([]byte(e.HTML)) + Subject(e.Subject) + if textSize > 0 { + mail = mail.Text([]byte(e.Text)) + } + if htmlSize > 0 { + mail = mail.HTML([]byte(e.HTML)) + } if e.InReplyTo != "" { mail = mail.Header("In-Reply-To", e.InReplyTo) }