Skip to content
This repository has been archived by the owner on Dec 23, 2023. It is now read-only.

Commit

Permalink
fix: include mime version and message id headers (resolve #36)
Browse files Browse the repository at this point in the history
  • Loading branch information
muety committed Jan 2, 2023
1 parent 01fbadc commit df44f7f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
21 changes: 15 additions & 6 deletions types/mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package types

import (
"fmt"
"github.com/google/uuid"
"strings"
"time"
)
Expand All @@ -12,12 +13,13 @@ const (
)

type Mail struct {
From MailAddress
To MailAddresses
Subject string
Body string
Type string
Date time.Time
From MailAddress
To MailAddresses
Subject string
Body string
Type string
Date time.Time
MessageID string
}

func (m *Mail) WithText(text string) *Mail {
Expand All @@ -39,20 +41,27 @@ func (m *Mail) Sanitized() *Mail {
if m.Date.IsZero() {
m.Date = time.Now()
}
if m.MessageID == "" {
m.MessageID = fmt.Sprintf("<%s@%s>", uuid.New().String(), m.From.Domain())
}
return m
}

func (m *Mail) String() string {
return fmt.Sprintf("To: %s\r\n"+
"From: %s\r\n"+
"Subject: %s\r\n"+
"Message-ID: %s\r\n"+
"MIME-Version: 1.0\r\n"+
"Content-Type: %s\r\n"+
"Content-Transfer-Encoding: 8bit\r\n"+
"Date: %s\r\n"+
"\r\n"+
"%s\r\n",
strings.Join(m.To.Strings(), ", "),
m.From.String(),
m.Subject,
m.MessageID,
m.Type,
m.Date.Format(time.RFC1123Z),
m.Body,
Expand Down
13 changes: 12 additions & 1 deletion types/mail_address.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package types

import "regexp"
import (
"regexp"
"strings"
)

const (
MailPattern = "[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+"
Expand Down Expand Up @@ -41,6 +44,14 @@ func (m MailAddress) Raw() string {
return ""
}

func (m MailAddress) Domain() string {
split := strings.Split(m.Raw(), "@")
if len(split) != 2 {
return ""
}
return split[1]
}

func (m MailAddress) Valid() bool {
return emailAddrRegex.Match([]byte(m))
}
Expand Down
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.13.1
0.13.2

0 comments on commit df44f7f

Please sign in to comment.