1
0
Fork 0
mirror of https://github.com/documize/community.git synced 2025-07-19 05:09:42 +02:00
documize/domain/mail/mailer.go

63 lines
1.6 KiB
Go
Raw Normal View History

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 (
"bytes"
2021-08-18 19:39:51 -04:00
"fmt"
"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"
"github.com/documize/community/core/mail"
2017-08-02 15:26:31 +01:00
"github.com/documize/community/domain"
"github.com/documize/community/domain/setting"
ds "github.com/documize/community/domain/smtp"
"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 {
Runtime *env.Runtime
Store *store.Store
Context domain.RequestContext
Config ds.Config
Dialer *mail.Dialer
2017-08-02 15:26:31 +01: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
}
// 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)
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)
return
}
2017-08-27 16:39:09 +01:00
buffer := new(bytes.Buffer)
2021-08-18 19:39:51 -04:00
t := template.Must(template.New("emailTemplate").Parse(content))
t.Execute(buffer, &params)
2017-08-27 16:39:09 +01:00
html = buffer.String()
2017-08-27 16:39:09 +01:00
return
2017-08-02 15:26:31 +01:00
}