In Nicepage you can create simple HTML forms using the Form control. By default, the Form control does not have form action scripts (server scripts). If you have a CMS, there are no problems with creating forms. Please refer to this article for more details. Therefore, to create a working HTML form in Nicepage, sending emails, for example, you have to use a third-party script.

Generally, you can use any script. However, please note that each script requires specific form classes, such are names, IDs, and, sometimes, you have to follow a particular form structure.

We will create a simple Contact Form sending fields' data to the email address specified by an action script. In this example, we will send an email to your email address.

In Nicepage, the Form control uses the predefined field names. Therefore you are unable to change Name attributes.

Step 1. Adding a Form

  1. Add the Form Control from the Add Menu.

  2. Enable Form fields you would like to see in your form.

  3. Specify a path to a script in the Form Settings -> Send To field. The path to a PHP script for sending emails can be either relative or absolute. In the screenshot below, the entered field value is contact.php, meaning that in this example a PHP script is located in the root folder of a website. The script URL usually contains a path and file name.

    form-action.png

  4. Export your website in HTML. Upload your website to a server.

IMPORTANT: Please verify email sending to be set on your server.

Step 2. Using a PHP Script

After your Form is ready, to send emails, you need a PHP script. You may use examples below to create or copy to the contact.php file located on your server.

Code Example 1

The simplest email action using the Name, Email, and Message fields. With the proper setup it can be used to send emails to and from any domain. Replace replace the placeholders, like YOUR_EMAIL with the email data.

<?php
$field_name = $_POST['name'];
$field_email = $_POST['email'];
$field_message = $_POST['message'];

//Specify the message recipient:
$mail_to = 'YOUR_EMAIL';
$mail_from = 'EMAIL_USED_FOR_SENDING';
$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: '.$mail_from."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);

//Show a json message about the successful or unsuccessful sending a message
if ($mail_status) {
   $resArray= array('success' => true);
   header('Content-Type: application/json');
   echo json_encode($resArray);
} else {
   $resArray= array('success' => false);
   header('Content-Type: application/json');
   echo json_encode($resArray);
}

?>

Recommendations

  1. Use EMAIL_USED_FOR_SENDING only of the same domain as your website.

  2. Set up the local MTA (SMTP server) or sendmail to send emails. Please read the following article: https://gist.github.com/adamstac/7462202. Please verify that you have set the proper hostname for your sending server. Otherwise, your mail server may not accept emails.

  3. OPTIONAL STEP. You can create matching A, and Reverse DNS (PTR) records for your server. Use the server Hostname like in the article recommended above. For example: srv1.mydomain.com <-> 14.22.36.42. The A record can be created in the Admin panel of your domain registrar or DNS provider. The PTR can be configured in the Admin panel of your hosting. For more instructions, please read to your provider or registrar documentation.

  4. OPTIONAL STEP. You can configure the SPF record for your domain, so emails sent with your server IP address are considered valid and do not fall into the junk mail. For more information about SPF records, please read this article: https://blog.returnpath.com/how-to-build-your-spf-record-in-5-simple-steps/. The SPF record can be configured in the Admin panel of your domain registrar or DNS provider.

Code Example 2

The simplest script to send emails using the Pear Mail Library. You can use 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)) {
     $resArray= array('success' => true);
     header('Content-Type: application/json');
     echo json_encode($resArray);
} else {
     $resArray= array('success' => false);
     header('Content-Type: application/json');
     echo json_encode($resArray);
}
?>

The major disadvantage of this script is that it stores the password to your Google account which is not encrypted.

DISCLAIMER: Both example codes are used from open resources on the Internet. These examples and were provided for informational purposes only as recommendations. Please note, everything you do with these provided code examples, you do at your own risk.

In Nicepage you can create simple HTML forms using the Form control. By default, the Form control does not have form action scripts (server scripts). If you have a CMS, there are no problems with creating forms. Please refer to this [article](page:4216) for more details. Therefore, to create a working HTML form in Nicepage, sending emails, for example, you have to use a third-party script. Generally, you can use any script. However, please note that each script requires specific form classes, such are names, IDs, and, sometimes, you have to follow a particular form structure. We will create a simple Contact Form sending fields' data to the email address specified by an action script. In this example, we will send an email to your email address. In Nicepage, the Form control uses the predefined field names. Therefore you are unable to change Name attributes. ## Step 1. Adding a Form 1. Add the **Form** Control from the Add Menu. 2. Enable Form fields you would like to see in your form. 3. Specify a path to a script in the **Form Settings -> Send To** field. The path to a PHP script for sending emails can be either relative or absolute. In the screenshot below, the entered field value is *contact.php*, meaning that in this example a PHP script is located in the root folder of a website. The script URL usually contains a path and file name. !form-action.png! 4. Export your website in HTML. Upload your website to a server. **IMPORTANT:** Please verify email sending to be set on your server. ## Step 2. Using a PHP Script After your Form is ready, to send emails, you need a PHP script. You may use examples below to create or copy to the *contact.php* file located on your server. ### Code Example 1 The simplest email action using the Name, Email, and Message fields. With the proper setup it can be used to send emails to and from any domain. Replace replace the placeholders, like *YOUR_EMAIL* with the email data. <?php $field_name = $_POST['name']; $field_email = $_POST['email']; $field_message = $_POST['message']; //Specify the message recipient: $mail_to = 'YOUR_EMAIL'; $mail_from = 'EMAIL_USED_FOR_SENDING'; $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: '.$mail_from."\r\n"; $headers .= 'Reply-To: '.$field_email."\r\n"; $mail_status = mail($mail_to, $subject, $body_message, $headers); //Show a json message about the successful or unsuccessful sending a message if ($mail_status) { $resArray= array('success' => true); header('Content-Type: application/json'); echo json_encode($resArray); } else { $resArray= array('success' => false); header('Content-Type: application/json'); echo json_encode($resArray); } ?> **Recommendations** 1. Use EMAIL_USED_FOR_SENDING only of the same domain as your website. 2. Set up the local MTA (SMTP server) or sendmail to send emails. Please read the following article: https://gist.github.com/adamstac/7462202. Please verify that you have set the proper hostname for your sending server. Otherwise, your mail server may not accept emails. 3. OPTIONAL STEP. You can create matching A, and Reverse DNS (PTR) records for your server. Use the server Hostname like in the article recommended above. For example: *srv1.mydomain.com <-> 14.22.36.42*. The A record can be created in the Admin panel of your domain registrar or DNS provider. The PTR can be configured in the Admin panel of your hosting. For more instructions, please read to your provider or registrar documentation. 4. OPTIONAL STEP. You can configure the SPF record for your domain, so emails sent with your server IP address are considered valid and do not fall into the junk mail. For more information about SPF records, please read this article: https://blog.returnpath.com/how-to-build-your-spf-record-in-5-simple-steps/. The SPF record can be configured in the Admin panel of your domain registrar or DNS provider. ### Code Example 2 The simplest script to send emails using the *[Pear Mail Library](https://pear.php.net/package/Mail)*. You can use 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)) { $resArray= array('success' => true); header('Content-Type: application/json'); echo json_encode($resArray); } else { $resArray= array('success' => false); header('Content-Type: application/json'); echo json_encode($resArray); } ?> The major disadvantage of this script is that it stores the password to your Google account which is not encrypted. **DISCLAIMER:** Both example codes are used from open resources on the Internet. These examples and were provided for informational purposes only as recommendations. Please note, everything you do with these provided code examples, you do at your own risk.