2016-12-16 10:37:42 +00:00
|
|
|
package main
|
|
|
|
|
2019-02-17 05:38:49 +00:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"os/exec"
|
|
|
|
"runtime"
|
|
|
|
|
|
|
|
"github.com/Azareal/Gosora/common"
|
|
|
|
)
|
2016-12-16 10:37:42 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
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() {
|
2019-02-17 05:38:49 +00:00
|
|
|
// 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})
|
2016-12-16 10:37:42 +00:00
|
|
|
}
|
|
|
|
|
2019-02-17 05:38:49 +00:00
|
|
|
func initSendmail(plugin *common.Plugin) error {
|
|
|
|
plugin.AddHook("email_send_intercept", sendSendmail)
|
|
|
|
return nil
|
2016-12-16 10:37:42 +00:00
|
|
|
}
|
|
|
|
|
2019-02-17 05:38:49 +00:00
|
|
|
// /usr/sbin/sendmail is only available on Linux
|
|
|
|
func activateSendmail(plugin *common.Plugin) error {
|
|
|
|
if !common.Site.EnableEmails {
|
2016-12-16 10:37:42 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-02-17 05:38:49 +00:00
|
|
|
func deactivateSendmail(plugin *common.Plugin) {
|
|
|
|
plugin.RemoveHook("email_send_intercept", sendSendmail)
|
2016-12-16 10:37:42 +00:00
|
|
|
}
|
|
|
|
|
2019-02-17 05:38:49 +00:00
|
|
|
func sendSendmail(data ...interface{}) interface{} {
|
2016-12-16 10:37:42 +00:00
|
|
|
to := data[0].(string)
|
|
|
|
subject := data[1].(string)
|
|
|
|
body := data[2].(string)
|
2019-02-17 05:38:49 +00:00
|
|
|
|
|
|
|
msg := "From: " + common.Site.Email + "\n"
|
2016-12-16 10:37:42 +00:00
|
|
|
msg += "To: " + to + "\n"
|
|
|
|
msg += "Subject: " + subject + "\n\n"
|
|
|
|
msg += body + "\n"
|
2019-02-17 05:38:49 +00:00
|
|
|
|
|
|
|
sendmail := exec.Command("/usr/sbin/sendmail", "-t", "-i")
|
2016-12-16 10:37:42 +00:00
|
|
|
stdin, err := sendmail.StdinPipe()
|
|
|
|
if err != nil {
|
2019-03-03 03:19:32 +00:00
|
|
|
return err // Possibly disable the plugin and show an error to the admin on the dashboard? Plugin log file?
|
2016-12-16 10:37:42 +00:00
|
|
|
}
|
2019-02-17 05:38:49 +00:00
|
|
|
|
2016-12-16 10:37:42 +00:00
|
|
|
err = sendmail.Start()
|
|
|
|
if err != nil {
|
2019-03-03 03:19:32 +00:00
|
|
|
return err
|
2016-12-16 10:37:42 +00:00
|
|
|
}
|
|
|
|
io.WriteString(stdin, msg)
|
2019-02-17 05:38:49 +00:00
|
|
|
|
2016-12-16 10:37:42 +00:00
|
|
|
err = stdin.Close()
|
|
|
|
if err != nil {
|
2019-03-03 03:19:32 +00:00
|
|
|
return err
|
2016-12-16 10:37:42 +00:00
|
|
|
}
|
2019-03-03 03:19:32 +00:00
|
|
|
return sendmail.Wait()
|
2019-02-17 05:38:49 +00:00
|
|
|
}
|