This tutorial describes how to use Form2Mail. At its core is one single file to process your form. The only requirement is that you add a recipient email to get the form data.

Contents

  1. Form2Mail
    What is it? What does it do? Who needs it?
    Brief examples of Form2Mail features.
  2. Single File Use
    How to use Form2Mail (by itself) to process your forms.
  3. Using Form2Mail Extras
    Sending mails with file attachments: filesystem, database or inline.
  4. What are Plugins?
    How to use the Plugins architecture for post-processing your forms data.
  5. Support: Where do I get help?
    How to find support resources: Form2Mail website, etc.

Form2Mail

This project was previous called PHPMailer-FE. (FE = FormMail Edition)
It is now called PHPMailer-F2m (Form 2 Mail)

Form2Mail is the ultimate secure way of processing forms data and send it to your inbox. All user submitted data is sanitized with any hack attempts blocked. You can use Form2Mail as a single script solution or use the entire suite that includes a robust Plugin architecture for post-processing the user submitted data. As a single script solution, Form2Mail will send using PHP mail(). Form2Mail can also use PHPMailer or PHPMailer Pro as an email transport - that provides the option of using Qmail, Sendmail, PHP mail() or SMTP as the transport agent. In short: Form2Mail is the safest and most efficient way of processing user submitted data in your forms.

Without any other variables or settings, Form2Mail can handle one single attachment or multiple attachments. The data has to be your form.

Anyone can use PHP code to get the form input data. But you also have to validate that data, and format it in a sensible way to email or store in a database. Then you have to use PHP code to send the email either as a text message or as an attachment. The coding and testing process can be a nightmare. With Form2Mail it is as simple as detecting the form submission, setting one variable (the email address where the email should be sent) and call Form2Mail.php. That’s it.

Form2Mail is not a class. It is procedural PHP with a few functions. This tutorial explains how to implement the script into your form flow.

Single File Use

Form2Mail is really easy to use. All it takes is one single variable: the recipient‘s email address. Here is an example of the entire section to process your form. Throughout this tutorial, we will be referring to a form. For the purpose of an example, we are assuming the form to be located and named:

Here's what a directory structure should look like for single-script use:
form/samplecontactus.php
form/mailer/Form2Mail.php

The top of the form would contain:

<?php
if (isset($_POST['Submit']) && $_POST['Submit'] == 'Send Message') {
  $wfm_recipient = 'yourname@yourdomain.com'; // who to send the form results to
  if (file_exists('mailer/Form2Mail.php')) {
    include('mailer/Form2Mail.php');
  }
  exit();
}
?>
... and your HTML form goes below this, note this code means that you have
no action set in the <form tag (meaning the form will process in the same page).

this example assumes your submit button is named “Submit” and that it’s value is set to “Send Message” and that you have Form2Mail at that location.
Note, previous versions used $recipient ... this is a change in version 7.0 to avoid conflict with other scripts using the same variable name and make it unique to Form2Mail.

Form2Mail is procedural, meaning that it processes the code in a sequence. Once that sequence completes, it will display either a failed message or a success message (thank you). It then quits and returns back to your page for any further processing you want to do ... that might include a timer and a link back to your home page (to prevent re-submitting).

Using Form2Mail Extras

Maybe the title should be “Using Form2Mail WITH Extras” ...
Previous versions of Form2Mail had a lot of extras. Many of these (like the _sanitize function) are now embedded in Form2Mail. There are still many other Extras that can be used.

Form2Mail introduces a new directory for the Extras. If you want to use Form2Mail with Extras, here is the directory structure we recommend:

