Validating email address in php

Any fields already completed will be left unchanged, allowing the user to simply adjust her input and re-submit the form without having to enter all of the data again. Let’s look at the PHP required for validating the form.

All of this code would be placed towards the top of the page before the HTML for the form: The exception is the brochure checkbox which does not need to be validated as it is an optional field.

Most of the time such a contact form would set SMTP headers such as to make it easy for the recipient to treat communication from the contact form just like they would any other email.

Unfortunately, unless the user’s input is validated before being inserted into SMTP headers, the contact form might be vulnerable to Email Header Injection (also referred to as SMTP header injection).

The script needs to be able to differentiate between the two stages so the form behaves properly.

To determine whether the form has been submitted by the user, either check the request method (an example follows in the next section) or check for the presence of a specific form control (usually the submit button).

The following code takes the name and email address provided by a website visitor and prepares a list of headers for the email.

The From header is used so that the email’s recipient (in this example it’s on Windows systems) and appending a bcc SMTP header containing additional email addresses to whom the SMTP server will deliver the email to in BCC.

Now that you’ve defined the form in HTML, let’s identify the two stages the form goes through – when the blank form is loaded, and when the user has completed the form and clicks the submit button.In this article you’ll construct and validate a simple form using HTML and PHP.The form is created using HTML and validation and processing of the form’s contents is done with PHP.He's also written a book about Dreamweaver CS3 for Hodder Education.It’s common practice for websites to implement contact forms which in-turn send emails to an intended recipient of the message by a legitimate user.