-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
851 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package service | ||
|
||
import ( | ||
"fmt" | ||
"net/smtp" | ||
"strings" | ||
"sync" | ||
|
||
"github.com/Lukmanern/gost/internal/env" | ||
"github.com/Lukmanern/gost/internal/helper" | ||
) | ||
|
||
type EmailService interface { | ||
// SendMail func sends message with subject to some emails address. | ||
SendMail(subject, message string, emails ...string) error | ||
} | ||
|
||
type EmailServiceImpl struct { | ||
Server string | ||
Port int | ||
Email string | ||
Password string | ||
SmptAuth smtp.Auth | ||
SmptMime string | ||
SmptAddr string | ||
} | ||
|
||
var ( | ||
emailService *EmailServiceImpl | ||
emailServiceOnce sync.Once | ||
) | ||
|
||
func NewEmailService() EmailService { | ||
emailServiceOnce.Do(func() { | ||
config := env.Configuration() | ||
emailService = &EmailServiceImpl{ | ||
Server: config.SMTPServer, | ||
Port: config.SMTPPort, | ||
Email: config.SMTPEmail, | ||
Password: config.SMTPPassword, | ||
} | ||
|
||
emailService.SmptAuth = smtp.PlainAuth("", emailService.Email, emailService.Password, emailService.Server) | ||
emailService.SmptAddr = fmt.Sprintf("%s:%d", emailService.Server, emailService.Port) | ||
emailService.SmptMime = "MIME-version: 1.0;\nContent-Type: text/html; charset=\"UTF-8\";\r\n" | ||
}) | ||
|
||
return emailService | ||
} | ||
|
||
func (svc *EmailServiceImpl) SendMail(subject, message string, emails ...string) error { | ||
validateErr := helper.ValidateEmails(emails...) | ||
if validateErr != nil { | ||
return validateErr | ||
} | ||
body := "From: " + "CONFIG_SENDER_NAME" + "\n" + | ||
"To: " + strings.Join(emails, ",") + "\n" + | ||
"Subject: " + subject + "\n" + svc.SmptMime + "\n\n" + | ||
message | ||
|
||
err := smtp.SendMail(svc.SmptAddr, svc.SmptAuth, svc.Email, emails, []byte(body)) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
Oops, something went wrong.