Wire up sudo email form to send emails
Probably.
This commit is contained in:
parent
67a7d20f6c
commit
9cbb81f347
6 changed files with 81 additions and 26 deletions
|
|
@ -9,7 +9,6 @@ import (
|
|||
"net/http"
|
||||
|
||||
"github.com/Gleipnir-Technology/nidus-sync/config"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/db/enums"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
|
|
@ -67,7 +66,7 @@ type emailResponse struct {
|
|||
|
||||
var FORWARDEMAIL_API = "https://api.forwardemail.net/v1/emails"
|
||||
|
||||
func Send(ctx context.Context, email Request, t enums.CommsMessagetypeemail) (response emailResponse, err error) {
|
||||
func Send(ctx context.Context, email Request) (response emailResponse, err error) {
|
||||
payload, err := json.Marshal(email)
|
||||
if err != nil {
|
||||
return response, fmt.Errorf("Failed to marshal email request: %w", err)
|
||||
|
|
|
|||
|
|
@ -20,9 +20,12 @@ var (
|
|||
FilesDirectory string
|
||||
FieldseekerSchemaDirectory string
|
||||
ForwardEmailAPIToken string
|
||||
ForwardEmailReportAddress string
|
||||
ForwardEmailReportPassword string
|
||||
ForwardEmailReportUsername string
|
||||
ForwardEmailRMOAddress string
|
||||
ForwardEmailRMOPassword string
|
||||
ForwardEmailRMOUsername string
|
||||
ForwardEmailNidusAddress string
|
||||
ForwardEmailNidusPassword string
|
||||
ForwardEmailNidusUsername string
|
||||
MapboxToken string
|
||||
PGDSN string
|
||||
PhoneNumberReport phonenumbers.PhoneNumber
|
||||
|
|
@ -105,21 +108,33 @@ func Parse() (err error) {
|
|||
if FilesDirectory == "" {
|
||||
return fmt.Errorf("You must specify a non-empty FILES_DIRECTORY")
|
||||
}
|
||||
ForwardEmailReportAddress = os.Getenv("FORWARDEMAIL_REPORT_ADDRESS")
|
||||
if ForwardEmailReportAddress == "" {
|
||||
return fmt.Errorf("You must specify a non-empty FORWARDEMAIL_REPORT_ADDRESS")
|
||||
}
|
||||
ForwardEmailAPIToken = os.Getenv("FORWARDEMAIL_API_TOKEN")
|
||||
if ForwardEmailAPIToken == "" {
|
||||
return fmt.Errorf("You must specify a non-empty FORWARDEMAIL_API_TOKEN")
|
||||
}
|
||||
ForwardEmailReportUsername = os.Getenv("FORWARDEMAIL_REPORT_USERNAME")
|
||||
if ForwardEmailReportUsername == "" {
|
||||
return fmt.Errorf("You must specify a non-empty FORWARDEMAIL_REPORT_USERNAME")
|
||||
ForwardEmailRMOAddress = os.Getenv("FORWARDEMAIL_RMO_ADDRESS")
|
||||
if ForwardEmailRMOAddress == "" {
|
||||
return fmt.Errorf("You must specify a non-empty FORWARDEMAIL_RMO_ADDRESS")
|
||||
}
|
||||
ForwardEmailReportPassword = os.Getenv("FORWARDEMAIL_REPORT_PASSWORD")
|
||||
if ForwardEmailReportPassword == "" {
|
||||
return fmt.Errorf("You must specify a non-empty FORWARDEMAIL_REPORT_PASSWORD")
|
||||
ForwardEmailRMOUsername = os.Getenv("FORWARDEMAIL_RMO_USERNAME")
|
||||
if ForwardEmailRMOUsername == "" {
|
||||
return fmt.Errorf("You must specify a non-empty FORWARDEMAIL_RMO_USERNAME")
|
||||
}
|
||||
ForwardEmailRMOPassword = os.Getenv("FORWARDEMAIL_RMO_PASSWORD")
|
||||
if ForwardEmailRMOPassword == "" {
|
||||
return fmt.Errorf("You must specify a non-empty FORWARDEMAIL_RMO_PASSWORD")
|
||||
}
|
||||
ForwardEmailNidusAddress = os.Getenv("FORWARDEMAIL_NIDUS_ADDRESS")
|
||||
if ForwardEmailNidusAddress == "" {
|
||||
return fmt.Errorf("You must specify a non-empty FORWARDEMAIL_NIDUS_ADDRESS")
|
||||
}
|
||||
ForwardEmailNidusUsername = os.Getenv("FORWARDEMAIL_NIDUS_USERNAME")
|
||||
if ForwardEmailNidusUsername == "" {
|
||||
return fmt.Errorf("You must specify a non-empty FORWARDEMAIL_NIDUS_USERNAME")
|
||||
}
|
||||
ForwardEmailNidusPassword = os.Getenv("FORWARDEMAIL_NIDUS_PASSWORD")
|
||||
if ForwardEmailNidusPassword == "" {
|
||||
return fmt.Errorf("You must specify a non-empty FORWARDEMAIL_NIDUS_PASSWORD")
|
||||
}
|
||||
MapboxToken = os.Getenv("MAPBOX_TOKEN")
|
||||
if MapboxToken == "" {
|
||||
|
|
|
|||
|
|
@ -149,10 +149,12 @@
|
|||
<div class="mb-3">
|
||||
<label for="emailFrom" class="form-label">From Account</label>
|
||||
<select class="form-select" id="emailFrom">
|
||||
<option value="support">support@example.com</option>
|
||||
<option value="noreply">noreply@example.com</option>
|
||||
<option value="billing">billing@example.com</option>
|
||||
<option value="marketing">marketing@example.com</option>
|
||||
<option value="{{ .C.ForwardEmailRMOAddress }}">
|
||||
{{ .C.ForwardEmailRMOAddress }}
|
||||
</option>
|
||||
<option value="{{ .C.ForwardEmailNidusAddress }}">
|
||||
{{ .C.ForwardEmailNidusAddress }}
|
||||
</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
|
|
|
|||
|
|
@ -52,7 +52,7 @@ func urlUnsubscribe(email string) string {
|
|||
func sendEmailInitialContact(ctx context.Context, destination string) error {
|
||||
//data := pgtypes.HStore{}
|
||||
data := make(map[string]string, 0)
|
||||
source := config.ForwardEmailReportAddress
|
||||
source := config.ForwardEmailRMOAddress
|
||||
data["Destination"] = destination
|
||||
data["Source"] = source
|
||||
data["URLLogo"] = config.MakeURLReport("/static/img/nidus-logo-no-lettering-64.png")
|
||||
|
|
@ -78,7 +78,7 @@ func sendEmailInitialContact(ctx context.Context, destination string) error {
|
|||
Subject: subject,
|
||||
Text: text,
|
||||
To: destination,
|
||||
}, enums.CommsMessagetypeemailInitialContact)
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to send email to %s: %w", err)
|
||||
|
|
|
|||
|
|
@ -70,17 +70,17 @@ func sendEmailReportConfirmation(ctx context.Context, job Job) error {
|
|||
return fmt.Errorf("Failed to render email report notification template: %w", err)
|
||||
}
|
||||
subject := fmt.Sprintf("Mosquito Report Submission - %s", report_id_str)
|
||||
err = insertEmailLog(ctx, data, j.destination(), public_id, config.ForwardEmailReportAddress, subject, templateReportNotificationConfirmationID)
|
||||
err = insertEmailLog(ctx, data, j.destination(), public_id, config.ForwardEmailRMOAddress, subject, templateReportNotificationConfirmationID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to store email log: %w", err)
|
||||
}
|
||||
resp, err := email.Send(ctx, email.Request{
|
||||
From: config.ForwardEmailReportAddress,
|
||||
From: config.ForwardEmailRMOAddress,
|
||||
HTML: html,
|
||||
Subject: subject,
|
||||
Text: text,
|
||||
To: j.destination(),
|
||||
}, enums.CommsMessagetypeemailReportNotificationConfirmation)
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to send email report confirmation to %s for report %s: %w", j.dest, j.reportID, err)
|
||||
}
|
||||
|
|
|
|||
43
sync/sudo.go
43
sync/sudo.go
|
|
@ -2,8 +2,10 @@ package sync
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/Gleipnir-Technology/nidus-sync/comms/email"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/comms/text"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/config"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/db/enums"
|
||||
|
|
@ -12,7 +14,10 @@ import (
|
|||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
type contentSudo struct{}
|
||||
type contentSudo struct {
|
||||
ForwardEmailRMOAddress string
|
||||
ForwardEmailNidusAddress string
|
||||
}
|
||||
|
||||
func getSudo(ctx context.Context, user *models.User) (string, interface{}, *errorWithStatus) {
|
||||
if user.Role != enums.UserroleRoot {
|
||||
|
|
@ -21,12 +26,46 @@ func getSudo(ctx context.Context, user *models.User) (string, interface{}, *erro
|
|||
Status: http.StatusForbidden,
|
||||
}
|
||||
}
|
||||
content := contentAdminDash{}
|
||||
content := contentSudo{
|
||||
ForwardEmailRMOAddress: config.ForwardEmailRMOAddress,
|
||||
ForwardEmailNidusAddress: config.ForwardEmailNidusAddress,
|
||||
}
|
||||
return "sync/sudo.html", content, nil
|
||||
}
|
||||
|
||||
var decoder = schema.NewDecoder()
|
||||
|
||||
type FormEmail struct {
|
||||
Body string `schema:"emailBody"`
|
||||
From string `schema:"emailFrom"`
|
||||
Subject string `schema:"emailSubject"`
|
||||
To string `schema:"emailTo"`
|
||||
}
|
||||
|
||||
func postSudoEmail(ctx context.Context, u *models.User, e FormEmail) (string, *errorWithStatus) {
|
||||
if u.Role != enums.UserroleRoot {
|
||||
return "", &errorWithStatus{
|
||||
Message: "You must have sudo powers to do this",
|
||||
Status: http.StatusForbidden,
|
||||
}
|
||||
}
|
||||
request := email.Request{
|
||||
From: e.From,
|
||||
HTML: fmt.Sprintf("<html><p>%s</p></html>", e.Body),
|
||||
Sender: e.From,
|
||||
Subject: e.Subject,
|
||||
To: e.To,
|
||||
Text: e.Body,
|
||||
}
|
||||
resp, err := email.Send(ctx, request)
|
||||
if err != nil {
|
||||
log.Warn().Err(err).Msg("Failed to send email")
|
||||
} else {
|
||||
log.Info().Str("id", resp.ID).Msg("Sent Email...?")
|
||||
}
|
||||
return "/sudo", nil
|
||||
}
|
||||
|
||||
type FormSMS struct {
|
||||
Message string `schema:"smsMessage"`
|
||||
Phone string `schema:"smsPhone"`
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue