SimpleContactForm by justb3a

Just a simple contact form for ProcessWire.

WARNING: This repository is no longer maintained

This repository will not be updated. The repository will be kept available in read-only mode.

SimpleContactForm for ProcessWire

Compatibility: ProcessWire 3.x

Just a simple contact form including spam protection. See ProcessWire Forums - Support Board.

Too long to read:
Install the module, fill in module settings, execute the module: echo $modules->get('SimpleContactForm')->render();.

FAQ

Module Settings


Fill in module settings, add all fields you want to attach to the form. You could either use existing fields or create new ones. All new fields will be prefixed with scf_.

If you want to change the field settings, edit the field and change all settings there (e.g. fieldtype, required, length).

Basic Usage


In the template add just one line to include the form:

echo $modules->get('SimpleContactForm')->render();

If you want to send the form via ajax, include /site/modules/SimpleContactForm/resources/jquery.simplecontactform.js and call it:

if ($('.js-simplecontactform').length) {
  $.simplecontactform($('.js-simplecontactform'));
}

To get just the necessary part, modify your template like this:

<?php
if ($config->ajax) {
  $modules->get('SimpleContactForm')->render();
} else {
  // html, header, nav etc.
  $modules->get('SimpleContactForm')->render();
  // html, footer etc.
}

Spam Protection: Hide honeypot field using CSS


Spam bots fill in automatically all form fields. By adding an invisible field you're able to trick the bots. The key to the honeypot technique is that the form only can be sent when the honeypot field remains empty otherwise it will be treated as spam.

The honeypot technique doesn't interfere with the user experience. It demands nothing extra of them like a captcha does. In fact, user won't even notice you're using it.

All that's required is a visually hidden form field. This form adds such a field named scf-website by default but you have to make sure to add a display: none; CSS rule on it.

Example Email Message


Hi!

You have received a new message:

Name: %fullName%,
Mail: %email%,
Phone Number: %phone%,
Message: %message%,

Date: %date%

ByeBye

Logging


This module creates a log file named simplecontactform-log.txt.

Admin > Setup > Logs > Simplecontactform-log

In this file every action is logged and marked with [SUCCESS] or [FAILURE]

If a message is treated as spam an entry in the spam log file will be added containing the reason why.

2016-02-09 11:02:15 [SUCCESS] Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36, 127.0.0.1, email@test.com
2016-03-23 10:55:05 [FAILURE] This IP address was already marked as spam. IP: 127.0.0.1
2016-03-21 12:35:05 [FAILURE] This IP address submitted this form too often. IP: 111.0.5.1

Mark messages as spam


If the save messages setting is turned on you have the possibility to mark received messages as spam by adding the IP address to a blacklist.

To mark a message as spam edit the belonging page. Each page has a checkbox to matk the IP address as spam.

How to translate


All phrases like email subjects are translatable. Add the module file to the language translator and start translating. Phrases which don't exist in this file belong to the ProcessWire core. For example the messages Please enter a valid e-mail address or Missing required value.

Relevant Files:

  • site/modules/SimpleContactForm/lib/SpamProtection.php
  • wire/modules/Inputfield/InputfieldEmail.module
  • wire/core/InputfieldWrapper.php

Depending on the fields you added to the form there might be some other files.

Render multiple instances


You can pass options as an array which overwrites the defaults set up in module settings. You don't have to pass all available keys, if you skip one, the input from module settings will be used (for example emailServer stays the same).

Here is an example:

$scf = $modules->get('SimpleContactForm');

$options = array(
  'emailSubject' => 'Test Subject',
  'emailAdd' => true,
  'emailAddSubject' => 'hi there',
  'emailAddMessage' => 'Hi %scf_fullName%',
  'emailAddReplyTo' => $input->scf_email,
  'emailAddTo' => $input->scf_email
);

echo $scf->render($options);

Available Keys

