Skip to content

Commit

Permalink
Merge pull request #4 from billmn/patch-1
Browse files Browse the repository at this point in the history
Support for ReplyTo and config parsing
  • Loading branch information
Stefan Göllner authored Apr 9, 2020
2 parents cd62040 + ed92c9e commit 6e2542f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 11 deletions.
47 changes: 39 additions & 8 deletions FormWithAttachments/FormWithAttachmentsListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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'];
Expand All @@ -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;
}
}
2 changes: 1 addition & 1 deletion FormWithAttachments/meta.yaml
Original file line number Diff line number Diff line change
@@ -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'
6 changes: 4 additions & 2 deletions FormWithAttachments/settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ fields:
type: suggest
mode: form_with_attachments.formsets
display: Formset
instructions: Select formset
max_items: 1
settings:
type: grid
Expand All @@ -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
Expand Down

0 comments on commit 6e2542f

Please sign in to comment.