mirror of
https://github.com/documize/community.git
synced 2025-07-24 23:59:47 +02:00
New SMTP library and refactoring of mailer code
This commit is contained in:
parent
99220641f3
commit
bd43319bb2
39 changed files with 3163 additions and 1268 deletions
|
@ -9,61 +9,57 @@
|
|||
//
|
||||
// https://documize.com
|
||||
|
||||
// jshint ignore:start
|
||||
|
||||
package mail
|
||||
|
||||
import (
|
||||
"net/smtp"
|
||||
"strings"
|
||||
"bytes"
|
||||
"html/template"
|
||||
|
||||
"github.com/documize/community/core/env"
|
||||
"github.com/documize/community/core/mail"
|
||||
"github.com/documize/community/domain"
|
||||
"github.com/documize/community/domain/setting"
|
||||
ds "github.com/documize/community/domain/smtp"
|
||||
"github.com/documize/community/server/web"
|
||||
)
|
||||
|
||||
// Mailer provides emailing facilities
|
||||
type Mailer struct {
|
||||
Runtime *env.Runtime
|
||||
Store *domain.Store
|
||||
Context domain.RequestContext
|
||||
Credentials Credentials
|
||||
Runtime *env.Runtime
|
||||
Store *domain.Store
|
||||
Context domain.RequestContext
|
||||
Config ds.Config
|
||||
Dialer *mail.Dialer
|
||||
}
|
||||
|
||||
// Credentials holds SMTP endpoint and authentication methods
|
||||
type Credentials struct {
|
||||
SMTPuserid string
|
||||
SMTPpassword string
|
||||
SMTPhost string
|
||||
SMTPport string
|
||||
SMTPsender string
|
||||
// Initialize prepares mailer instance for action.
|
||||
func (m *Mailer) Initialize() {
|
||||
m.Config = setting.GetSMTPConfig(m.Store)
|
||||
m.Dialer, _ = ds.Connect(m.Config)
|
||||
}
|
||||
|
||||
// GetAuth to return SMTP authentication details
|
||||
func (m *Mailer) GetAuth() smtp.Auth {
|
||||
a := smtp.PlainAuth("", m.Credentials.SMTPuserid, m.Credentials.SMTPpassword, m.Credentials.SMTPhost)
|
||||
return a
|
||||
// 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
|
||||
}
|
||||
|
||||
// GetHost to return SMTP host details
|
||||
func (m *Mailer) GetHost() string {
|
||||
h := m.Credentials.SMTPhost + ":" + m.Credentials.SMTPport
|
||||
return h
|
||||
}
|
||||
|
||||
// LoadCredentials loads up SMTP details from database
|
||||
func (m *Mailer) LoadCredentials() {
|
||||
userID, _ := m.Store.Setting.Get("SMTP", "userid")
|
||||
m.Credentials.SMTPuserid = strings.TrimSpace(userID)
|
||||
|
||||
pwd, _ := m.Store.Setting.Get("SMTP", "password")
|
||||
m.Credentials.SMTPpassword = strings.TrimSpace(pwd)
|
||||
|
||||
host, _ := m.Store.Setting.Get("SMTP", "host")
|
||||
m.Credentials.SMTPhost = strings.TrimSpace(host)
|
||||
|
||||
port, _ := m.Store.Setting.Get("SMTP", "port")
|
||||
m.Credentials.SMTPport = strings.TrimSpace(port)
|
||||
|
||||
sender, _ := m.Store.Setting.Get("SMTP", "sender")
|
||||
m.Credentials.SMTPsender = strings.TrimSpace(sender)
|
||||
// ParseTemplate produces email template.
|
||||
func (m *Mailer) ParseTemplate(filename string, params interface{}) (html string, err error) {
|
||||
html = ""
|
||||
|
||||
file, err := web.ReadFile(filename)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
emailTemplate := string(file)
|
||||
buffer := new(bytes.Buffer)
|
||||
|
||||
t := template.Must(template.New("emailTemplate").Parse(emailTemplate))
|
||||
t.Execute(buffer, ¶ms)
|
||||
|
||||
html = buffer.String()
|
||||
|
||||
return
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue