40 lines
		
	
	
		
			984 B
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			40 lines
		
	
	
		
			984 B
		
	
	
	
		
			Go
		
	
	
	
	
	
| package services
 | |
| 
 | |
| import (
 | |
| 	cfg "mailsrv/config"
 | |
| 	"mailsrv/mail"
 | |
| 
 | |
| 	"github.com/go-kit/kit/log"
 | |
| 	"github.com/go-kit/kit/log/level"
 | |
| 	"net/smtp"
 | |
| )
 | |
| 
 | |
| type Sender struct {
 | |
| 	SMTPConfig cfg.SMTPConfig
 | |
| 	logger     log.Logger
 | |
| 	// fetch this directory to collect `.json` e-mail format
 | |
| 	OutboxPath string
 | |
| }
 | |
| 
 | |
| func NewSender(logger log.Logger, config cfg.SMTPConfig, outboxPath string) Sender {
 | |
| 	logger = log.With(logger, "actor", "sender")
 | |
| 	return Sender{
 | |
| 		SMTPConfig: config,
 | |
| 		logger:     logger,
 | |
| 		OutboxPath: outboxPath,
 | |
| 	}
 | |
| }
 | |
| 
 | |
| func (s Sender) SendMail(email mail.Email) error {
 | |
| 	auth := smtp.PlainAuth("", s.SMTPConfig.User, s.SMTPConfig.Password, s.SMTPConfig.Url)
 | |
| 	s.logger.Log("action", "authentication succeed")
 | |
| 
 | |
| 	if err := smtp.SendMail(s.SMTPConfig.GetFullUrl(), auth, email.Sender, email.Receivers, email.Generate()); err != nil {
 | |
| 		level.Error(s.logger).Log("msg", "error while sending email", "err", err)
 | |
| 		return err
 | |
| 	}
 | |
| 
 | |
| 	s.logger.Log("msg", "mail send successfully")
 | |
| 	return nil
 | |
| }
 |