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.
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.
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).
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)
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';
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.