Creating a Contact Form with FormMail

Creating a Contact Form with FormMail Header image
Creating a Contact Form with FormMail Header Image

You want visitors to reach you through your website without exposing your email address. A FormMail contact form routes submissions from your HTML form through a Perl script hosted in your account’s cgi-bin directory, then delivers them directly to your inbox. By the end of this guide, you will have FormMail installed, configured, and connected to a working HTML form.

Important: FormMail is a long-standing Perl script and remains supported on InMotion Hosting shared plans, but it is not the only way to build a contact form today. If you would rather use a WordPress plugin or another approach, browse the Support Center for current contact-form options.

Prerequisites

  • An active InMotion Hosting Shared, VPS, or Dedicated account. CGI and Perl execute by default on shared hosting, so no additional activation step is required.
  • Access to cPanel File Manager or Secure File Transfer Protocol (SFTP).
  • A text editor that saves files with Unix line endings (LF, not CRLF). Saving the Perl script with Windows line endings is a common cause of 500 errors when the script runs.
  • An email address associated with your hosting account that will receive form submissions.

Step 1: Download the FormMail Script

The NMS FormMail script is the version you want. It is a drop-in replacement for the original FormMail and is maintained on SourceForge. Download the “compat” release, the recommended package for most users, from Matt’s Script Archive or the NMS project on SourceForge. The compat release supports older form configurations when migrating an existing site.

Step 2: Upload the Script to cgi-bin

The cgi-bin directory lives inside public_html on InMotion shared hosting and is the location on your server where executable scripts like FormMail run. Your web server is configured to run scripts placed there, so uploading FormMail.pl to any other folder will prevent it from executing.

  1. Unzip the downloaded archive on your local computer.
  2. Log in to cPanel. See How to Log in to cPanel if you need help.
  3. In the Files section, click File Manager.
  4. Navigate to the cgi-bin folder inside public_html.
  5. Upload FormMail.pl to that folder.

Step 3: Configure the Script

Two variables control which domains and email addresses the script will accept. Incorrectly setting them is the most common reason FormMail rejects submissions, so read both edits before saving.

  1. Open FormMail.pl in a text editor.
  2. Search for @referers. In the compat release this sits around line 47. This variable tells the script which domains are allowed to submit the form. Replace the placeholder contents with your domain in two formats, one without “www” and one with “www”, separated by a space. Remove “localhost” if it appears.
  3. Search for @allow_mail_to. This variable lists the email addresses that may receive submissions. Replace any placeholder value with the address where you want form emails delivered. Remove “localhost” if it appears.
  4. Save the file, making sure your editor uses Unix line endings.

Step 4: Set the File Permission

The web server needs execute permission on FormMail.pl. Permission 755 gives the owner full access and gives the server read and execute access without opening the file to all users.

  1. In File Manager, right-click FormMail.pl and select Change Permissions.
  2. Set the permission value to 755.
  3. Click Change Permissions to save.

Warning: Do not set permissions to 777. That gives every user on the server write access to your script, which is a security risk.

Step 5: Add the Form Action to Your HTML Page

The action attribute in your HTML form tag tells the browser where to send the form data when a visitor clicks Submit. For a FormMail contact form, the action URL must point to the FormMail.pl script you uploaded to cgi-bin. If the path does not match where the script is installed, the form will return an error instead of sending an email.

Add or update the opening <form> tag in your HTML page to match this pattern, replacing <your-domain> with your actual domain name:

<form action="https://<your-domain>/cgi-bin/FormMail.pl" method="POST">

Replace <your-domain> with your actual domain name (for example, example.com). On InMotion shared hosting, Apache serves the cgi-bin folder at the /cgi-bin/ URL path, so the public URL always takes this form regardless of the folder’s location on disk.

Note: The method="POST" attribute sends form data in the request body rather than in the URL. FormMail requires POST. Using method="GET" will cause the submission to fail.

Step 6: Add the Required Hidden Fields

FormMail reads hidden input fields to determine where to send the email, what subject line to use, and where to redirect the visitor after submission. Place these fields inside the <form> tag you set up in Step 5.

Recipient (required). This tells FormMail which address to send the submission to. The address must appear in the @allow_mail_to list you configured in Step 3.

<input type="hidden" name="recipient" value="you@<your-domain>" />

Replace you@<your-domain> with the email address you added to @allow_mail_to.

Subject (optional). This sets the subject line of every email FormMail sends.

<input type="hidden" name="subject" value="Message from the contact form" />

Redirect (optional). This sends the visitor to a thank-you page after they submit. If you omit this field, FormMail displays its own generic confirmation page.

<input type="hidden" name="redirect" value="https://<your-domain>/thank-you.html" />

Replace <your-domain>/thank-you.html with the path to your actual thank-you page.

Configuring SMTP on Shared Hosting

On InMotion shared hosting, the server routes outbound mail through a restricted sendmail path to prevent spam. If your form submissions are not arriving, switching FormMail to use SMTP directly often resolves the issue. The NMS FormMail script supports an SMTP relay out of the box through the $mailprog variable.

  1. Open FormMail.pl in a text editor.
  2. Find this line:
$mailprog = '/usr/lib/sendmail -oi -t';
  1. Replace it with the line below, substituting <your-mail-server> with your actual outgoing mail server hostname:
$mailprog = 'SMTP:<your-mail-server>';

To find your outgoing mail server hostname, see Finding your Email Settings in cPanel or Webmail.

Troubleshooting

Symptom: 500 Internal Server Error when the form is submitted

This error almost always means one of two things: the file permissions on FormMail.pl are not set to 755, or the file was saved with Windows line endings (CRLF) instead of Unix line endings (LF). Open the file in a text editor that can convert line endings, save it again with Unix line endings, re-upload it, and confirm the permission is 755.

Symptom: Form submits without error but no email arrives

Check that the email address in the recipient hidden field exactly matches an address in the @allow_mail_to array inside FormMail.pl. A mismatch causes FormMail to silently drop the submission. Also check your spam or junk folder, because server-side mail can be filtered aggressively. If the address is correct and the email is not in the spam folder, try switching to SMTP as described in the Configuring SMTP section above.

Symptom: Form action returns a “not found” or 404 error

The URL in the action attribute does not match the location of FormMail.pl on your server. Confirm the script is in the cgi-bin folder (Step 2) and that the URL path in your form tag matches exactly, including capitalization. Filenames on InMotion’s Linux servers are case-sensitive, so FormMail.pl and formmail.pl are different files.

Conclusion

Your FormMail contact form is now installed, configured, and connected to your HTML page. Visitors can fill in the form, and their submissions will arrive in the inbox you specified. To test the form before launching, submit it yourself and confirm you receive the email. If you want to expand the form with additional fields, see How Contact Forms Work for a deeper look at how HTML form data is structured and processed.

Share this Article
Derrell Willis
Derrell Willis Manager, Developer Relations

More Articles by Derrell