Skip to content

Commit

Permalink
add settings and email out functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
cburyta committed May 5, 2012
1 parent 1501d03 commit c67ca78
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 19 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lib/settings.php
2 changes: 1 addition & 1 deletion examples/example1.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
optionally do things like hilight error input fields with a red border with CSS.
-->

<form name="contactForm" action="lib/PHPContactor.setup.php" method="post">
<form name="contactForm" action="../lib/PHPContactor.setup.php" method="post">

<!-- ERROR PRINTING METHOD ONE - GROUP -->
<?php /*
Expand Down
30 changes: 30 additions & 0 deletions lib/PHPContactor.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public function getErrors()
*/
public function submittedAndValid()
{
var_dump($_POST[$this->settings['form_name']]);exit;
// @todo: check form was submitted, then validate
}

Expand All @@ -61,6 +62,7 @@ public function submittedAndValid()
public function process()
{
// @todo: process the form, get the values and send an email
$this->sendEmail();
}

/**
Expand Down Expand Up @@ -128,4 +130,32 @@ public function printCaptcha()
{
return '<p>@todo build captcha</p>';
}

/**
* Send the email with the info
* @todo: right now this is a very basic text email, may
* be good to add template support or something later.
*/
protected function sendEmail()
{
// build the message
$message = "\nEmail form submission.";

// for every value, we'll add a line.
foreach($this->values as $name => $value)
{
$message .= "\n$name : $value";
}

// wordwrap at 70 chars
$message = wordwrap($message, 70);

// add headers to support from addressi
$from = (empty($this->settings['from_name'])) ? $from_email : $from_name . ' <' . $from_email . '>';
$headers = 'From: ' . $from . "\r\n" .
'Reply-To: ' . $from . "\r\n" .

// send email
mail($this->settings['to_emails'], $this->settings['subject'], $message);
}
}
46 changes: 28 additions & 18 deletions lib/PHPContactor.setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,35 @@
require_once('PHPContactor.class.php');

// MOST SETTINGS WOULD GO HERE
/// E.G. THE FROM ADDRESS, CAPTCHA KEYS, SUBJECT LINES ETC...
$settings = array(
'form_name' => 'contactForm', // this need to be in sync with the form name and the form fields
'to_emails' => 'To Name<to_name@example.com>,To Name 2<to_name_2@example.com>',
'subject' => 'Subject line here',
'captcha_service' => 'recaptcha',
'recaptcha_api' => 'API-KEY-HERE',
'success_redirect' => '/thankyou-page', // could be used to redirect to to any page on success
'error_format' => '<div class="error">%%error%%</div>', // this would provide easy customization of error messages if needed
'error_class' => 'error', // if the field has an error, then this is the class that's returned in the printErrorClass method
);
// E.G. THE FROM ADDRESS, CAPTCHA KEYS, SUBJECT LINES ETC...
// Also if your working on dev, you can use a settings.php file to store these in rather than placing your stuff here.
if(file_exists('settings.php'))
{
include('settings.php');
}
else
{
$settings = array(
'form_name' => 'contactForm', // this need to be in sync with the form name and the form fields
'to_emails' => 'To Name<to_name@example.com>,To Name 2<to_name_2@example.com>',
'from_name' => '', // optional, a name for the from header
'from_email' => 'email@fromdomain.com', // email address the form will be sent on behalf of
'subject' => 'Subject line here',
'captcha_service' => 'recaptcha',
'recaptcha_api' => 'API-KEY-HERE',
'success_redirect' => '/thankyou-page', // could be used to redirect to to any page on success
'error_format' => '<div class="error">%%error%%</div>', // this would provide easy customization of error messages if needed
'error_class' => 'error', // if the field has an error, then this is the class that's returned in the printErrorClass method
);

// Required fields here
// key == name of the field (e.g. 'email' for the field with name="contactForm[name]")
$required = array(
'name' => true,
'email' => true,
'phone' => true,
);
// Required fields here
// key == name of the field (e.g. 'email' for the field with name="contactForm[name]")
$required = array(
'name' => true,
'email' => true,
'phone' => true,
);
}

//
// SETUP THE FORM PROCESSING CLASS
Expand Down

0 comments on commit c67ca78

Please sign in to comment.