Build Email Message Dynamically From Submitted Form

I wrote some code to dynamically generate an email message based on the names of form elements being sent through post.
In this post I’ll describe how it’s done.

The foreach loop grabs the keys from the post array. An example key for this ¬†$_POST[‘Name’]¬†would be “Name”. It’s important to put in the name of the field how you want it to show in the email. So if you want uppercase letters or spaces put them in.

The $keywos (key without spaces) gets the current key in the foreach loop and replaces the underscores that are automatically put in if the name from the HTML form elements had spaces when posting and replaces them with spaces.

Copy the post array to values array

If the current value for a particular key doesn’t have information in it don’t add it to the email message. This means the user hasn’t typed in any information in the input or selected an option.

If the key isn’t equal to submit then add it to the email messages. The submit button doesn’t need to be added to the email message.


Have questions? Email me at [email protected].