I. Introduction to sending email form PHP
Many of you respond and ask for extensions of previous tutorials on the forms PHP, and it's always nice that you wear a great interest in our tutorials. In the previous tutorial, I explained how to make a form PHP, HTML, CSS and send the form data in a MySQL database.
We will now see how to send the data via email. For this, we use the same form as before, but the difference is that you will receive the data entered by a user on your email address and not in a database.
II. Explanations
1. Add a field on the form email
We will assume that if you send by e-mail the form to your site, you need the email address of the user eventually respond. I did not include this field in the HTML form of the previous tutorial, this will make us the opportunity to see how to add a text field by HTML.
We will add the email address field for example between the City field and scope country. See the code to be added in bold below:
Code:
<label> City: </ label>
<input type="text" name="ville" size="30" /> <br />
<label> Email Address: </ label>
<input type="text" name="email" size="30" /> <br />
<label> Country: </ label>
<select name="pays">
<option value="Mumbai">Mumbai </ option>
<option value="Pune"> Pune </ option>
<option value="Delhi"> Delhi </ option>
</ select>
2. The processing file PHP form
2-1. Definitions and verification fields
We will now create a new file traitement.php which will send the form as in the previous tutorial, except that you will change accordingly to send the data via email.
As with the previous tutorial, starting with some error messages or confirmation that appears on the Internet:
Code:
$ msg_error = "Error. The following fields must be filled: <br/>";
$ msg_ok = "Your application has been taken into account."
$ message = $ msg_error;
Then we will define the recipient of the email that you will be sent containing the information form. Therefore replace email address with your own.
Enter a subject for the email that you receive the form. On this example I put the subject "Message form example.com".
Code:
define ( 'MAIL_DESTINATAIRE', 'webmaster@techarena.in'); / / replace your email
define ( 'MAIL_SUJET', 'Message form example.com');
2-2.Preparation of
If the user does not fulfills one of the required fields, you must begin by him display the error message. But how will we know if this is the case? Well this is where you will see the value of the variable $ message, it serves to keep the $ msg_error intact to make a comparison. The idea is: "if the contents of $ message and larger than the $ msg_error is that we just add fields not met, and this means to display the mistake.
NB: we could also check that all mandatory fields are not empty to do the same thing, but it will make many calls to the function empty () that can be avoided. That is what to do
Code:
if (strlen ($ message)> strlen ($ msg_error)) (
echo $ message;
)
As against if the fields are filled, we will start with each key recover the $ _POST into variables with $ $ while cleaning the spaces start and end with unnecessary trim () and any slashes added to the variables $ _POST for security with stripslashes.
Code:
else (
foreach ($ _POST as $ index => $ value) (
$ index = stripslashes (trim ($ value));
)
Then for no change, we will still concentrate centers of interest marked list separated by commas:
Code:
Interest = $ _POST [ 'interest'];
$ sqlinterets ='';
for ($ i = 0; $ i <count ($ interest); $ i + +)
(
$ sqlinterets interest .= $ [$ i];
$ sqlinterets .= ',';
)
Bookmarks