I’ve spent a little more time on prettying up a recent fommail.php installation, and have come to grips with some quirks involved in getting all the juicy info possible into the email I recieve.
If you are setting up formmail.php for the first time, it might be worth reading my previous post Configure formmail.php more easily.
My priorities are:
- using formmail.php to conceal my email address from spammers
- setting the subject to include derived fields
- tracking the IP address and User Agent of the user
- compile all the data into a presentable email
Using formmail.ini
My .ini file is really simple, and looks like this:
[special_fields] recipients = "tim@mysite.com" ;this is a comment
Now I no longer need to specify recipients in my form, so the address is not harvestable, and there’s no need to use an AT_MANGLE to obsfucate the address.
Note that a semi-colon functions comments out a line.
I’ve tried including other options in formmail.ini, but few function, so I prefer to keep all the values bar recipients together in the form.
Setting a useful subject line with ‘Derived Fields’
Derived Fields allow the form to combine values that the user enters into new variables which can be used in the email or to name files. I wanted to be able to see the User’s name in the subject line of the email (and other details.. I’ve dumbed this down for clarity), which requires defining ‘subject’ as a derived field.
The documentation is pretty clear about the nitty gritty of configuring derived fields, just not on including them in the html template.
Check the following example from my form:
<input type="hidden" name="derive_fields" value="subject=%'[Contact Form] -'%+email" />
- ‘subject=’ is pretty straight-forward
- %’this is how to include a literal string using formmail‘%
- + concatenates (joins) two values with a space between. A period joins with no space.
- email calls a field that you’ve defined somewhere else, either as a user-entered field, or defined later in the derived_fields.. stay tuned.
You can define a whole lot of these values within the one hidden field. They can get mighty confusing, so it’s best to span them over many lines, as I’ve done below:
<input type="hidden" name="derive_fields" value="
subject=%'[Contact Form] -'%+email,
DateTime = %'Form was submitted: '% . %date% + %time%,
remote_addr = REMOTE_ADDR,
http_user_agent = HTTP_USER_AGENT" />
Note the commas between each line. You can span required fields over multiple lines in the same way.
Using Derived Fields in the email template
Now we have access to all those handy values we’ve defined. Printing them in the email template is as simple as prefixing each value with a $. Here’s a taste of my email template:
<html> <body> <h1>$title</h1> <p>$DateTime</p> <pre> You can put preformat text in here, ready to be pasted into a website, as long as you convert all your opening angle brackets to html entities. Substitute each < with <. </pre> <ul> <li>IP Address: $remote_addr</li> <li>User Agent: $http_user_agent</li> </ul> </body> </html>
Simple, in retrospect