Skip to content

Commit 453fd91

Browse files
committed
1 parent 1e67a29 commit 453fd91

1 file changed

Lines changed: 37 additions & 20 deletions

File tree

bootstrap.php

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -138,17 +138,25 @@
138138

139139
$frm = $this->form($form);
140140

141+
// Invalid form name
141142
if (!$frm) {
142143
return false;
143144
}
144145

145-
// custom form validation
146-
if ($this->app->path("#config:forms/{$form}.php") && false===include($this->app->path("#config:forms/{$form}.php"))) {
146+
// Load custom form validator
147+
if ($this->app->path("#config:forms/{$form}.php") && false === include($this->app->path("#config:forms/{$form}.php"))) {
147148
return false;
148149
}
149150

151+
// Filter submitted data
150152
$this->app->trigger('forms.submit.before', [$form, &$data, $frm, &$options]);
151153

154+
// Invalid form data
155+
if (empty($data)) {
156+
return false;
157+
}
158+
159+
// Send email
152160
if (isset($frm['email_forward']) && $frm['email_forward']) {
153161

154162
if (!empty(getenv('EMAIL_FORWARD'))) {
@@ -158,9 +166,8 @@
158166
$emails = array_map('trim', explode(',', $frm['email_forward']));
159167
$filtered_emails = [];
160168

169+
// Validate each email address individually, push if valid
161170
foreach ($emails as $to){
162-
163-
// Validate each email address individually, push if valid
164171
if ($this->app->helper('utils')->isEmail($to)){
165172
$filtered_emails[] = $to;
166173
}
@@ -170,42 +177,52 @@
170177

171178
$frm['email_forward'] = implode(',', $filtered_emails);
172179

173-
// There is an email template available
174-
if ($template = $this->app->path("#config:forms/emails/{$form}.php")) {
175-
176-
$body = $this->app->renderer->file($template, ['data' => $data, 'frm' => $frm], false);
177-
178-
// Prepare template manually
179-
} else {
180+
$body = null;
180181

181-
$body = [];
182+
// Load custom email template
183+
if ($template = $this->app->path("#config:forms/emails/{$form}.php")) {
184+
$originalLayout = $this->app->layout;
185+
$this->app->layout = false;
186+
$body = $this->app->view($template, ['data' => $data, 'frm' => $frm]);
187+
$this->app->layout = $originalLayout;
188+
}
182189

183-
foreach ($data as $key => $value) {
184-
$body[] = "<b>{$key}:</b>\n<br>";
185-
$body[] = (is_string($value) ? $value:json_encode($value))."\n<br>";
186-
}
190+
// Filter email content
191+
$this->app->trigger('forms.submit.email', [$form, &$data, $frm, &$body, &$options]);
187192

188-
$body = implode("\n<br>", $body);
193+
// Fallback to default email template
194+
if (empty($body)) {
195+
$originalLayout = $this->app->layout;
196+
$this->app->layout = false;
197+
$body = $this->app->view("formvalidation:templates/emails/contactform.php", ['data' => $data, 'frm' => $frm]);
198+
$this->app->layout = $originalLayout;
189199
}
190200

191201
$formname = isset($frm['label']) && trim($frm['label']) ? $frm['label'] : $form;
202+
$to = $frm['email_forward'];
203+
$subject = $options['subject'] ?? $this->app->helper('i18n')->getstr("New form data for: %s", [$formname]);
192204

193205
try {
194-
$response = $this->app->mailer->mail($frm['email_forward'], $options['subject'] ?? "New form data for: {$formname}", $body, $options);
206+
$response = $this->app->mailer->mail($to, $subject, $body, $options);
195207
} catch (\Exception $e) {
196208
$response = $e->getMessage();
197209
}
198210
}
199211
}
200212

213+
// Push entry to database
201214
if (isset($frm['save_entry']) && $frm['save_entry']) {
202215
$entry = ['data' => $data];
203216
$this->save($form, $entry);
204217
}
205218

206-
$this->app->trigger('forms.submit.after', [$form, &$data, $frm]);
219+
// Generate response array
220+
$response = (isset($response) && $response !== true) ? ['error' => $response, 'data' => $data] : $data;
221+
222+
// Filter submission response
223+
$this->app->trigger('forms.submit.after', [$form, &$data, $frm, &$response]);
207224

208-
return (isset($response) && $response !== true) ? ['error' => $response, 'data' => $data] : $data;
225+
return $response;
209226
}
210227
]);
211228

0 commit comments

Comments
 (0)