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"
|
|
|
|
"html/template"
|
2016-07-07 18:54:16 -07:00
|
|
|
|
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"
|
2018-03-07 18:52:19 +00:00
|
|
|
"github.com/documize/community/server/web"
|
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
|
|
|
|
2018-03-07 18:52:19 +00:00
|
|
|
file, err := web.ReadFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2017-08-27 16:39:09 +01:00
|
|
|
|
2018-03-07 18:52:19 +00:00
|
|
|
emailTemplate := string(file)
|
|
|
|
buffer := new(bytes.Buffer)
|
2017-08-27 16:39:09 +01:00
|
|
|
|
2018-03-07 18:52:19 +00:00
|
|
|
t := template.Must(template.New("emailTemplate").Parse(emailTemplate))
|
|
|
|
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
|
|
|
}
|