keytypedescription
allFieldsstringcomma-separated list of all fields
submitNamestringif you use more than one form at one page, you have to pass the submit button name. That means, you have to use different submit names
btnClassstringadd custom submit button class(es), defaults to button
btnTextstringadd custom submit button text, defaults to Send
actionstringset specific form action, defaults to same page './'
sendEmailsbooleanwhether Emails should be sent
redirectSamePagebooleanRedirect to the same page after successfull submission to prevent form resubmission. OPTIONAL.
redirectPageintegerRedirect to a specific page after successfull submission. OPTIONAL: If you prefer to stay on the same page, just leave this field empty.
emailMessagestringemail message
successMessagestringsuccess message
errorMessagestringerror message
emailAddMessagestringemail message
emailSubjectstringemail subject
emailTostringemail address of recipient
emailServerstringserver address
emailAddbooleanset this to true if you want to send more than one email
emailAddSubjectstringemail subject
emailAddTostringemail to
emailAddReplyTostringemail reply to
saveMessagesbooleanwhether to save received messages
saveMessagesParentintegerAll items created and managed will live under the parent you select here
saveMessagesTemplateintegerTemplate for received messages
markuparrayoverwrite markup
classesarrayoverwrite classes
prependMarkupstringprepend some markup/content
appendMarkupstringappend some markup/content

To get an overview of what's possible, have a look at How to overwrite classes and markup

More options for email templates, emailTo, ..

You have the full ProcessWire API available. Feel free to add any value you want to!

$options['emailTo'] = $input->email;
$options['emailSubject'] = $page->title;
$options['emailMessage'] = $message;

$message could be a single partial including the whole message.

More than one contact form on a page

If you need more than one contact form on a single page you have to pass the $options['submitName'] option. That means, you have to create a unique name for each submit button.

Send more than one mail

Sometimes you need to send different emails to different people. Here is an example how this works:

$options['emailAdd'] = true;
$options['emailAddSubject'] = 'hi there';
$options['emailAddMessage'] = $anotherMessage;
$options['emailAddTo'] = $input->email_recommend;

It's important to set $options['emailAdd] = true;. Otherwise it won't send an additional email. If you want to use the same email subject (for example), you can skip this part and it will use the default value.

Add custom validation


You could add additional custom validation after the form was processed. This allows custom/extra validation and field manipulation.

$this->addHookBefore('SimpleContactForm::processValidation', function(HookEvent $event) {
  $form = $event->arguments(0);
  $email = $form->get('scf_email');

  // add error if email address already exists
  if (count($this->users->find("email={$email->value}")) > 0) { // attach an error to the field
    $email->error(__('This email address is already registered.')); // it will be displayed along the field
  }
});

Install and use modules at your own risk. Always have a site and database backup before installing new modules.

Twitter updates

  • New TinyMCE 6.2.0 Inputfield released for ProcessWire— More
    28 October 2022
  • This week we take a look at a new rich text editor for ProcessWire, why we chose it, some highlights, screenshots, and why we think you’ll like it— More
    21 October 2022
  • This week we have ProcessWire 3.0.206 on the dev branch and a new version of the ProDevTools UserActivity module, which we'll take a closer look at in this post— More
    7 October 2022

Latest news

  • ProcessWire Weekly #446
    In the 446th issue of ProcessWire Weekly we're going to check out the latest weekly updates, introduce a new third party module called AlpineJS, and more. Read on!
    Weekly.pw / 26 November 2022
  • New rich text editor for ProcessWire
    This week we take a look at a new rich text editor for ProcessWire, why we chose it, some highlights, screenshots, and why we think you’ll like it.
    Blog / 21 October 2022
  • Subscribe to weekly ProcessWire news

“To Drupal, or to ProcessWire? The million dollar choice. We decided to make an early switch to PW. And in retrospect, ProcessWire was probably the best decision we made. Thanks are due to ProcessWire and the amazing system and set of modules that are in place.” —Unni Krishnan, Founder of PigtailPundits