Summary
- Overview
- Add Contact Form
- Add PHP Script
Overview
In Nicepage, you can create simple HTML forms using the Contact Form Element. The Contact Form Element is set to send data to the account email if you have an active Nicepage license by default. Alternative to the built-in Email Submission, you can create a working HTML Contact Form using a third-party script. Generally, you can use any script.
NOTE: Each script requires specific form classes, such as names and IDs, and, sometimes, you have to follow a particular form structure. Nicepage uses the predefined Field Names for the Contact Form Element. You must not change the Name Attributes.
If you use WordPress or Joomla, you can use the popular methods to submit forms. You can learn about other Contact From Submit options.
Add Contact Form
Please take the following steps to add the Contact From Element and assign it to a script to send emails to your email address.
- Add the Contact Form Element from the Add Panel.
- Edit the Contact Form Fields.
- Specify the path to the Script file in the Contact Form Submit URL field.
- Export your website in HTML. Upload your website to a server.
NOTE: The path to the PHP script can be absolute or relative. The script URL usually contains the Path and File Name. In this example, we will use contact.php, located in the website's root folder.
IMPORTANT: Please verify your server must be set to send emails.
Add PHP Script
After adding the Contact Form, you need to create a file containing the PHP script to send emails. Create the contact.php file in the root of your server folder and paste one of the following code examples.
Sample 1
The following is the simplest Email Action using the Name, Email, and Message fields. Replace the placeholders, like YOUR_EMAIL with the email data. You need the proper server setup to send emails to and from any domain.
<?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
- EMAIL_USED_FOR_SENDING must be of the same domain as your website.
- Set up the local MTA (SMTP server) or sendmail to send emails.
Please read the following article. Please verify that you have set the proper Hostname for your sending server. Otherwise, your mail server may not accept emails.
- OPTIONAL. You can create your server's matching A and Reverse DNS (PTR) records. Use the server Hostname like in this article.
For example: srv1.mydomain.com <-> 14.22.36.42. You can create the A record in the Admin panel of your domain registrar or DNS provider and configure the PTR in the Admin panel of your hosting. For more instructions, please read your provider or registrar's documentation.
- OPTIONAL. You can also configure the SPF record for your domain, so emails sent with your server IP address are considered valid and do not fall into junk mail.
For more information about SPF records, please read this article. You can configure the SPF record in the Admin panel of your domain registrar or DNS provider.
Sample 2
The following is 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 main disadvantage of this script is it stores the password to your Google account and is not encrypted.
DISCLAIMER: These examples are provided for informational purposes only as recommendations. You can find both code examples at open resources on the Internet.
Please note that everything you do with these provided code examples is at your own risk.
## Summary
- Overview
- Add Contact Form
- Add PHP Script
- Sample 1
- Sample 2
## Overview
In Nicepage, you can create simple HTML forms using the Contact Form Element. The Contact Form Element is set to send data to the account email if you have an active Nicepage license by default. Alternative to the built-in Email Submission, you can create a working HTML Contact Form using a third-party script. Generally, you can use any script.
NOTE: Each script requires specific form classes, such as names and IDs, and, sometimes, you have to follow a particular form structure. Nicepage uses the predefined Field Names for the Contact Form Element. You must not change the Name Attributes.
If you use WordPress or Joomla, you can use the popular methods to submit forms. You can learn about other [Contact From Submit](page:4216) options.
## Add Contact Form
Please take the following steps to add the Contact From Element and assign it to a script to send emails to your email address.
1. Add the **Contact Form** Element from the Add Panel.
2. Edit the Contact Form Fields.
3. Specify the path to the Script file in the Contact Form Submit URL field.
4. Export your website in HTML. Upload your website to a server.
NOTE: The path to the PHP script can be absolute or relative. The script URL usually contains the Path and File Name. In this example, we will use *contact.php*, located in the website's root folder.
IMPORTANT: Please verify your server must be set to send emails.
!form-action.png!
## Add PHP Script
After adding the Contact Form, you need to create a file containing the PHP script to send emails. Create the *contact.php* file in the root of your server folder and paste one of the following code examples.
### Sample 1
The following is the simplest Email Action using the Name, Email, and Message fields. Replace the placeholders, like *YOUR_EMAIL* with the email data. You need the proper server setup to send emails to and from any domain.
<?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. EMAIL_USED_FOR_SENDING must be 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. You can create your server's matching A and Reverse DNS (PTR) records. Use the server Hostname like in this [article](https://gist.github.com/adamstac/7462202).
For example: *srv1.mydomain.com <-> 14.22.36.42*. You can create the A record in the Admin panel of your domain registrar or DNS provider and configure the PTR in the Admin panel of your hosting. For more instructions, please read your provider or registrar's documentation.
4. OPTIONAL. You can also configure the SPF record for your domain, so emails sent with your server IP address are considered valid and do not fall into 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/). You can configure the SPF record in the Admin panel of your domain registrar or DNS provider.
### Sample 2
The following is 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 main disadvantage of this script is it stores the password to your Google account and is not encrypted.
**DISCLAIMER: These examples are provided for informational purposes only as recommendations. You can find both code examples at open resources on the Internet.**
**Please note that everything you do with these provided code examples is at your own risk.**
##