[#1069] added default Message-ID and more options to customize the mail message

This commit is contained in:
Gani Georgiev
2022-11-21 14:53:05 +02:00
parent c4a660d2d2
commit 3e1a19685b
11 changed files with 186 additions and 157 deletions
+13 -17
View File
@@ -3,10 +3,8 @@ package mailer
import (
"bytes"
"errors"
"io"
"mime"
"net/http"
"net/mail"
"os/exec"
)
@@ -20,19 +18,11 @@ type Sendmail struct {
}
// Send implements `mailer.Mailer` interface.
//
// Attachments are currently not supported.
func (m *Sendmail) Send(
fromEmail mail.Address,
toEmail mail.Address,
subject string,
htmlContent string,
attachments map[string]io.Reader,
) error {
func (c *Sendmail) Send(m *Message) error {
headers := make(http.Header)
headers.Set("Subject", mime.QEncoding.Encode("utf-8", subject))
headers.Set("From", fromEmail.String())
headers.Set("To", toEmail.String())
headers.Set("Subject", mime.QEncoding.Encode("utf-8", m.Subject))
headers.Set("From", m.From.String())
headers.Set("To", m.To.String())
headers.Set("Content-Type", "text/html; charset=UTF-8")
cmdPath, err := findSendmailPath()
@@ -50,12 +40,18 @@ func (m *Sendmail) Send(
if _, err := buffer.Write([]byte("\r\n")); err != nil {
return err
}
if _, err := buffer.Write([]byte(htmlContent)); err != nil {
return err
if m.HTML != "" {
if _, err := buffer.Write([]byte(m.HTML)); err != nil {
return err
}
} else {
if _, err := buffer.Write([]byte(m.Text)); err != nil {
return err
}
}
// ---
sendmail := exec.Command(cmdPath, toEmail.Address)
sendmail := exec.Command(cmdPath, m.To.Address)
sendmail.Stdin = &buffer
return sendmail.Run()