Results 1 to 4 of 4

Thread: PHP form and sending via email

  1. #1
    Join Date
    Aug 2008
    Posts
    30

    PHP form and sending via email

    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 .= ','; 
    )

  2. #2
    Join Date
    Aug 2008
    Posts
    30

    Re: PHP form and sending via email

    2-3 Sending email

    Finally, we will proceed with sending the email, so you receive the form completed by the user. Start by preparing the header of mail that will make it possible to specify the email address of the person who sends the form to be able to respond, the encoding and so on. Knowing all the headers for sending an email is purely technical and unnecessary in most cases because usually it's your mail client that handles all this when you send an e-mail.


    Code:
    / / Preparing the header of the mail: 
    $ mail_entete = "MIME-Version: 1.0 \ r \ n"; 
    $ mail_entete .= "From: ($ _POST [ 'name'])" 
    ."<{$_ POST [ 'email'])> \ r \ n "; 
    $ mail_entete .= 'Reply-To:'. $ _POST [ 'email']. "\ r \ n"; 
    $ mail_entete .= 'Content-Type: text / plain; charset = "iso-8859-1"; 
    $ mail_entete .= "\ r \ nContent-Transfer-Encoding: 8bit \ r \ n"; 
    $ mail_entete .= 'X-Mailer: PHP /'. phpversion (). "\ r \ n";
    Also preparing the body of the mail you will receive. We will just specify the message, the address of the person, their interests and the optional message. This way you get all the data entered on the form created.
    Code:
    / / Preparation of the body of the mail 
    $ mail_corps = "Message: Gender $ $ name \ n"; 
    $ mail_corps .= "Address: $ address, $ Postcode $ city, $ country \ n"; 
    $ mail_corps .= "His main interests: $ sqlinterets \ n \ n \ n"; 
    mail_corps .= $ $ comments;
    Finally we will have to send the mail with the PHP mail () which takes in setting a recipient, subject, body and the header. If the mail is sent, the function returns "TRUE" and we display the message.
    Otherwise, if the function returns FALSE, we display an error message.

    Code:
    / / Sending mail 
    if (mail (MAIL_DESTINATAIRE, MAIL_SUJET, $ mail_corps, $ mail_entete)) ( 
    
    / / The mail is sent 
    echo $ msg_ok; 
    Else () 
    
    / / The mail was not sent 
    echo "An error occurred while l \ 'sent the form by email'; 
    )
    
    )

  3. #3
    Join Date
    Aug 2008
    Posts
    30

    Re: PHP form and sending via email

    3. The final code of traitement.php

    Finally the entire code file traitement.php that we have just seen, to send the form data by email:
    Code:
    <? php 
    $ msg_erreur = "Error. The following fields must be filled: <br/>"; 
    $msg_ok = "Your application has been taken into account.."; 
    $ message = $ msg_error; 
    define ( 'MAIL_DESTINATAIRE', 'webmaster@techarena.in'); / / replace your email 
    define ( 'MAIL_SUJET', 'Message form example.com');
    / / Verification fields 
    if (empty ($ _POST [ 'Gender'])) 
    $ message .= "Your civility <br/>"; 
    if (empty ($ _POST [ 'name'])) 
    $ message .= "Your name <br/>"; 
    if (empty ($ _POST [ 'address'])) 
    $ message .= "Your address <br/>"; 
    if (empty ($ _POST [ 'Postcode'])) 
    $ message .= "Your zip code <br/>"; 
    if (empty ($ _POST [ 'city'])) 
    $ message .= "Your city <br/>"; 
    if (empty ($ _POST [ 'comments'])) 
    $ message .= "Your message <br/>";
    
    / / If a field is empty, we display the error message 
    if (strlen ($ message)> strlen ($ msg_error)) ( 
    
    echo $ message; 
    
    / / Otherwise it's ok 
    Else () 
    
    foreach ($ _POST as $ index => $ value) ( 
    $ index = stripslashes (trim ($ value)); 
    )
    
    Interest = $ _POST [ 'interest']; 
    $ sqlinterets =''; 
    for ($ i = 0; $ i <count ($ interest); $ i + +) 
    ( 
    $ sqlinterets interest .= $ [$ i]; 
    $ sqlinterets .= ','; 
    ) 
    
    
    
    / / Preparing the header of the mail: 
    $ mail_entete = "MIME-Version: 1.0 \ r \ n"; 
    $ mail_entete .= "From: ($ _POST [ 'name'])" 
    ."<{$_ POST [ 'email'])> \ r \ n "; 
    $ mail_entete .= 'Reply-To:'. $ _POST [ 'email']. "\ r \ n"; 
    $ mail_entete .= 'Content-Type: text / plain; charset = "iso-8859-1"; 
    $ mail_entete .= "\ r \ nContent-Transfer-Encoding: 8bit \ r \ n"; 
    $ mail_entete .= 'X-Mailer: PHP /'. phpversion (). "\ r \ n"; 
    
    
    
    / / Preparation of the body of the mail 
    $ mail_corps = "Message: Gender $ $ name \ n"; 
    $ mail_corps .= "Address: $ address, $ Postcode $ city, $ country \ n"; 
    $ mail_corps .= "His main interests: $ sqlinterets \ n \ n \ n"; 
    mail_corps .= $ $ comments; 
    
    
    
    / / Sending mail 
    if (mail (MAIL_DESTINATAIRE, MAIL_SUJET, $ mail_corps, $ mail_entete)) ( 
    
    / / The mail is sent 
    echo $ msg_ok; 
    Else () 
    
    / / The mail was not sent 
    echo "An error occurred while l \ 'sent the form by email'; 
    ) 
    
    
    
    ) 
    >

  4. #4
    Join Date
    Aug 2008
    Posts
    30

    Re: PHP form and sending via email

    4. Mistake

    Attention, sending data form is conducted by the PHP mail () as you see it. So PHP is properly configured to send mail with a mail server, otherwise it will not.
    For the mail function () is available, PHP must have access to the service sendmail on the server, at compile time. If you use another mail program, such as qmail and postfix, be sure to use good API. PHP will start looking sendmail in your path, then in the following folders: / usr / bin: / usr / sbin: / usr / etc: / etc: / usr / ucblib: / usr / lib. It is highly recommended to have sendmail available in your PATH. In addition, the user who compiles PHP should have the right to access the sendmail binary.

    Usually when you do the test on an Apache server with windows installed easyphp or WAMP, you can have the problem because there is no mail server running on Windows XP by default. You may therefore have an error like this:

    Warning: mail () [function.mail]: SMTP server response: 502 negative vibes in C: \ Program Files \ wamp \ www \ form \ email.php on line 60
    An error occurred in form by email

    But if you test the same script in most hosts websites, it will work because they are usually configured to function as mail () works with a mail server installed. So do not panic ...

Similar Threads

  1. JSP form - error in sending request
    By Vodka in forum Software Development
    Replies: 5
    Last Post: 18-01-2010, 02:04 PM
  2. Sending email periodically
    By Aaliya Seth in forum Windows Software
    Replies: 5
    Last Post: 31-12-2009, 10:35 AM
  3. email the web form detail
    By Ekpah in forum Windows Software
    Replies: 3
    Last Post: 24-11-2009, 11:01 PM
  4. sending a song through email
    By Justvicks in forum Networking & Security
    Replies: 3
    Last Post: 08-04-2009, 06:39 PM
  5. PHP Form: Email + MySQL
    By GaryK in forum Software Development
    Replies: 0
    Last Post: 04-12-2008, 02:18 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,130,077.51649 seconds with 17 queries