feat: SendRaw

This commit is contained in:
urko 2025-05-29 09:34:24 +02:00
parent b57eb95497
commit e04ed76fee
3 changed files with 24 additions and 9 deletions

View File

@ -16,7 +16,7 @@ func main() {
From: "your@email.com", From: "your@email.com",
}) })
emailService.SendEmail(email.EmailMessage{ emailService.SendEmail(email.MessageWithAttachments{
To: "other@email.com", To: "other@email.com",
Subject: "Test Email", Subject: "Test Email",
Body: "<html><body><p>Here your body, you can attach as html<p/></body></html>", Body: "<html><body><p>Here your body, you can attach as html<p/></body></html>",

View File

@ -31,13 +31,19 @@ type SecureConfig struct {
From string // Sender email address From string // Sender email address
} }
type EmailMessage struct { type MessageWithAttachments struct {
To string To string
Subject string Subject string
Body string Body string
Attachments []EmailAttachment Attachments []EmailAttachment
} }
type RawMessage struct {
To string
Subject string
Body string
}
type SMTPClientIface interface { type SMTPClientIface interface {
StartTLS(*tls.Config) error StartTLS(*tls.Config) error
Auth(a smtp.Auth) error Auth(a smtp.Auth) error
@ -205,7 +211,7 @@ func dialTLS(hostPort string, tlsConfig *tls.Config) (SMTPClientIface, error) {
return c, nil return c, nil
} }
func (e *EmailService) SendEmail(emailData EmailMessage) error { func (e *EmailService) SendEmail(emailData MessageWithAttachments) error {
msg, err := newMessage(e.from, emailData.To, emailData.Subject). msg, err := newMessage(e.from, emailData.To, emailData.Subject).
withAttachments(emailData.Body, emailData.Attachments) withAttachments(emailData.Body, emailData.Attachments)
@ -221,6 +227,15 @@ func (e *EmailService) SendEmail(emailData EmailMessage) error {
} }
} }
func (e *EmailService) SendRaw(emailData RawMessage) error {
switch e.port {
case "465":
return e.sendTLS(emailData.To, []byte(emailData.Body))
default:
return e.send(emailData.To, []byte(emailData.Body))
}
}
func (e *EmailService) send(to string, msg []byte) error { func (e *EmailService) send(to string, msg []byte) error {
c, err := e.dial(e.host + ":" + e.port) c, err := e.dial(e.host + ":" + e.port)
if err != nil { if err != nil {

View File

@ -45,7 +45,7 @@ func TestNewConfig_MissingEnvFile(t *testing.T) {
func TestMockSendEmail(t *testing.T) { func TestMockSendEmail(t *testing.T) {
service := NewMockMailService(func(params ...interface{}) {}) service := NewMockMailService(func(params ...interface{}) {})
emailData := EmailMessage{ emailData := MessageWithAttachments{
To: "test@example.com", To: "test@example.com",
Subject: "Test Email", Subject: "Test Email",
Body: "This is a test email.", Body: "This is a test email.",
@ -68,7 +68,7 @@ func TestNewInsecure(t *testing.T) {
}) })
t.Run("TestSendEmail", func(t *testing.T) { t.Run("TestSendEmail", func(t *testing.T) {
data := EmailMessage{ data := MessageWithAttachments{
To: cfg.MailTo, To: cfg.MailTo,
Subject: "Mail Sender", Subject: "Mail Sender",
Body: "Hello this is a test email", Body: "Hello this is a test email",
@ -89,7 +89,7 @@ func TestNewInsecure(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
defer reader3.Close() defer reader3.Close()
data := EmailMessage{ data := MessageWithAttachments{
To: cfg.MailTo, To: cfg.MailTo,
Subject: "Mail Sender", Subject: "Mail Sender",
Body: "Hello this is a test email", Body: "Hello this is a test email",
@ -120,7 +120,7 @@ func TestNewInsecure(t *testing.T) {
}) })
t.Run("TestSendEmail_InvalidRecipient", func(t *testing.T) { t.Run("TestSendEmail_InvalidRecipient", func(t *testing.T) {
data := EmailMessage{ data := MessageWithAttachments{
To: "invalid_email", To: "invalid_email",
Subject: "Test Email", Subject: "Test Email",
Body: "This is a test email.", Body: "This is a test email.",
@ -137,7 +137,7 @@ func TestNewInsecure(t *testing.T) {
Port: cfg.MailPort, Port: cfg.MailPort,
From: cfg.MailFrom, From: cfg.MailFrom,
}) })
data := EmailMessage{ data := MessageWithAttachments{
To: cfg.MailTo, To: cfg.MailTo,
Subject: "Test Email", Subject: "Test Email",
Body: "This is a test email.", Body: "This is a test email.",
@ -172,7 +172,7 @@ func TestSecure(t *testing.T) {
} }
emailService.dial = mockDialFn emailService.dial = mockDialFn
data := EmailMessage{ data := MessageWithAttachments{
To: cfg.MailTo, To: cfg.MailTo,
Subject: "Mail Sender", Subject: "Mail Sender",
Body: "Hello this is a test email", Body: "Hello this is a test email",