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",
})
emailService.SendEmail(email.EmailMessage{
emailService.SendEmail(email.MessageWithAttachments{
To: "other@email.com",
Subject: "Test Email",
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
}
type EmailMessage struct {
type MessageWithAttachments struct {
To string
Subject string
Body string
Attachments []EmailAttachment
}
type RawMessage struct {
To string
Subject string
Body string
}
type SMTPClientIface interface {
StartTLS(*tls.Config) error
Auth(a smtp.Auth) error
@ -205,7 +211,7 @@ func dialTLS(hostPort string, tlsConfig *tls.Config) (SMTPClientIface, error) {
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).
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 {
c, err := e.dial(e.host + ":" + e.port)
if err != nil {

View File

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