This repository has been archived by the owner on Aug 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: create mailer (go-mailer is used) * fix linter * sendGrid implementation * refactor: separate smtp and sendgrid * refactor: add sender name to config * fix newWithSMTP wrong parameter * mixed email content break down MailerConfig struct * resolve notes * resolve notes testing * testing * test cases * test cases * Merge branch 'feat/authentication-system' of https://github.com/reearth/reearth-backend into auth/mailer # Conflicts: # go.mod # internal/app/config.go * Merge branch 'feat/authentication-system' of https://github.com/reearth/reearth-backend into auth/mailer # Conflicts: # go.mod * swap plain and html contents * switch to alternative * remove unused functions * validate email and smtp url
- Loading branch information
Showing
10 changed files
with
376 additions
and
1 deletion.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package mailer | ||
|
||
import ( | ||
"github.com/reearth/reearth-backend/internal/usecase/gateway" | ||
"github.com/sendgrid/sendgrid-go" | ||
"github.com/sendgrid/sendgrid-go/helpers/mail" | ||
) | ||
|
||
type sendgridMailer struct { | ||
api string | ||
// sender data | ||
name string | ||
email string | ||
} | ||
|
||
func NewWithSendGrid(senderName, senderEmail, api string) gateway.Mailer { | ||
return &sendgridMailer{ | ||
name: senderName, | ||
email: senderEmail, | ||
api: api, | ||
} | ||
} | ||
|
||
func (m *sendgridMailer) SendMail(to []gateway.Contact, subject, plainContent, htmlContent string) error { | ||
contact := to[0] | ||
sender := mail.NewEmail(m.name, m.email) | ||
receiver := mail.NewEmail(contact.Name, contact.Email) | ||
message := mail.NewSingleEmail(sender, subject, receiver, plainContent, htmlContent) | ||
client := sendgrid.NewSendClient(m.api) | ||
_, err := client.Send(message) | ||
return 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package mailer | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/reearth/reearth-backend/internal/usecase/gateway" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestNewWithSendGrid(t *testing.T) { | ||
type args struct { | ||
senderName string | ||
senderEmail string | ||
api string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want gateway.Mailer | ||
}{ | ||
{ | ||
name: "should create a sendGrid mailer", | ||
args: args{ | ||
senderName: "test sender", | ||
senderEmail: "sender@test.com", | ||
api: "TEST_API", | ||
}, | ||
want: &sendgridMailer{ | ||
api: "TEST_API", | ||
name: "test sender", | ||
email: "sender@test.com", | ||
}, | ||
}, | ||
} | ||
for _, tc := range tests { | ||
t.Run(tc.name, func(tt *testing.T) { | ||
got := NewWithSendGrid(tc.args.senderName, tc.args.senderEmail, tc.args.api) | ||
assert.Equal(tt, tc.want, got) | ||
}) | ||
} | ||
} |
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,116 @@ | ||
package mailer | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"mime/multipart" | ||
"net/mail" | ||
"net/smtp" | ||
"net/textproto" | ||
"strings" | ||
|
||
"github.com/reearth/reearth-backend/internal/usecase/gateway" | ||
) | ||
|
||
type smtpMailer struct { | ||
host string | ||
port string | ||
username string | ||
password string | ||
} | ||
|
||
type message struct { | ||
to []string | ||
subject string | ||
plainContent string | ||
htmlContent string | ||
} | ||
|
||
func (m *message) encodeContent() (string, error) { | ||
buf := bytes.NewBuffer(nil) | ||
writer := multipart.NewWriter(buf) | ||
boundary := writer.Boundary() | ||
|
||
altBuffer, err := writer.CreatePart(textproto.MIMEHeader{"Content-Type": {"multipart/alternative; boundary=" + boundary}}) | ||
if err != nil { | ||
return "", err | ||
} | ||
altWriter := multipart.NewWriter(altBuffer) | ||
err = altWriter.SetBoundary(boundary) | ||
if err != nil { | ||
return "", err | ||
} | ||
var content io.Writer | ||
content, err = altWriter.CreatePart(textproto.MIMEHeader{"Content-Type": {"text/plain"}}) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
_, err = content.Write([]byte(m.plainContent + "\r\n\r\n")) | ||
if err != nil { | ||
return "", err | ||
} | ||
content, err = altWriter.CreatePart(textproto.MIMEHeader{"Content-Type": {"text/html"}}) | ||
if err != nil { | ||
return "", err | ||
} | ||
_, err = content.Write([]byte(m.htmlContent + "\r\n")) | ||
if err != nil { | ||
return "", err | ||
} | ||
_ = altWriter.Close() | ||
return buf.String(), nil | ||
} | ||
|
||
func (m *message) encodeMessage() ([]byte, error) { | ||
buf := bytes.NewBuffer(nil) | ||
buf.WriteString(fmt.Sprintf("Subject: %s\n", m.subject)) | ||
buf.WriteString(fmt.Sprintf("To: %s\n", strings.Join(m.to, ","))) | ||
content, err := m.encodeContent() | ||
if err != nil { | ||
return nil, err | ||
} | ||
buf.WriteString(content) | ||
|
||
return buf.Bytes(), nil | ||
} | ||
|
||
func NewWithSMTP(host, port, username, password string) gateway.Mailer { | ||
return &smtpMailer{ | ||
host: host, | ||
port: port, | ||
username: username, | ||
password: password, | ||
} | ||
} | ||
|
||
func (m *smtpMailer) SendMail(to []gateway.Contact, subject, plainContent, htmlContent string) error { | ||
emails := make([]string, 0, len(to)) | ||
for _, c := range to { | ||
_, err := mail.ParseAddress(c.Email) | ||
if err != nil { | ||
return fmt.Errorf("invalid email %s", c.Email) | ||
} | ||
emails = append(emails, c.Email) | ||
} | ||
|
||
msg := &message{ | ||
to: emails, | ||
subject: subject, | ||
plainContent: plainContent, | ||
htmlContent: htmlContent, | ||
} | ||
|
||
encodedMsg, err := msg.encodeMessage() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
auth := smtp.PlainAuth("", m.username, m.password, m.host) | ||
if len(m.host) == 0 { | ||
return errors.New("invalid smtp url") | ||
} | ||
return smtp.SendMail(m.host+":"+m.port, auth, m.username, emails, encodedMsg) | ||
} |
Oops, something went wrong.