2016-07-07 18:54:16 -07:00
|
|
|
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
|
|
|
|
//
|
2016-07-20 15:58:37 +01:00
|
|
|
// This software (Documize Community Edition) is licensed under
|
2016-07-07 18:54:16 -07:00
|
|
|
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
|
|
|
|
//
|
|
|
|
// You can operate outside the AGPL restrictions by purchasing
|
|
|
|
// Documize Enterprise Edition and obtaining a commercial license
|
2016-07-20 15:58:37 +01:00
|
|
|
// by contacting <sales@documize.com>.
|
2016-07-07 18:54:16 -07:00
|
|
|
//
|
|
|
|
// https://documize.com
|
|
|
|
|
|
|
|
package mail
|
|
|
|
|
|
|
|
import (
|
2018-03-07 18:52:19 +00:00
|
|
|
"bytes"
|
2021-08-18 19:39:51 -04:00
|
|
|
"fmt"
|
2018-03-07 18:52:19 +00:00
|
|
|
"html/template"
|
2016-07-07 18:54:16 -07:00
|
|
|
|
2021-08-18 19:39:51 -04:00
|
|
|
"github.com/documize/community/core/asset"
|
2017-08-02 15:26:31 +01:00
|
|
|
"github.com/documize/community/core/env"
|
2018-03-07 18:52:19 +00:00
|
|
|
"github.com/documize/community/core/mail"
|
2017-08-02 15:26:31 +01:00
|
|
|
"github.com/documize/community/domain"
|
2018-03-07 18:52:19 +00:00
|
|
|
"github.com/documize/community/domain/setting"
|
|
|
|
ds "github.com/documize/community/domain/smtp"
|
2018-09-27 15:14:48 +01:00
|
|
|
"github.com/documize/community/domain/store"
|
2021-08-18 19:39:51 -04:00
|
|
|
"github.com/pkg/errors"
|
2016-07-07 18:54:16 -07:00
|
|
|
)
|
|
|
|
|
2017-08-02 15:26:31 +01:00
|
|
|
// Mailer provides emailing facilities
|
|
|
|
type Mailer struct {
|
2018-03-07 18:52:19 +00:00
|
|
|
Runtime *env.Runtime
|
2018-09-27 15:14:48 +01:00
|
|
|
Store *store.Store
|
2018-03-07 18:52:19 +00:00
|
|
|
Context domain.RequestContext
|
|
|
|
Config ds.Config
|
|
|
|
Dialer *mail.Dialer
|
2017-08-02 15:26:31 +01:00
|
|
|
}
|
|
|
|
|
2018-03-07 18:52:19 +00:00
|
|
|
// Initialize prepares mailer instance for action.
|
|
|
|
func (m *Mailer) Initialize() {
|
|
|
|
m.Config = setting.GetSMTPConfig(m.Store)
|
|
|
|
m.Dialer, _ = ds.Connect(m.Config)
|
2016-07-07 18:54:16 -07:00
|
|
|
}
|
|
|
|
|
2018-03-07 18:52:19 +00:00
|
|
|
// ParseTemplate produces email template.
|
|
|
|
func (m *Mailer) ParseTemplate(filename string, params interface{}) (html string, err error) {
|
|
|
|
html = ""
|
2017-08-02 15:26:31 +01:00
|
|
|
|
2021-08-18 19:39:51 -04:00
|
|
|
content, _, err := asset.FetchStatic(m.Runtime.Assets, filename)
|
2018-03-07 18:52:19 +00:00
|
|
|
if err != nil {
|
2021-08-18 19:39:51 -04:00
|
|
|
err = errors.Wrap(err, fmt.Sprintf("missing %s", filename))
|
|
|
|
m.Runtime.Log.Error("failed to load mail template", err)
|
2018-03-07 18:52:19 +00:00
|
|
|
return
|
|
|
|
}
|
2017-08-27 16:39:09 +01:00
|
|
|
|
2018-03-07 18:52:19 +00:00
|
|
|
buffer := new(bytes.Buffer)
|
2021-08-18 19:39:51 -04:00
|
|
|
t := template.Must(template.New("emailTemplate").Parse(content))
|
2018-03-07 18:52:19 +00:00
|
|
|
t.Execute(buffer, ¶ms)
|
2017-08-27 16:39:09 +01:00
|
|
|
|
2018-03-07 18:52:19 +00:00
|
|
|
html = buffer.String()
|
2017-08-27 16:39:09 +01:00
|
|
|
|
2018-03-07 18:52:19 +00:00
|
|
|
return
|
2017-08-02 15:26:31 +01:00
|
|
|
}
|