https://github.com/ttskch/contact-form
PHP utility classes to implement general contact form
https://github.com/ttskch/contact-form
contact-form email form sendmail
Last synced: 11 months ago
JSON representation
PHP utility classes to implement general contact form
- Host: GitHub
- URL: https://github.com/ttskch/contact-form
- Owner: ttskch
- License: mit
- Created: 2018-07-17T09:08:48.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2019-05-29T12:19:36.000Z (over 6 years ago)
- Last Synced: 2025-02-08T12:16:14.923Z (about 1 year ago)
- Topics: contact-form, email, form, sendmail
- Language: PHP
- Homepage:
- Size: 35.2 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# contact-form
[](https://travis-ci.com/ttskch/contact-form)
[](https://packagist.org/packages/ttskch/contact-form)
[](https://packagist.org/packages/ttskch/contact-form)
PHP utility classes to implement general contact form (also with confirmation view). It's maybe useful to build contact form on WordPress or pure PHP site.
## Requirements
* PHP 5.4+
* Configuring `date.timezone` in php.ini
## Supported features
* Csrf protection with session and hidden input tag
* Server side validation for submitted values
* **Short hands to print submitted value itself, validation errors, "selected" option, "checked" option**
* Attaching files and handle them easily in the same session
* Sending email which contains submissions information easily
## Installation
```bash
$ composer require ttskch/contact-form
```
or
```bash
$ git clone git@github.com:ttskch/contact-form.git
$ cd contact-form
$ composer install --no-dev
# If your web site is not composer-friendly, upload whole "contact-form" directory by hand.
```
## Usage
```php
validateAndRedirectAfterSelfPosted('./confirm.php', $requiredKeys, $emailKeys);
?>
= $cf->csrfHiddenInput(); ?>
= $cf->presentError('Name'); ?>
= $cf->presentError('Email'); ?>
presentSelected('Gender', 'Male', $default = true); ?>>Male
presentSelected('Gender', 'Female'); ?>>Female
presentSelected('Gender', 'Other'); ?>>Other
Confirm
```
```php
rejectAccessWithoutSubmissions('./index.php');
// after posted, validate csrf and redirect to next page
$cf->validateAndRedirectAfterSelfPosted('./thanks.php');
?>
= $cf->csrfHiddenInput(); ?>
= $cf->present('Name'); ?>
= $cf->present('Email'); ?>
= $cf->present('Gender'); ?>
Send
Back
```
```php
rejectAccessWithoutSubmissions('./index.php');
$template = <<present('Name', false),
$cf->present('Email', false),
$cf->present('Gender', false),
]);
$cf->sendEmail(
'you@email.com', // to
'from@email.com', // from
'Your Name', // from name
'Got inquiry', // subject
$body // body
);
// clear submissions after sending email
// by this, if users reload thanks.php after sending email they will be redirected to index.php
$cf->clearSubmissions();
?>
Form is successfully submitted!
```
See [demo](demo) code or run it on your local to learn more :)
### Precaution
Because this utility calls [header()](https://www.php.net/manual/en/function.header.php) function to redirect, you must execute instantiation and some methods before any actual output is sent.