Skip to content
This repository has been archived by the owner on Jul 8, 2021. It is now read-only.

Commit

Permalink
Emails now send in HTML and are formatted better
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean-PierreGassin committed Mar 26, 2018
1 parent abc112f commit f4c4b33
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 11 deletions.
17 changes: 15 additions & 2 deletions database/seeds/SettingsTableSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,31 @@ class SettingsTableSeeder extends Seeder
{
public function run()
{
$setting = $this->findSetting('forms.default_email');
$setting = $this->findSetting('forms.default_to_email');

if (!$setting->exists) {
$setting->fill([
'display_name' => 'Default Enquiry Email',
'display_name' => 'Default Enquiry To Email',
'value' => '',
'details' => 'The default email address to send form enquiries to',
'type' => 'text',
'order' => 1,
'group' => 'Forms',
])->save();
}

$setting = $this->findSetting('forms.default_from_email');

if (!$setting->exists) {
$setting->fill([
'display_name' => 'Default Enquiry From Email',
'value' => '',
'details' => 'The default email address to use as the sender address for form enquiries',
'type' => 'text',
'order' => 2,
'group' => 'Forms',
])->save();
}
}

protected function findSetting($key)
Expand Down
4 changes: 0 additions & 4 deletions resources/views/enquiries/view.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,6 @@
<h3 class="panel-title">Submitted Data</h3>

@foreach ($enquiry->data as $key => $value)
@if (in_array($key, ['_token', 'id']))
@continue
@endif

<div class="panel panel-bordered">
<div class="panel-body">
<b>{{ str_replace('_', ' ', $key) }}</b>
Expand Down
9 changes: 4 additions & 5 deletions src/Http/Controllers/EnquiryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
use Pvtl\VoyagerForms\Form;
use Pvtl\VoyagerForms\FormEnquiry;
use Pvtl\VoyagerForms\Traits\DataType;
use Pvtl\VoyagerForms\Traits\Email;
use Pvtl\VoyagerFrontend\Helpers\ClassEvents;
use TCG\Voyager\Facades\Voyager;
use TCG\Voyager\Http\Controllers\VoyagerBreadController as BaseVoyagerBreadController;

class EnquiryController extends BaseVoyagerBreadController
{
use DataType;
use DataType, Email;

/**
* @param Request $request
Expand Down Expand Up @@ -55,7 +56,7 @@ public function create(Request $request)
*/
public function store(Request $request)
{
$formData = $request->all();
$formData = $request->except(['_token', 'id']);
$form = Form::where('id', $request->input('id'))->first();

if ($form->hook) {
Expand All @@ -69,9 +70,7 @@ public function store(Request $request)
'ip_address' => $_SERVER['REMOTE_ADDR'],
])->save();

foreach (explode(',', str_replace(' ', '', $form->mailto)) as $recipient) {
mail($recipient, "New Form Enquiry - $form->title", implode("\r", $formData));
}
$this->sendEmail($form, $formData);

return redirect()
->back();
Expand Down
36 changes: 36 additions & 0 deletions src/Traits/Email.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Pvtl\VoyagerForms\Traits;

use Pvtl\VoyagerForms\Form;

trait Email
{
public function sendEmail(Form $form, array $formData)
{
$recipients = explode(',', str_replace(' ', '', $form->mailto));

$subject = "New Form Enquiry - $form->title";

$headers = "From: " . setting('forms.default_from_email') . "\r\n";
$headers .= "Reply-To: ". setting('forms.default_from_email') . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

$emailData = [];

array_unshift($emailData, "<html><body>");

foreach ($formData as $key => $value) {
$realKey = str_replace('_', ' ', $key);
$emailData[] = "$realKey: $value<br>";
}

$emailData = implode("\r\r", $emailData);
$emailData .= "</body></html>";

foreach ($recipients as $to) {
mail($to, $subject, $emailData, $headers);
}
}
}

0 comments on commit f4c4b33

Please sign in to comment.