From 988df8e729b9cdde796a71e970ef33165ad16cd7 Mon Sep 17 00:00:00 2001 From: jojo Date: Sun, 21 Jan 2024 15:19:50 -0300 Subject: [PATCH] :sparkles: mail feature --- mail.go | 31 +++++++++++++++++++++++++++++++ mail_test.go | 1 + scrape_test.go | 18 +++++------------- 3 files changed, 37 insertions(+), 13 deletions(-) create mode 100644 mail.go create mode 100644 mail_test.go diff --git a/mail.go b/mail.go new file mode 100644 index 0000000..630d3f1 --- /dev/null +++ b/mail.go @@ -0,0 +1,31 @@ +package newsletter + +import ( + "fmt" + "net/smtp" +) + +// 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" + +// 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) + + 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)) + if err != nil { + return fmt.Errorf("error sending email: %v", err) + } + return nil +} diff --git a/mail_test.go b/mail_test.go new file mode 100644 index 0000000..68615a8 --- /dev/null +++ b/mail_test.go @@ -0,0 +1 @@ +package newsletter diff --git a/scrape_test.go b/scrape_test.go index 16278d4..a40dc3b 100644 --- a/scrape_test.go +++ b/scrape_test.go @@ -79,7 +79,7 @@ func TestCrawlerRun(t *testing.T) { } } -func TestGetReferences(t *testing.T) { +func TestFetch(t *testing.T) { wantBody := "Hello, World!" server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) @@ -99,7 +99,7 @@ func TestGetReferences(t *testing.T) { } } -func TestGetReferences_Status500(t *testing.T) { +func TestFetch_Status500(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusInternalServerError) })) @@ -116,21 +116,13 @@ func TestGetReferences_Status500(t *testing.T) { } } -type StorageMockImpl struct { -} - -func NewStorageMock() StorageMockImpl { - return StorageMockImpl{} -} - -func (s StorageMockImpl) SavePage(_ context.Context, _ []mongodb.Page) error { - return nil -} +type StorageMockImpl struct{} +func NewStorageMock() StorageMockImpl { return StorageMockImpl{} } +func (s StorageMockImpl) SavePage(_ context.Context, _ []mongodb.Page) error { return nil } func (s StorageMockImpl) DistinctEngineerURLs(_ context.Context) ([]interface{}, error) { return []interface{}{fakeURL}, nil } - func (s StorageMockImpl) Page(_ context.Context, _ string) ([]mongodb.Page, error) { return []mongodb.Page{}, nil }