mirror of
https://codeberg.org/scip/ephemerup.git
synced 2025-12-19 05:21:04 +01:00
Added mail notification support
This commit is contained in:
@@ -59,11 +59,20 @@ func FormCreate(c *fiber.Ctx, cfg *cfg.Config, db *Db) error {
|
||||
ex, err := common.Untaint(formdata.Expire, cfg.RegDuration) // duration or asap allowed
|
||||
if err != nil {
|
||||
return JsonStatus(c, fiber.StatusForbidden,
|
||||
"Invalid data: "+err.Error())
|
||||
"Invalid expire data: "+err.Error())
|
||||
}
|
||||
entry.Expire = ex
|
||||
}
|
||||
|
||||
if len(formdata.Notify) != 0 {
|
||||
nt, err := common.Untaint(formdata.Notify, cfg.RegEmail)
|
||||
if err != nil {
|
||||
return JsonStatus(c, fiber.StatusForbidden,
|
||||
"Invalid email address: "+err.Error())
|
||||
}
|
||||
entry.Notify = nt
|
||||
}
|
||||
|
||||
// get url [and zip if there are multiple files]
|
||||
returnUrl := strings.Join([]string{cfg.Url, "form", id}, "/")
|
||||
entry.Url = returnUrl
|
||||
|
||||
54
api/mail.go
Normal file
54
api/mail.go
Normal file
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
Copyright © 2023 Thomas von Dein
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/tlinden/cenophane/cfg"
|
||||
"net/smtp"
|
||||
)
|
||||
|
||||
var mailtpl string = `To: %s\r
|
||||
From: %s\r
|
||||
Subject: %s\r
|
||||
\r
|
||||
%s\r
|
||||
`
|
||||
|
||||
/*
|
||||
Send an email via an external mail gateway. SMTP Auth is
|
||||
required. Errors may occur with a time delay, like server timeouts
|
||||
etc. So only call it detached via go routine.
|
||||
*/
|
||||
func Sendmail(c *cfg.Config, recipient string, body string, subject string) error {
|
||||
// Message.
|
||||
message := []byte(fmt.Sprintf(mailtpl, recipient, c.Mail.From, subject, body))
|
||||
|
||||
// Authentication.
|
||||
auth := smtp.PlainAuth("", c.Mail.From, c.Mail.Password, c.Mail.Server)
|
||||
|
||||
// Sending email.
|
||||
Log("Trying to send mail to %s via %s:%s with subject %s",
|
||||
recipient, c.Mail.Server, c.Mail.Port, subject)
|
||||
err := smtp.SendMail(c.Mail.Server+":"+c.Mail.Port, auth, c.Mail.From, []string{recipient}, []byte(message))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -24,6 +24,7 @@ import (
|
||||
"github.com/tlinden/cenophane/cfg"
|
||||
"github.com/tlinden/cenophane/common"
|
||||
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
@@ -122,7 +123,7 @@ func UploadPost(c *fiber.Ctx, cfg *cfg.Config, db *Db) error {
|
||||
|
||||
// ok, check if we need to remove a form, if so we do it in the
|
||||
// background. delete error doesn't lead to upload failure, we
|
||||
// only log it.
|
||||
// only log it. same applies to mail notification.
|
||||
formid, _ := SessionGetFormId(c)
|
||||
if formid != "" {
|
||||
go func() {
|
||||
@@ -132,6 +133,16 @@ func UploadPost(c *fiber.Ctx, cfg *cfg.Config, db *Db) error {
|
||||
if r.Forms[0].Expire == "asap" {
|
||||
db.Delete(apicontext, formid)
|
||||
}
|
||||
|
||||
// email notification to form creator
|
||||
if r.Forms[0].Notify != "" {
|
||||
body := fmt.Sprintf("Upload is available under: %s", returnUrl)
|
||||
subject := fmt.Sprintf("Upload form %s has been used", formid)
|
||||
err := Sendmail(cfg, r.Forms[0].Notify, body, subject)
|
||||
if err != nil {
|
||||
Log("Failed to send mail: %s", err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
Reference in New Issue
Block a user