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"
|
2021-05-24 20:33:02 +01:00
|
|
|
"time"
|
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"
|
|
|
|
|
2021-05-24 20:33:02 +01:00
|
|
|
"storj.io/common/context2"
|
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 {
|
testplanet/satellite: reduce the number of places default values need to be configured
Satellites set their configuration values to default values using
cfgstruct, however, it turns out our tests don't test these values
at all! Instead, they have a completely separate definition system
that is easy to forget about.
As is to be expected, these values have drifted, and it appears
in a few cases test planet is testing unreasonable values that we
won't see in production, or perhaps worse, features enabled in
production were missed and weren't enabled in testplanet.
This change makes it so all values are configured the same,
systematic way, so it's easy to see when test values are different
than dev values or release values, and it's less hard to forget
to enable features in testplanet.
In terms of reviewing, this change should be actually fairly
easy to review, considering private/testplanet/satellite.go keeps
the current config system and the new one and confirms that they
result in identical configurations, so you can be certain that
nothing was missed and the config is all correct.
You can also check the config lock to see what actual config
values changed.
Change-Id: I6715d0794887f577e21742afcf56fd2b9d12170e
2021-05-31 22:15:00 +01:00
|
|
|
SMTPServerAddress string `help:"smtp server address" default:"" testDefault:"smtp.mail.test:587"`
|
2019-03-02 15:22:20 +00:00
|
|
|
TemplatePath string `help:"path to email templates source" default:""`
|
testplanet/satellite: reduce the number of places default values need to be configured
Satellites set their configuration values to default values using
cfgstruct, however, it turns out our tests don't test these values
at all! Instead, they have a completely separate definition system
that is easy to forget about.
As is to be expected, these values have drifted, and it appears
in a few cases test planet is testing unreasonable values that we
won't see in production, or perhaps worse, features enabled in
production were missed and weren't enabled in testplanet.
This change makes it so all values are configured the same,
systematic way, so it's easy to see when test values are different
than dev values or release values, and it's less hard to forget
to enable features in testplanet.
In terms of reviewing, this change should be actually fairly
easy to review, considering private/testplanet/satellite.go keeps
the current config system and the new one and confirms that they
result in identical configurations, so you can be certain that
nothing was missed and the config is all correct.
You can also check the config lock to see what actual config
values changed.
Change-Id: I6715d0794887f577e21742afcf56fd2b9d12170e
2021-05-31 22:15:00 +01:00
|
|
|
From string `help:"sender email address" default:"" testDefault:"Labs <storj@mail.test>"`
|
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
|
2021-11-18 18:55:37 +00:00
|
|
|
Sender Sender
|
2019-03-02 15:22:20 +00:00
|
|
|
|
|
|
|
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
|
2021-11-18 18:55:37 +00:00
|
|
|
service := &Service{log: log, Sender: sender}
|
2019-03-02 15:22:20 +00:00
|
|
|
|
|
|
|
// 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)
|
2021-11-18 18:55:37 +00: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
|
|
|
|
2021-05-24 20:33:02 +01:00
|
|
|
ctx, cancel := context.WithTimeout(context2.WithoutCancellation(ctx), 5*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
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{
|
2021-11-18 18:55:37 +00:00
|
|
|
From: service.Sender.FromAddress(),
|
2019-03-02 15:22:20 +00:00
|
|
|
To: to,
|
|
|
|
Subject: msg.Subject(),
|
|
|
|
PlainText: textBuffer.String(),
|
|
|
|
Parts: []post.Part{
|
|
|
|
{
|
|
|
|
Type: "text/html; charset=UTF-8",
|
|
|
|
Content: htmlBuffer.String(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-11-18 18:55:37 +00:00
|
|
|
return service.Sender.SendEmail(ctx, m)
|
2019-03-02 15:22:20 +00:00
|
|
|
}
|