diff --git a/FormWithAttachments/FormWithAttachmentsListener.php b/FormWithAttachments/FormWithAttachmentsListener.php index bad354c..976214f 100755 --- a/FormWithAttachments/FormWithAttachmentsListener.php +++ b/FormWithAttachments/FormWithAttachmentsListener.php @@ -6,6 +6,7 @@ use Statamic\Contracts\Forms\Submission; use Statamic\API\Email; use Statamic\API\File; +use Statamic\API\Parse; class FormWithAttachmentsListener extends Listener { @@ -40,10 +41,17 @@ public function handleSubmission(Submission $submission) foreach ($settings_forms as $form) { foreach ($form['settings'] as $setting) { + $setting = $this->parseConfig($setting, $submission->toArray()); + $single_email = []; $single_email['subject'] = $setting['subject']; $single_email['recipient'] = $setting['recipient']; $single_email['template'] = $setting['template']; + + if ($reply_to = array_get($setting, 'reply_to')) { + $single_email['reply_to'] = $reply_to; + } + array_push($emails, $single_email); } $file_deletion = (Boolean)$form['file_deletion']; @@ -52,22 +60,45 @@ public function handleSubmission(Submission $submission) $submissionData = $submission->toArray(); foreach ($emails as $email) { - $email_builder = Email::create(); - if (empty($assets)) { - $email_builder->to($email['recipient'])->subject($email['subject'])->template($email['template'])->with($submissionData); - } else { - $email_builder->to($email['recipient'])->subject($email['subject'])->template($email['template'])->with($submissionData); + $builder = Email::create() + ->to($email['recipient']) + ->subject($email['subject']) + ->template($email['template']) + ->with($submissionData); + + if (isset($email['reply_to'])) { + $builder->replyTo($email['reply_to']); + } + + if (! empty($assets)) { foreach ($assets as $asset) { - $email_builder->attach($asset); + $builder->attach($asset); } } - $email_builder->send(); + + $builder->send(); } - if ($file_deletion && !empty($assets)) { + if ($file_deletion && ! empty($assets)) { foreach ($assets as $asset) { File::delete($asset); } } } + + /** + * Parse the config values as templates so submission values may be used within them. + * + * @param array $config + * @param array $data + * @return array + */ + private function parseConfig(array $config, array $data) + { + foreach ($config as $key => &$value) { + $value = Parse::template(Parse::env($value), $data); + } + + return $config; + } } diff --git a/FormWithAttachments/meta.yaml b/FormWithAttachments/meta.yaml index 1becf2f..d1fc5f3 100755 --- a/FormWithAttachments/meta.yaml +++ b/FormWithAttachments/meta.yaml @@ -1,5 +1,5 @@ name: 'Form with Attachments' -version: '1.1' +version: '1.2' description: 'Sends form submissions as email with attachment' developer: 'wild' developer_url: 'https://wild.as' diff --git a/FormWithAttachments/settings.yaml b/FormWithAttachments/settings.yaml index 739a618..bf963f1 100644 --- a/FormWithAttachments/settings.yaml +++ b/FormWithAttachments/settings.yaml @@ -11,7 +11,6 @@ fields: type: suggest mode: form_with_attachments.formsets display: Formset - instructions: Select formset max_items: 1 settings: type: grid @@ -20,13 +19,16 @@ fields: recipient: type: text instructions: 'Enter recipient e-mail' + reply_to: + type: text + display: 'Reply To' + instructions: 'Enter if you want reply to a different email address' template: type: suggest mode: form_with_attachments.email_templates max_items: 1 subject: type: text - instructions: 'Enter e-mail subjecet' file_deletion: type: toggle display: File deletion