From b63fecd97396ade00a4a62e88191f1afb185d05b Mon Sep 17 00:00:00 2001 From: Cyberguru1 Date: Sat, 10 Aug 2024 05:40:05 -0500 Subject: [PATCH 1/3] chore: email-service-revamp --- services/send/email.go | 69 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 61 insertions(+), 8 deletions(-) diff --git a/services/send/email.go b/services/send/email.go index 298f53ea..578c8e5f 100644 --- a/services/send/email.go +++ b/services/send/email.go @@ -1,6 +1,7 @@ package send import ( + "crypto/tls" "fmt" "net/smtp" "strings" @@ -113,18 +114,70 @@ func (e *EmailRequest) sendEmailViaSMTP() error { subject := e.Subject recipients := e.To mime := "\nMIME-version: 1.0;\nContent-Type: text/html; charset=\"UTF-8\";\n\n" - body := []byte(subject + mime + e.Body) + body := []byte(fmt.Sprintf("From: %s\r\nTo: %s\r\n%s%s%s", sender, recipients[0], subject, mime, e.Body)) - err := smtp.SendMail( + conn, err := tls.Dial( + "tcp", mailConfig.Server+":"+mailConfig.Port, - auth, - sender, - recipients, - body, - ) + &tls.Config{ + InsecureSkipVerify: true, + ServerName: mailConfig.Server, + }) + + if err != nil { + + return fmt.Errorf("failed to connect to the server: %v", err) + + } + + defer conn.Close() + + client, err := smtp.NewClient(conn, mailConfig.Server) if err != nil { - return fmt.Errorf("error connecting to SMTP server: %w", err) + + return fmt.Errorf("failed to create SMTP client: %v", err) + } + + defer client.Quit() + + if err = client.Auth(auth); err != nil { + return fmt.Errorf("failed to authenticate: %v", err) + + } + + if err = client.Mail(sender); err != nil { + return fmt.Errorf("failed to set the sender: %v", err) + + } + + if err = client.Rcpt(recipients[0]); err != nil { + return fmt.Errorf("failed to set the recipient: %v", err) + + } + + writer, err := client.Data() + if err != nil { + + return fmt.Errorf("failed to write the message: %v", err) + + } + + _, err = writer.Write(body) + if err != nil { + + return fmt.Errorf("failed to send the message: %v", err) + + } + + err = writer.Close() + if err != nil { + + return fmt.Errorf("failed to close the writer: %v", err) + + } + return nil + } From a881b1695542ce2ee69b17f6cd11dc18d0669c86 Mon Sep 17 00:00:00 2001 From: Cyberguru1 Date: Sat, 10 Aug 2024 06:29:43 -0500 Subject: [PATCH 2/3] chore: fix insecure check --- services/send/email.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/send/email.go b/services/send/email.go index 578c8e5f..c9f3bc9b 100644 --- a/services/send/email.go +++ b/services/send/email.go @@ -120,7 +120,7 @@ func (e *EmailRequest) sendEmailViaSMTP() error { "tcp", mailConfig.Server+":"+mailConfig.Port, &tls.Config{ - InsecureSkipVerify: true, + InsecureSkipVerify: false, ServerName: mailConfig.Server, }) From db9a201f6cdad3c0388ae1cf743bc0d086d19e70 Mon Sep 17 00:00:00 2001 From: Cyberguru1 Date: Sat, 10 Aug 2024 06:44:53 -0500 Subject: [PATCH 3/3] chore: change sender email address --- services/send/email.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/services/send/email.go b/services/send/email.go index c9f3bc9b..2da3519b 100644 --- a/services/send/email.go +++ b/services/send/email.go @@ -112,9 +112,10 @@ func (e *EmailRequest) sendEmailViaSMTP() error { sender := mailConfig.Username subject := e.Subject + From := "telex@hng.email" recipients := e.To mime := "\nMIME-version: 1.0;\nContent-Type: text/html; charset=\"UTF-8\";\n\n" - body := []byte(fmt.Sprintf("From: %s\r\nTo: %s\r\n%s%s%s", sender, recipients[0], subject, mime, e.Body)) + body := []byte(fmt.Sprintf("From: %s\r\nTo: %s\r\n%s%s%s", From, recipients[0], subject, mime, e.Body)) conn, err := tls.Dial( "tcp",