form/samplecontactus.php
form/mailer/Form2Mail.php
form/mailer/_logs
form/mailer/_plugins
form/mailer/_tpl
form/mailer/samplecontactus (OPTIONAL ... read below)
  • form/samplecontactus.php would be your form
    Your form should be able to execute PHP code. If it does not, you will have to use the action directive in your <form tag. An example would be:
    <form method="post" action="mailer/Form2Mail.php">
    and, if you have an input for file uploads, that would be:
    <form method="post" action="mailer/Form2Mail.php" enctype="multipart/form-data">
  • form/mailer/Form2Mail.php would be the Form2Mail script
  • form/mailer/_logs is to store logs (ban logs, etc.)
    Previous versions of Form2Mail used to include a “PRO” version that could create a CSV version of the form data. When using this feature, a user could then run a monthly CRON and email the CSV file to a support department for further evaluation. Capturing the form data as a CSV file is now supported in all versions and would be stored in the _logs directory.
  • form/mailer/_plugins is the location all plugins and other scripts should be located
    The architecture for plugins has been in previous versions of Form2Mail, but not quite defined as such.
    Pre-processing is reserved strictly for Form2Mail and named scripts. For pre-processing, you can include these scripts:
    – class.worx-html2text.php - this is a script originally authored by Jon Abernathy. The last version available from his account(s) will not work with PHP version 7. I have updated it and made a few modifications and will include it for downloads on sourceforge with Form2Mail.
    – PHPMailer Pro.php - this is an email transfer class that is secure, and fast. It supports Sendmail, Postfix, Qmail, EXIM and SMTP. PHPMailer Pro is a stand-alone script with no dependencies, no language files. At less than 70Kb, it is designed for smaller projects and forms processing.
    – SMTP2.php - this is an implementation of Chris Ryan's SMTP class (no version, no license, published in 2001). I am attributing the original to Chris Ryan, but this version is significantly enhanced. It is brought up to PHP version 7 standards. I have purposely maintained compatibility with PHPMailer’s SMTP version -- that, plus PHPMailer Pro compatibility means the combination is a reasonable drop-in strategy (with some minor changes possibly required).
    – PHPMailer Pro.php - this is an email transfer class available on Github that has dependencies (minimum, Exception.php, more depending on how you want to connect and authenticate). PHPMailer Pro.php by itself is 167Kb ... but is relatively easy to use. You can also store PHPMailer Pro.php and all of its dependencies in another location and pass that location by a Form2Mail property.
    For post-processing, there are no limitations. The way I use this is to occasionally post the already-sanitized user submitted data to a database table, or merge with other data (including hidden fields) to forward to an IT support department.
  • form/mailer/_tpl is where form templates are located
    This is where I store the default error.php and default thankyou.php messages. Those as the last step in processing the form data and indicate the result of the processing.br>
  • form/mailer/samplecontactus Note, this is optional. If you download the entire suite, we have a (sample) samplecontactus.php form included and a sample form/mailer/samplecontactus/samplecontactus.htm HTML file included. The HTML file is a template and the way that we want the visual representation of the form in the body of the email. The form has titles that are hard coded. The variable information is within braces. An example is the "Name" field from the form is in the template as {Name}. It is a “token substitution” type of system, where PHPMailer Pro will check to see if that folder exists, that form template exists, and call it if true to both checks. It will then perform a token substitution so that everything within braces is replaces with live user-submitted (and sanitized) data. Form2Mail will then use the template with filled in data as the email body.
    There is also another file in the same directory, that is samplecontactus.config.php. This configuration file exists so that you can have unique Form2Mail properties based on each individual form. Example: different $wfm_recipients, different CC or BCC, etc. This file is optional, if not found, Form2Mail will use the properties passed through the form, or through the default.config.php script.

What are Plugins?

A Plugin is a script that takes the sanitized $_POST data (user input) and processes it in some way. That can be to post to a database, append a CSV, used for calculations ... well, the list really is endless.

Why use Plugins? Can’t we just have a script run after Form2Mail? The short answer is sort of. Once Form2Mail finishes processing, it will redirect to either a failure page (error) or a success page (thank you). At that point, the $_POST data is lost to further processing. Form2Mail’s Plugin architecture provides a hook after the email is set and before the redirect action takes place. $_POST data is still available, actually more importantly – it is sanitized data.

A Plugin is a PHP script. It can be any name. It would be typically stored in mailer/_plugins ... and if it is, all you need to pass in the location. Here is an example:

  $ext_plugin = 'myscript.php';
  if (file_exists('mailer/Form2Mail.php')) {
    include('mailer/Form2Mail.php');
  }

Once Form2Mail has sent the email with the data, it will look for the script mailer/_plugins/myscript.php and process it if found.
Some advice: if you write a plugin for your organization, copy the $_POST data and work with the copy. Leave the $_POST data as is for possible further processing. Here is a cope snippet to handle the copy ... insert this in myscript.php near the top:
$myscript_post = $_POST;
... after this, treat $myscript_post just like you would the $_POST variable. The only difference is that $_POST is a global, $myscript_post is a local variable. The data is identical in both.

One note: if you choose to have your Plugin in a different location, you must enter the full path:
$ext_plugin = '/full/path/to/myscript.php';
if the path is relative to the FORM, you can enter it that way too:
$ext_plugin = 'relative/path/to/myscript.php';

Support - Where do I get help?

Form2Mail is open source and published under GPL3. Most of the questions are answered within this tutorial, but some questions may remain. Form2Mail has its own homepage on SourceForge and you can request support there.