Home > Uncategorized > Simple PHP form (POST) to send e-mail

Simple PHP form (POST) to send e-mail

I came across a very nice sample on how to write a PHP form to send e-mail (using POST form action [aka HTTP request method] and PHP’s mail function).

The sample was simple enough, but thought I should add some explanation since a user had been asking how it works:

  1. At start it checks if it’s rendering the page the 1st time (same goes if page was refreshed) or if it’s redrawing it as a result of a form POST action (a postback in Microsoft’ ASP.net terminology)
  2. a) if it’s not a postback, it lays out the HTML elements that make up the form (all input items or items the value of which we don’t want to recalculate every time [if they’re not security critical that is since readonly form field values can be edited by hackers] should be enclosed in FORM tag)
    b) if it’s a postback, it grabs the form fields (by name), constructs $emailsend variable (shown in multiple steps above for overview of the resulting e-mail), then calls mail function of PHP to send the e-mail (from value is hardcoded in that call, will usually need to be a value that your server allows, like “someone@yourserverdomain”)

Note: If form should send e-mail to you, don’t allow user to set the TO field (remove it, don’t just make it readonly else hackers can exploit to send spam to others via your server) and hardcode it in the mail function call.

Update: Since the above URL is now broken, I suggest you use the free FormToEmail PHP script (or if you don’t have a PHP host try their free FormToEmailRemote service) for e-mailing feedback forms. Both these offerings also have a "Pro" version you can register if you need more advanced features like CAPTCHA etc.

Advertisements
Categories: Uncategorized Tags:
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: