diff --git a/examples/main.go b/examples/main.go index c763df6..894284e 100644 --- a/examples/main.go +++ b/examples/main.go @@ -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: "

Here your body, you can attach as html

", diff --git a/pkg/email/email.go b/pkg/email/email.go index 8bb9031..076d5be 100644 --- a/pkg/email/email.go +++ b/pkg/email/email.go @@ -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 { diff --git a/pkg/email/email_test.go b/pkg/email/email_test.go index fba5296..b9bebf4 100755 --- a/pkg/email/email_test.go +++ b/pkg/email/email_test.go @@ -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",