2019-03-02 15:22:20 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information
|
|
|
|
|
|
|
|
package mailservice
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
htmltemplate "html/template"
|
|
|
|
"path/filepath"
|
2019-05-17 13:29:35 +01:00
|
|
|
"sync"
|
2019-03-02 15:22:20 +00:00
|
|
|
|
2019-11-08 20:40:39 +00:00
|
|
|
"github.com/spacemonkeygo/monkit/v3"
|
2019-03-02 15:22:20 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2019-11-14 19:46:15 +00:00
|
|
|
"storj.io/storj/private/post"
|
2019-03-02 15:22:20 +00:00
|
|
|
)
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// Config defines values needed by mailservice service.
|
2019-03-02 15:22:20 +00:00
|
|
|
type Config struct {
|
|
|
|
SMTPServerAddress string `help:"smtp server address" default:""`
|
|
|
|
TemplatePath string `help:"path to email templates source" default:""`
|
|
|
|
From string `help:"sender email address" default:""`
|
2019-05-07 15:44:47 +01:00
|
|
|
AuthType string `help:"smtp authentication type" releaseDefault:"login" devDefault:"simulate"`
|
2019-03-24 00:08:41 +00:00
|
|
|
Login string `help:"plain/login auth user login" default:""`
|
|
|
|
Password string `help:"plain/login auth user password" default:""`
|
2019-03-02 15:22:20 +00:00
|
|
|
RefreshToken string `help:"refresh token used to retrieve new access token" default:""`
|
|
|
|
ClientID string `help:"oauth2 app's client id" default:""`
|
|
|
|
ClientSecret string `help:"oauth2 app's client secret" default:""`
|
|
|
|
TokenURI string `help:"uri which is used when retrieving new access token" default:""`
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
mon = monkit.Package()
|
|
|
|
)
|
|
|
|
|
2020-12-05 16:01:42 +00:00
|
|
|
// Sender sends emails.
|
2019-09-10 14:24:16 +01:00
|
|
|
//
|
|
|
|
// architecture: Service
|
2019-03-02 15:22:20 +00:00
|
|
|
type Sender interface {
|
2019-06-04 12:55:38 +01:00
|
|
|
SendEmail(ctx context.Context, msg *post.Message) error
|
2019-03-02 15:22:20 +00:00
|
|
|
FromAddress() post.Address
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// Message defines mailservice template-backed message for SendRendered method.
|
2019-03-02 15:22:20 +00:00
|
|
|
type Message interface {
|
|
|
|
Template() string
|
|
|
|
Subject() string
|
|
|
|
}
|
|
|
|
|
2020-12-05 16:01:42 +00:00
|
|
|
// Service sends template-backed email messages through SMTP.
|
2019-09-10 14:24:16 +01:00
|
|
|
//
|
|
|
|
// architecture: Service
|
2019-03-02 15:22:20 +00:00
|
|
|
type Service struct {
|
|
|
|
log *zap.Logger
|
|
|
|
sender Sender
|
|
|
|
|
|
|
|
html *htmltemplate.Template
|
|
|
|
// TODO(yar): prepare plain text version
|
2020-10-13 13:47:55 +01:00
|
|
|
// text *texttemplate.Template
|
2019-05-17 13:29:35 +01:00
|
|
|
|
|
|
|
sending sync.WaitGroup
|
2019-03-02 15:22:20 +00:00
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// New creates new service.
|
2019-03-02 15:22:20 +00:00
|
|
|
func New(log *zap.Logger, sender Sender, templatePath string) (*Service, error) {
|
|
|
|
var err error
|
|
|
|
service := &Service{log: log, sender: sender}
|
|
|
|
|
|
|
|
// TODO(yar): prepare plain text version
|
2020-10-13 13:47:55 +01:00
|
|
|
// service.text, err = texttemplate.ParseGlob(filepath.Join(templatePath, "*.txt"))
|
|
|
|
// if err != nil {
|
|
|
|
// return nil, err
|
|
|
|
// }
|
2019-03-02 15:22:20 +00:00
|
|
|
|
|
|
|
service.html, err = htmltemplate.ParseGlob(filepath.Join(templatePath, "*.html"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return service, nil
|
|
|
|
}
|
|
|
|
|
2019-05-17 13:29:35 +01:00
|
|
|
// Close closes and waits for any pending actions.
|
|
|
|
func (service *Service) Close() error {
|
|
|
|
service.sending.Wait()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// Send is generalized method for sending custom email message.
|
2019-03-02 15:22:20 +00:00
|
|
|
func (service *Service) Send(ctx context.Context, msg *post.Message) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-06-04 12:55:38 +01:00
|
|
|
return service.sender.SendEmail(ctx, msg)
|
2019-03-02 15:22:20 +00:00
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// SendRenderedAsync renders content from htmltemplate and texttemplate templates then sends it asynchronously.
|
2019-05-17 13:29:35 +01:00
|
|
|
func (service *Service) SendRenderedAsync(ctx context.Context, to []post.Address, msg Message) {
|
|
|
|
// TODO: think of a better solution
|
|
|
|
service.sending.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer service.sending.Done()
|
2019-11-28 10:29:48 +00:00
|
|
|
|
|
|
|
err := service.SendRendered(ctx, to, msg)
|
|
|
|
|
|
|
|
var recipients []string
|
|
|
|
for _, recipient := range to {
|
|
|
|
recipients = append(recipients, recipient.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
service.log.Error("fail sending email",
|
|
|
|
zap.Strings("recipients", recipients),
|
|
|
|
zap.Error(err))
|
|
|
|
} else {
|
|
|
|
service.log.Info("email sent successfully",
|
|
|
|
zap.Strings("recipients", recipients))
|
|
|
|
}
|
2019-05-17 13:29:35 +01:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// SendRendered renders content from htmltemplate and texttemplate templates then sends it.
|
2019-03-02 15:22:20 +00:00
|
|
|
func (service *Service) SendRendered(ctx context.Context, to []post.Address, msg Message) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
var htmlBuffer bytes.Buffer
|
|
|
|
var textBuffer bytes.Buffer
|
|
|
|
|
|
|
|
// TODO(yar): prepare plain text version
|
2020-10-13 13:47:55 +01:00
|
|
|
// if err = service.text.ExecuteTemplate(&textBuffer, msg.Template() + ".txt", msg); err != nil {
|
|
|
|
// return
|
|
|
|
// }
|
2019-03-02 15:22:20 +00:00
|
|
|
|
|
|
|
if err = service.html.ExecuteTemplate(&htmlBuffer, msg.Template()+".html", msg); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
m := &post.Message{
|
|
|
|
From: service.sender.FromAddress(),
|
|
|
|
To: to,
|
|
|
|
Subject: msg.Subject(),
|
|
|
|
PlainText: textBuffer.String(),
|
|
|
|
Parts: []post.Part{
|
|
|
|
{
|
|
|
|
Type: "text/html; charset=UTF-8",
|
|
|
|
Content: htmlBuffer.String(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-11-28 10:29:48 +00:00
|
|
|
return service.sender.SendEmail(ctx, m)
|
2019-03-02 15:22:20 +00:00
|
|
|
}
|