change receiver field type

This commit is contained in:
rmanach 2023-09-09 16:07:08 +02:00
parent 2bc22b637d
commit 4d0076e625
2 changed files with 10 additions and 15 deletions

View File

@ -10,20 +10,11 @@ import (
type Email struct { type Email struct {
Path string Path string
Sender string `json:"sender"` Sender string `json:"sender"`
Receivers []string `json:"receivers"` Receivers string `json:"receivers"`
Subject string `json:"subject"` Subject string `json:"subject"`
Content string `json:"content"` Content string `json:"content"`
} }
func NewEmail(sender string, receivers []string, subject, content string) Email {
return Email{
Sender: sender,
Receivers: receivers,
Subject: subject,
Content: content,
}
}
func FromJSON(path string) (Email, error) { func FromJSON(path string) (Email, error) {
var mail Email var mail Email
@ -39,10 +30,14 @@ func FromJSON(path string) (Email, error) {
return mail, nil return mail, nil
} }
func (e Email) GetReceivers() []string {
return strings.Split(e.Receivers, ",")
}
func (e Email) Generate() []byte { func (e Email) Generate() []byte {
mail := fmt.Sprintf( mail := fmt.Sprintf(
"To: %s\nFrom: %s\nContent-Type: text/html;charset=utf-8\nSubject: %s\n\n%s", "To: %s\nFrom: %s\nContent-Type: text/html;charset=utf-8\nSubject: %s\n\n%s",
strings.Join(e.Receivers, ","), e.Receivers,
e.Sender, e.Sender,
e.Subject, e.Subject,
e.Content, e.Content,

View File

@ -42,7 +42,7 @@ func (s Sender) SendMail(email mail.Email) error {
auth := smtp.PlainAuth("", s.smtpConfig.User, s.smtpConfig.Password, s.smtpConfig.Url) auth := smtp.PlainAuth("", s.smtpConfig.User, s.smtpConfig.Password, s.smtpConfig.Url)
log.Debug().Msg("SMTP authentication succeed") log.Debug().Msg("SMTP authentication succeed")
if err := smtp.SendMail(s.smtpConfig.GetFullUrl(), auth, email.Sender, email.Receivers, email.Generate()); err != nil { if err := smtp.SendMail(s.smtpConfig.GetFullUrl(), auth, email.Sender, email.GetReceivers(), email.Generate()); err != nil {
log.Err(err).Msg("error while sending email") log.Err(err).Msg("error while sending email")
return err return err
} }