-
Notifications
You must be signed in to change notification settings - Fork 0
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
7 changed files
with
136 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,3 +19,4 @@ | |
|
||
# Go workspace file | ||
go.work | ||
cmd/newsletter/newsletter |
Binary file not shown.
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 |
---|---|---|
@@ -1,31 +1,71 @@ | ||
package newsletter | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log/slog" | ||
"net/smtp" | ||
) | ||
|
||
// SMTPServer is the SMTP server of gmail | ||
const SMTPServer = "smtp.gmail.com" | ||
|
||
// EmailConfig contains the necessary information to authenticate in the SMTP server | ||
type EmailConfig struct { | ||
Password string | ||
UserName string | ||
} | ||
|
||
// SMTPServer is the SMTP server of gmail | ||
const SMTPServer = "smtp.gmail.com" | ||
type MailClient struct { | ||
cfg EmailConfig | ||
} | ||
|
||
func NewMailClient(cfg EmailConfig) *MailClient { | ||
return &MailClient{ | ||
cfg: cfg, | ||
} | ||
} | ||
|
||
// Send sends an email to the given destination | ||
func Send(dest []string, bodyMessage string, cfg EmailConfig) error { | ||
auth := smtp.PlainAuth("", cfg.UserName, cfg.Password, SMTPServer) | ||
func (m MailClient) Send(dest []string, bodyMessage string) error { | ||
auth := smtp.PlainAuth("", m.cfg.UserName, m.cfg.Password, SMTPServer) | ||
|
||
msg := []byte("To: " + dest[0] + "\r\n" + | ||
"Subject: Newsletter\r\n" + | ||
"\r\n" + | ||
bodyMessage + "\r\n") | ||
|
||
err := smtp.SendMail(SMTPServer+":587", auth, cfg.UserName, dest, []byte(msg)) | ||
err := smtp.SendMail(SMTPServer+":587", auth, m.cfg.UserName, dest, []byte(msg)) | ||
if err != nil { | ||
return fmt.Errorf("error sending email: %v", err) | ||
} | ||
return nil | ||
} | ||
|
||
func EmailTrigger(ctx context.Context, s Storage, e Email) error { | ||
nl, err := s.Newsletter() | ||
if err != nil { | ||
return fmt.Errorf("error getting newsletter: %v", err) | ||
} | ||
|
||
for _, n := range nl { | ||
pages, err := s.PageIn(ctx, n.URLs) | ||
if err != nil { | ||
slog.Error("error getting pages", "error", err) | ||
} | ||
var validURLS []string | ||
for _, p := range pages { | ||
if p.IsMostRecent { | ||
validURLS = append(validURLS, p.URL) | ||
} | ||
} | ||
if len(validURLS) > 0 { | ||
err = e.Send([]string{n.UserEmail}, fmt.Sprintf("Hi %s, \n\nWe have found %d new articles for you: \n\n%s", n.UserEmail, len(validURLS), validURLS)) | ||
if err != nil { | ||
slog.Error("error sending email", "error", err) | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} |
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