feat: 0.0.1
This commit is contained in:
146 files changed
+10444
No files matched your search
@@ -0,0 +1,72 @@
|
||||
package mailer
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"net/smtp"
|
||||
"text/template"
|
||||
|
||||
"github.com/jordan-wright/email"
|
||||
)
|
||||
|
||||
const (
|
||||
smtpAuthAddress = "smtp.gmail.com"
|
||||
smtpServerAddress = "smtp.gmail.com:587"
|
||||
)
|
||||
|
||||
type GmailSender struct {
|
||||
name string
|
||||
fromEmailAddress string
|
||||
fromEmailPassword string
|
||||
}
|
||||
|
||||
func NewGmailSender(name, fromEmailAddress, fromEmailPassword string) MailSender {
|
||||
return &GmailSender{
|
||||
name: name,
|
||||
fromEmailAddress: fromEmailAddress,
|
||||
fromEmailPassword: fromEmailPassword,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *GmailSender) Send(
|
||||
mailTemplate string,
|
||||
vars any,
|
||||
to []string,
|
||||
cc []string,
|
||||
bcc []string,
|
||||
attachFiles []string,
|
||||
) error {
|
||||
e := email.NewEmail()
|
||||
e.From = fmt.Sprintf("%s <%s>", s.name, s.fromEmailAddress)
|
||||
e.To = to
|
||||
e.Cc = cc
|
||||
e.Bcc = bcc
|
||||
|
||||
tmpl, err := template.ParseFS(FS, "templates/"+mailTemplate)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to parse email template: %w", err)
|
||||
}
|
||||
|
||||
subject := new(bytes.Buffer)
|
||||
if err := tmpl.ExecuteTemplate(subject, "subject", vars); err != nil {
|
||||
return fmt.Errorf("failed to execute subject template: %w", err)
|
||||
}
|
||||
|
||||
body := new(bytes.Buffer)
|
||||
if err := tmpl.ExecuteTemplate(body, "body", vars); err != nil {
|
||||
return fmt.Errorf("failed to execute body template: %w", err)
|
||||
}
|
||||
|
||||
e.Subject = subject.String()
|
||||
e.HTML = body.Bytes()
|
||||
|
||||
for _, file := range attachFiles {
|
||||
_, err := e.AttachFile(file)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to attach file %s: %w", file, err)
|
||||
}
|
||||
}
|
||||
|
||||
smtpAuth := smtp.PlainAuth("", s.fromEmailAddress, s.fromEmailPassword, smtpAuthAddress)
|
||||
return e.Send(smtpServerAddress, smtpAuth)
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
// Package mailer provides functionalities for sending emails using predefined templates.
|
||||
package mailer
|
||||
|
||||
import "embed"
|
||||
|
||||
//go:embed "templates"
|
||||
var FS embed.FS
|
||||
|
||||
const (
|
||||
UserWelcomeTemplate = "user_activation.tmpl"
|
||||
TestEmailTemplate = "test_email.tmpl"
|
||||
)
|
||||
|
||||
type MailSender interface {
|
||||
Send(
|
||||
mailTemplate string,
|
||||
vars any,
|
||||
to []string,
|
||||
cc []string,
|
||||
bcc []string,
|
||||
attachFiles []string,
|
||||
) error
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package mailer
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/FernandoJVideira/simple_bank/internal/config"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestSendEmailWithGmail(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("skipping test in short mode.")
|
||||
}
|
||||
|
||||
config, err := config.LoadTestConfig("../..")
|
||||
require.NoError(t, err)
|
||||
|
||||
sender := NewGmailSender(config.EmailSenderName, config.EmailSender, config.EmailPassword)
|
||||
|
||||
vars := struct {
|
||||
ServiceName string
|
||||
}{
|
||||
ServiceName: "Simple Bank",
|
||||
}
|
||||
|
||||
to := []string{"[email protected]"}
|
||||
|
||||
attatchFiles := []string{"../README.md"}
|
||||
|
||||
err = sender.Send(TestEmailTemplate, vars, to, nil, nil, attatchFiles)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
{{define "subject"}} Test Email From {{.ServiceName}} {{end}}
|
||||
|
||||
{{define "body"}}
|
||||
<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<meta name="viewport" content="width=device-width" />
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
|
||||
</head>
|
||||
<body>
|
||||
<h1>Hello World!</h1>
|
||||
<p>This is a test email from {{.ServiceName}}.</p>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
{{end}}
|
||||
@@ -0,0 +1,22 @@
|
||||
{{define "subject"}} Finish Registration with {{.ServiceName}} {{end}}
|
||||
|
||||
{{define "body"}}
|
||||
<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<meta name="viewport" content="width=device-width" />
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
|
||||
</head>
|
||||
<body> <p>Hi {{.Username}},</p>
|
||||
<p>Thanks for signing up for {{.ServiceName}}. We're excited to have you on board!</p></p>
|
||||
<p>Before you can start using {{.ServiceName}}, you need to confirm your email address. Click the link below to confirm your email address:</p>
|
||||
<p><a href="{{.ActivationURL}}">{{.ActivationURL}}</a></p>
|
||||
<p>If you want to activate your account manually copy and paste the code from the link above</p>
|
||||
<p>If you didn't sign up for {{.ServiceName}}, you can safely ignore this email.</p>
|
||||
|
||||
<p>Thanks,</p>
|
||||
<p>The {{.ServiceName}} Team</p>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
{{end}}
|
||||
Reference in new issue
Block a user