@@ -2,6 +2,7 @@ package api
2
2
3
3
import (
4
4
"fmt"
5
+ "github.com/wneessen/js-mailer/form"
5
6
"github.com/wneessen/js-mailer/response"
6
7
"net/http"
7
8
"time"
@@ -10,10 +11,12 @@ import (
10
11
"github.com/labstack/echo/v4"
11
12
)
12
13
13
- // SentSuccessfull represents confirmation JSON structure for a successfully sent message
14
- type SentSuccessfull struct {
15
- FormId string `json:"form_id"`
16
- SendTime int64 `json:"send_time"`
14
+ // SentSuccessful represents confirmation JSON structure for a successfully sent message
15
+ type SentSuccessful struct {
16
+ FormId string `json:"form_id"`
17
+ SendTime int64 `json:"send_time"`
18
+ ConfirmationSent bool `json:"confirmation_sent"`
19
+ ConfirmationRcpt string `json:"confirmation_rcpt"`
17
20
}
18
21
19
22
// SendForm handles the HTTP form sending API request
@@ -24,6 +27,33 @@ func (r *Route) SendForm(c echo.Context) error {
24
27
return echo .NewHTTPError (http .StatusInternalServerError , "Internal Server Error" )
25
28
}
26
29
30
+ // Do we have some confirmation mail to handle?
31
+ confirmWasSent := false
32
+ confirmRcpt := ""
33
+ if sr .FormObj .Confirmation .Enabled {
34
+ sendConfirm := true
35
+ confirmRcpt = c .FormValue (sr .FormObj .Confirmation .RecipientField )
36
+ if confirmRcpt == "" {
37
+ c .Logger ().Warnf ("confirmation mail feature activated, but recpienent field not found or empty" )
38
+ sendConfirm = false
39
+ }
40
+ if sr .FormObj .Confirmation .Subject == "" {
41
+ c .Logger ().Warnf ("confirmation mail feature activated, but no subject found in configuration" )
42
+ sendConfirm = false
43
+ }
44
+ if sr .FormObj .Confirmation .Content == "" {
45
+ c .Logger ().Warnf ("confirmation mail feature activated, but no content found in configuration" )
46
+ sendConfirm = false
47
+ }
48
+ if sendConfirm {
49
+ confirmWasSent = true
50
+ if err := SendFormConfirmation (sr .FormObj , confirmRcpt ); err != nil {
51
+ c .Logger ().Warnf ("failed to send confirmation mail: %s" , err )
52
+ confirmWasSent = false
53
+ }
54
+ }
55
+ }
56
+
27
57
// Compose the mail message
28
58
mailMsg := mail .NewMessage ()
29
59
mailMsg .SetHeader ("From" , sr .FormObj .Sender )
@@ -39,19 +69,7 @@ func (r *Route) SendForm(c echo.Context) error {
39
69
mailMsg .SetBody ("text/plain" , mailBody )
40
70
41
71
// Send the mail message
42
- var serverTimeout time.Duration
43
- var err error
44
- serverTimeout , err = time .ParseDuration (sr .FormObj .Server .Timeout )
45
- if err != nil {
46
- c .Logger ().Warnf ("Could not parse configured server timeout: %s" , err )
47
- serverTimeout = time .Second * 5
48
- }
49
- mailDailer := mail .NewDialer (sr .FormObj .Server .Host , sr .FormObj .Server .Port , sr .FormObj .Server .Username ,
50
- sr .FormObj .Server .Password )
51
- mailDailer .Timeout = serverTimeout
52
- if sr .FormObj .Server .ForceTLS {
53
- mailDailer .StartTLSPolicy = mail .MandatoryStartTLS
54
- }
72
+ mailDailer := GetMailDailer (sr .FormObj )
55
73
mailSender , err := mailDailer .Dial ()
56
74
if err != nil {
57
75
c .Logger ().Errorf ("Could not connect to configured mail server: %s" , err )
@@ -76,9 +94,47 @@ func (r *Route) SendForm(c echo.Context) error {
76
94
return c .JSON (http .StatusOK , response.SuccessResponse {
77
95
StatusCode : http .StatusOK ,
78
96
Status : http .StatusText (http .StatusOK ),
79
- Data : & SentSuccessfull {
80
- FormId : sr .FormObj .Id ,
81
- SendTime : time .Now ().Unix (),
97
+ Data : SentSuccessful {
98
+ FormId : sr .FormObj .Id ,
99
+ SendTime : time .Now ().Unix (),
100
+ ConfirmationSent : confirmWasSent ,
101
+ ConfirmationRcpt : confirmRcpt ,
82
102
},
83
103
})
84
104
}
105
+
106
+ // SendFormConfirmation sends out a confirmation mail if requested in the form
107
+ func SendFormConfirmation (f * form.Form , r string ) error {
108
+ mailMsg := mail .NewMessage ()
109
+ mailMsg .SetHeader ("From" , f .Sender )
110
+ mailMsg .SetHeader ("To" , r )
111
+ mailMsg .SetHeader ("Subject" , f .Confirmation .Subject )
112
+ mailMsg .SetBody ("text/plain" , f .Confirmation .Content )
113
+ mailDailer := GetMailDailer (f )
114
+ mailSender , err := mailDailer .Dial ()
115
+ if err != nil {
116
+ return fmt .Errorf ("could not connect to configured mail server: %w" , err )
117
+ }
118
+ if err := mail .Send (mailSender , mailMsg ); err != nil {
119
+ return fmt .Errorf ("could not send confirmation mail message: %w" , err )
120
+ }
121
+ if err := mailSender .Close (); err != nil {
122
+ return fmt .Errorf ("failed to close mail server connection: %w" , err )
123
+ }
124
+ return nil
125
+ }
126
+
127
+ // GetMailDailer returns a new mail dailer object based on the form configuration
128
+ func GetMailDailer (f * form.Form ) * mail.Dialer {
129
+ var serverTimeout time.Duration
130
+ serverTimeout , err := time .ParseDuration (f .Server .Timeout )
131
+ if err != nil {
132
+ serverTimeout = time .Second * 5
133
+ }
134
+ mailDailer := mail .NewDialer (f .Server .Host , f .Server .Port , f .Server .Username , f .Server .Password )
135
+ mailDailer .Timeout = serverTimeout
136
+ if f .Server .ForceTLS {
137
+ mailDailer .StartTLSPolicy = mail .MandatoryStartTLS
138
+ }
139
+ return mailDailer
140
+ }
0 commit comments