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

66 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"
"html/template"
2016-07-07 18:54:16 -07:00
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/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 {
Runtime *env.Runtime
Store *domain.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
}
// Send prepares and sends email.
func (m *Mailer) Send(em ds.EmailMessage) (ok bool, err error) {
ok, err = ds.SendMessage(m.Dialer, m.Config, em)
return
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
file, err := web.ReadFile(filename)
if err != nil {
return
}
2017-08-27 16:39:09 +01:00
emailTemplate := string(file)
buffer := new(bytes.Buffer)
2017-08-27 16:39:09 +01:00
t := template.Must(template.New("emailTemplate").Parse(emailTemplate))
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
}