-
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
106 additions
and
2 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
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 |
---|---|---|
|
@@ -6,4 +6,7 @@ GRPC_SERVER_ADDRESS=0.0.0.0:9090 | |
TOKEN_SYMMETRIC_KEY=12345678901234567890123456789012 | ||
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=Simple Bank | ||
EMAIL_SENDER_ADDRESS=[email protected] | ||
EMAIL_SENDER_PASSWORD=prgctnuzmwqlkcrn |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package mail | ||
|
||
import ( | ||
"fmt" | ||
"net/smtp" | ||
|
||
"github.com/jordan-wright/email" | ||
) | ||
|
||
const ( | ||
smtpAuthAddress = "smtp.gmail.com" | ||
smtpServerAddress = "smtp.gmail.com:587" | ||
) | ||
|
||
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, fromEmailAddress string, fromEmailPassword string) EmailSender { | ||
return &GmailSender{ | ||
name: name, | ||
fromEmailAddress: fromEmailAddress, | ||
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) | ||
} | ||
} | ||
|
||
smtpAuth := smtp.PlainAuth("", sender.fromEmailAddress, sender.fromEmailPassword, smtpAuthAddress) | ||
return e.Send(smtpServerAddress, smtpAuth) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package mail | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/eizyc/simplebank/util" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestSendEmailWithGmail(t *testing.T) { | ||
if testing.Short() { | ||
t.Skip() | ||
} | ||
|
||
config, err := util.LoadConfig("..") | ||
require.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://eizyc.github.io/">Eizyc</a></p> | ||
` | ||
to := []string{"[email protected]"} | ||
attachFiles := []string{"../README.md"} | ||
|
||
err = sender.SendEmail(subject, content, to, nil, nil, attachFiles) | ||
require.NoError(t, err) | ||
} |
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