Skip to content

Commit

Permalink
created the send service with mail go package
Browse files Browse the repository at this point in the history
  • Loading branch information
morka17 committed Jul 7, 2023
1 parent 7414724 commit 01f2136
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,6 @@ redis:


test:
go test -v -cover ./...
go test -v -cover -short ./...

.PHONY: postgres mockgenstore createdb db_docs db_schema dropdb migrateup migrateup1 migratedown1 migratedown sqlc test server proto
5 changes: 4 additions & 1 deletion app.env
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ GRPC_Server_Address = :9090
TOKEN_SYMMETRIC_KEY = 12344563454624235362365474356564
ACCESS_TOKEN_DURATION=15m
REFRESH_TOKEN_DURATION=24h
REDIS_ADDRESS=0.0.0.0:6379
REDIS_ADDRESS=0.0.0.0:6379
EMAIL_SENDER_NAME=Shiny bank
EMAIL_SENDER_ADDRESS=morkajoshua50@gmail.com
EMAIL_SENDER_PASSWORD = zndwhrxsufitpbay
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ require (
github.com/google/uuid v1.3.0
github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0
github.com/hibiken/asynq v0.24.1
github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible
github.com/o1egl/paseto v1.0.0
github.com/rakyll/statik v0.1.7
github.com/rs/zerolog v1.29.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ github.com/hibiken/asynq v0.24.1 h1:+5iIEAyA9K/lcSPvx3qoPtsKJeKI5u9aOIvUmSsazEw=
github.com/hibiken/asynq v0.24.1/go.mod h1:u5qVeSbrnfT+vtG5Mq8ZPzQu/BmCKMHvTGb91uy9Tts=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible h1:jdpOPRN1zP63Td1hDQbZW73xKmzDvZHzVdNYxhnTMDA=
github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible/go.mod h1:1c7szIrayyPPB/987hsnvNzLushdWf4o/79s3P08L8A=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
Expand Down
67 changes: 67 additions & 0 deletions src/mail/sender.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package mail

import (
"fmt"
"net/smtp"

"github.com/jordan-wright/email"
)

const (
smtpAuthAddress = "smtp.gmail.com"
smtpServerAddress = "smtp.gmail.com:465"
)


type EmailSender interface {
SendEmail(
subject string,
content string,
to []string,
cc []string,
bcc []string,
attachFiles []string,
) error
}

type GmailSender struct {
name string
fromEmailAddress string
fromEmailPassword string
}

func NewGmailSender(name string, fromEmailAdress string, fromEmailPassword string) EmailSender {
return &GmailSender{
name: name,
fromEmailAddress: fromEmailAdress,
fromEmailPassword: fromEmailPassword,
}
}

func (sender *GmailSender) SendEmail(
subject string,
content string,
to []string,
cc []string,
bcc []string,
attachFiles []string,
) error {

e := email.NewEmail()
e.From = fmt.Sprintf("%s <%s>", sender.name, sender.fromEmailAddress)
e.Subject = subject
e.HTML = []byte(content)
e.To = to
e.Cc = cc
e.Bcc = bcc

for _, f := range attachFiles {
_, err := e.AttachFile(f)
if err != nil {
return fmt.Errorf("Failed to attach file %s: %w", f, err)
}
}

smptAuth := smtp.PlainAuth("identity string", sender.fromEmailAddress, sender.fromEmailPassword, smtpAuthAddress)
return e.Send(smtpServerAddress, smptAuth)
}
31 changes: 31 additions & 0 deletions src/mail/sender_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package mail

import (
"testing"

"github.com/morka17/shiny_bank/v1/src/utils"
"github.com/stretchr/testify/assert"
)

func TestSendEmailWithGmail(t *testing.T) {

if testing.Short() {
t.Skip()
}

config, err := utils.LoadConfig("../../")
assert.NoError(t, err)

sender := NewGmailSender(config.EmailSenderName, config.EmailSenderAddress, config.EmailSenderPassword)

subject := "A test email"
content := `
<h1>Hello world</h1>
<p>This is a test message from <a href="https://github.com/morka17/api_bank> API Bank</a></p>
`
to := []string{config.EmailSenderAddress}
attachFiles :=[]string{"../../readme.md"}

err = sender.SendEmail(subject, content, to, nil, nil, attachFiles)
assert.NoError(t, err)
}
3 changes: 3 additions & 0 deletions src/utils/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ type Config struct {
TokenSymmetricKey string `mapstructure:"TOKEN_SYMMETRIC_KEY"`
AccessTokenDuration time.Duration `mapstructure:"ACCESS_TOKEN_DURATION"`
RefreshTokenDuration time.Duration `mapstructure:"REFRESH_TOKEN_DURATION"`
EmailSenderName string `mapstructure:"EMAIL_SENDER_NAME"`
EmailSenderAddress string `mapstructure:"EMAIL_SENDER_ADDRESS"`
EmailSenderPassword string `mapstructure:"EMAIL_SENDER_PASSWORD"`
}

// LoadConfig reads configuration from file or environment variables.
Expand Down
3 changes: 2 additions & 1 deletion src/worker/distributor.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ func (processor *RedisTaskProcessor) ProcessTaskSendVerifyEmail(ctx context.Cont
user, err := processor.store.GetUser(ctx, payload.Username)
if err != nil {
if err == sql.ErrNoRows {
return fmt.Errorf("user doesn't exist: %v", asynq.SkipRetry)
// return fmt.Errorf("user doesn't exist: %v", asynq.SkipRetry)
return fmt.Errorf("user doesn't exist: %v", err)
}
return fmt.Errorf("Failed to get user: %w", err)
}
Expand Down

0 comments on commit 01f2136

Please sign in to comment.