diff --git a/services/send/email.go b/services/send/email.go index 298f53ea..2da3519b 100644 --- a/services/send/email.go +++ b/services/send/email.go @@ -1,6 +1,7 @@ package send import ( + "crypto/tls" "fmt" "net/smtp" "strings" @@ -111,20 +112,73 @@ 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(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)) - err := smtp.SendMail( + conn, err := tls.Dial( + "tcp", mailConfig.Server+":"+mailConfig.Port, - auth, - sender, - recipients, - body, - ) + &tls.Config{ + InsecureSkipVerify: false, + 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 + }