Web Form that e-mails an XML attachment

PHPWebFormXML

More often than not the feature I’m trying to implement doesn’t exist. Or it’s just not quite right and doesn’t get the job done.

  • At face value this seemed fairly straight forward – build a web form that
    • performs validation of required fields.
    • e-mails the response.
    • attaches a Maximizer compatible XML file to the e-mail for quick & easy import.
    • host the web form inside a Word Press page.

Of course there are plenty of examples online of how to implement each of these features but no example that ties them all together.

Step 1 – The Basic PHP Form

Let’s start with a basic PHP web form. W3 Schools has good examples to get you up to speed on the basics. The idea is that form submits itself to itself allowing the fields to be validated, the values entered to remain in the inputs, and the ability to change what is displayed when the form is submitted. W3 Schools Complete Example.

</pre>
<form action="#" method="post">;
 <input type="text" name="email" value="<?php echo $email;?>" />
 <input type="submit" name="submit" value="submit" /></form>
<pre>

Step 2 – PHP mail()

Now we can collect the required information it’s time to e-mail the results. Once again, fairly straight forward and there are plenty of examples online (W3 Schools) of how to use the PHP mail() function.

Step 3 – XML

My specific goal was to create an XML document that could be opened from an e-mail and imported directly into Maximizer. However, this code can be easily modified to plug the data into any XML schema. The full schema for Maximizer can be found in their Administrator Guide. The contents of a basic MXI file is shown below.

<!--?xml version="1.0"?-->

	
		Joe
		Blogs
</pre>
<address>joe@blogs.com,</address>
<pre>
		
		
			123.456.7890
</pre>
<address>1 Beach Road Unit #B Newport Beach
 CA
 USA
 92663</address>
<pre>
	

This XML document can be created in PHP using the DOMDocument and associated functions.

	/* Create XML Document */
	$xmlDoc = new DOMDocument('1.0');

	/* Build Maximizer XML file */
	$xmlRoot = $xmlDoc->createElement('AllData');
	$xmlDoc->appendChild($xmlRoot);

	$xmlIndividual = $xmlDoc->createElement('Individual');

	$xmlFirstName = $xmlDoc->createElement('FirstName', $input_firstname);
	$xmlIndividual->appendChild($xmlFirstName);

	$xmlLastName = $xmlDoc->createElement('LastName', $input_lastname);
	$xmlIndividual->appendChild($xmlLastName);

	/* Continue to add other collected data */

	/* No need to create an actual file, just use the content of the created XML document */
	$content = $xmlDoc->saveXML();

In this case there is no need create an actual XML file since we are going to be e-mailing it as an attachment. However, we must still encode the XML string before attaching it to the e-mail.

$content = chunk_split(base64_encode($xmlDoc->saveXML()));

Step 4 – Emailing the XML as an Attachment

In order to create an e-mail with an attachment using the PHP mail() function we have to build an e-mail header containing all the correct information. We want to send a multipart message – plain text & an attachment.

At this stage we have a fully functioning stand alone PHP Web Form that e-mails an XML Attachment.

Step 5 – WordPress

To make it work inside a WordPress page / post it should be as easy as pasting our code into the page / post / widget where we want the form. Unfortunately WordPress doesn’t let you have any PHP code included inside the contents of a page / post / widget. The solution? A plugin! There are a variety but this one worked for me – PHP Code for posts.

Since the plugin works with “snippets” I found it more manageable to keep the form in a separate file and just include it inside the php snippet as follows.

<!--?php include '/home/account/public_html/mywebform.php';?-->

Now all that is left is including the “PHP Code for posts” shortcode in your page / post / widget.



Step 6 – reCaptcha

In order to add a little more security to the form we can add a reCapthca input that prohibits automated entries being submitted via our form. This shows how to use reCaptcha with PHP.

Complete source code for this example can be found on GitHub