gosora/experimental/plugin_sendmail.go
Azareal e9b46e1cd6 Email errors should be logged now.
Reordered the registration logic so the session is created before any, if any, email errors fire.
Fixed a bug where the analytics phrases would load after the graph is drawn.
2019-03-03 13:19:32 +10:00

72 lines
1.9 KiB
Go

package main
import (
"errors"
"io"
"os/exec"
"runtime"
"github.com/Azareal/Gosora/common"
)
/*
Sending emails in a way you really shouldn't be sending them.
This method doesn't require a SMTP server, but has higher chances of an email being rejected or being seen as spam. Use at your own risk. Only for Linux as Windows doesn't have Sendmail.
*/
func init() {
// Don't bother registering this plugin on platforms other than Linux
if runtime.GOOS != "linux" {
return
}
common.Plugins.Add(&common.Plugin{UName: "sendmail", Name: "Sendmail", Author: "Azareal", URL: "http://github.com/Azareal", Tag: "Linux Only", Init: initSendmail, Activate: activateSendmail, Deactivate: deactivateSendmail})
}
func initSendmail(plugin *common.Plugin) error {
plugin.AddHook("email_send_intercept", sendSendmail)
return nil
}
// /usr/sbin/sendmail is only available on Linux
func activateSendmail(plugin *common.Plugin) error {
if !common.Site.EnableEmails {
return errors.New("You have emails disabled in your configuration file")
}
if runtime.GOOS != "linux" {
return errors.New("This plugin only supports Linux")
}
return nil
}
func deactivateSendmail(plugin *common.Plugin) {
plugin.RemoveHook("email_send_intercept", sendSendmail)
}
func sendSendmail(data ...interface{}) interface{} {
to := data[0].(string)
subject := data[1].(string)
body := data[2].(string)
msg := "From: " + common.Site.Email + "\n"
msg += "To: " + to + "\n"
msg += "Subject: " + subject + "\n\n"
msg += body + "\n"
sendmail := exec.Command("/usr/sbin/sendmail", "-t", "-i")
stdin, err := sendmail.StdinPipe()
if err != nil {
return err // Possibly disable the plugin and show an error to the admin on the dashboard? Plugin log file?
}
err = sendmail.Start()
if err != nil {
return err
}
io.WriteString(stdin, msg)
err = stdin.Close()
if err != nil {
return err
}
return sendmail.Wait()
}