do not add empty mime parts, fixes #51

This commit is contained in:
Aine
2022-11-17 23:11:11 +02:00
parent 99a89ef87a
commit 66bd1a4fab
3 changed files with 21 additions and 3 deletions

View File

@@ -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)

View File

@@ -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 {

View File

@@ -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)
}