Themler includes Form control which creates regular HTML Forms.
But it does not provide form action scripts (server script). In other words you can build Form in Themler but to get the form to work (to send emails, for example) you should use third-party scripts.
In general, you can use any script. But each script requires specific form classes (names, IDs) and sometimes specific form structure.
Let's create a simple Contact Us form which sends the data entered in the fields to the email address specified in the action script (to your email address, for example).
For our example we need a form with fields that have specific name
attributes.
Step #1
Create the Form in Themler (for more details please refer to the Form control description):
1. Add Form control available under the Insert tab >> More.
2. Add two Inputs and one Textarea to the Form.
3. Add Button control from Insert tab and enable "Submit Button" option.
4. Specify the required attributes for Form controls:
- Input text field with
cf-name
name for "Name" field; - Input text or email field with
cf-email
name for "Email" field; - Text Area with
cf-message
name for "Message" field;
Our form is almost ready.
5. Now we need to specify the path to the action script under the form settings.
In the Form action
field we should specify the relative or absolute path to the php script that sends emails. On the screenshot below the action field value is contact.php
. It means that the php script contact.php
is located in the root folder of our website. But there is no difference where this file is located. You can put it into the different directory on the server, for example.
NOTE: If you build HTML websites with Themler you should not locate custom files inside the theme directory. Themler will remove all extra files on each saving. You can put the custom php file into the neighboring folder.
Step #2
The Form is ready. Now we need a php script.
Copy the code provided below to the contact.php
file located on your server.
Please note that the server should be configured to send emails.
Example #1.
The simplest send email action.
<?php
$field_name = $_POST['cf-name'];
$field_email = $_POST['cf-email'];
$field_message = $_POST['cf-message'];
//Specify the message recipient:
$mail_to = 'YOUR_EMAIL';
$subject = 'Message from a site visitor '.$field_name;
//The email content:
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
//Show a javascript message about the successful or unsuccessful sending a message
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Message successfully sent.');
window.location = 'URL_TO_OPEN_AFTER_SUBMIT';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed.');
window.location = 'URL_TO_OPEN_AFTER_SUBMIT';
</script>
<?php
}
?>
Example #2.
The simplest send email script which uses Pear Mail Library and send email from your Gmail or any other account.
// Pear Mail Library
require_once "Mail.php";
//Specify the message recipient:
$from = '<YOUR_EMAIL@gmail.com>';
$to = '<YOUR_EMAIL@gmail.com>';
$field_name = $_POST['cf-name'];
$field_email = $_POST['cf-email'];
$field_message = $_POST['cf-message'];
$subject = 'Message from a site visitor '.$field_name;
//The email content:
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;
$body = $body_message;
$headers = array(
'From' => $from,
'To' => $to,
'Subject' => $subject
);
// SMTP Google Settings
$smtp = Mail::factory('smtp', array(
'host' => 'ssl://smtp.gmail.com',
'port' => '465',
'auth' => true,
'username' => 'YOUR_EMAIL@gmail.com',
'password' => 'YOUR_PASSWORD'
));
$mail = $smtp->send($to, $headers, $body);
//Show a javascript message about the successful or unsuccessful sending a message
if (PEAR::isError($mail)) {
?>
<script language="javascript" type="text/javascript">
alert('Message failed.');
window.location = 'URL_TO_OPEN_AFTER_SUBMIT';
</script>
<?php
} else {
?>
<script language="javascript" type="text/javascript">
alert('Thank you for the message. We will contact you shortly.');
window.location = 'URL_TO_OPEN_AFTER_SUBMIT';
</script>
<?php
}
?>
The disadvantage of this example is that the action script stores not encrypted password to your Google account.
Themler includes [Form](page:71585) control which creates regular HTML Forms. But it does not provide form action scripts (server script). In other words you can build Form in Themler but to get the form to work (to send emails, for example) you should use third-party scripts. In general, you can use any script. But each script requires specific form classes (names, IDs) and sometimes specific form structure. Let's create a simple Contact Us form which sends the data entered in the fields to the email address specified in the action script (to your email address, for example). For our example we need a form with fields that have specific `name` attributes. ### Step #1 Create the Form in Themler (for more details please refer to the [Form](page:71585) control description): 1. Add **Form** control available under the **Insert tab** >> **More**. 2. Add two **Inputs** and one **Textarea** to the Form. 3. Add **Button** control from **Insert tab** and enable "Submit Button" option. 4. Specify the required attributes for Form controls: - Input text field with `cf-name` name for "Name" field; - Input text or email field with `cf-email` name for "Email" field; - Text Area with `cf-message` name for "Message" field; !form-2.png! Our form is almost ready. 5. Now we need to specify the path to the action script under the form settings. In the Form `action` field we should specify the relative or absolute path to the php script that sends emails. On the screenshot below the action field value is `contact.php`. It means that the php script `contact.php` is located in the root folder of our website. But there is no difference where this file is located. You can put it into the different directory on the server, for example. **NOTE:** If you build HTML websites with Themler you should not locate custom files inside the theme directory. Themler will remove all extra files on each saving. You can put the custom php file into the neighboring folder. !form-1.png! ### Step #2 The Form is ready. Now we need a php script. Copy the code provided below to the `contact.php` file located on your server. Please note that the server should be configured to send emails. #### Example #1. The simplest send email action. `<?php $field_name = $_POST['cf-name']; $field_email = $_POST['cf-email']; $field_message = $_POST['cf-message'];` //Specify the message recipient: `$mail_to = 'YOUR_EMAIL'; $subject = 'Message from a site visitor '.$field_name;` //The email content: `$body_message = 'From: '.$field_name."\n"; $body_message .= 'E-mail: '.$field_email."\n"; $body_message .= 'Message: '.$field_message; $headers = 'From: '.$field_email."\r\n"; $headers .= 'Reply-To: '.$field_email."\r\n"; $mail_status = mail($mail_to, $subject, $body_message, $headers);` //Show a javascript message about the successful or unsuccessful sending a message `if ($mail_status) { ?> <script language="javascript" type="text/javascript"> alert('Message successfully sent.'); window.location = 'URL_TO_OPEN_AFTER_SUBMIT'; </script> <?php } else { ?> <script language="javascript" type="text/javascript"> alert('Message failed.'); window.location = 'URL_TO_OPEN_AFTER_SUBMIT'; </script> <?php } ?>` #### Example #2. The simplest send email script which uses [Pear Mail Library][1] and send email from your Gmail or any other account. // Pear Mail Library `require_once "Mail.php";` //Specify the message recipient: `$from = '<YOUR_EMAIL@gmail.com>'; $to = '<YOUR_EMAIL@gmail.com>'; $field_name = $_POST['cf-name']; $field_email = $_POST['cf-email']; $field_message = $_POST['cf-message']; $subject = 'Message from a site visitor '.$field_name;` //The email content: `$body_message = 'From: '.$field_name."\n"; $body_message .= 'E-mail: '.$field_email."\n"; $body_message .= 'Message: '.$field_message; $body = $body_message;` `$headers = array( 'From' => $from, 'To' => $to, 'Subject' => $subject );` // [SMTP Google Settings][2] `$smtp = Mail::factory('smtp', array( 'host' => 'ssl://smtp.gmail.com', 'port' => '465', 'auth' => true, 'username' => 'YOUR_EMAIL@gmail.com', 'password' => 'YOUR_PASSWORD' ));` `$mail = $smtp->send($to, $headers, $body);` //Show a javascript message about the successful or unsuccessful sending a message `if (PEAR::isError($mail)) { ?> <script language="javascript" type="text/javascript"> alert('Message failed.'); window.location = 'URL_TO_OPEN_AFTER_SUBMIT'; </script> <?php } else { ?> <script language="javascript" type="text/javascript"> alert('Thank you for the message. We will contact you shortly.'); window.location = 'URL_TO_OPEN_AFTER_SUBMIT'; </script> <?php } ?>` The disadvantage of this example is that the action script stores not encrypted password to your Google account. [1]: https://pear.php.net/package/Mail [2]: https://support.google.com/a/answer/176600?hl=en