SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: php mailer code

  1. #1
    SitePoint Member
    Join Date
    Aug 2012
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    php mailer code

    Hi Friends,

    I am planning to go for freelancing and I am learning to design websites by using free templates available online. I am almost done with the website and I am about to host it. The last page left is the contact page and I have no clue how to make the contact form working. With the information online, I came to know that php mailer form does resolve this issue. Following is my html code.. I request you to explain the step by step procedure to make this working, as I am a completely new beginner.

    Form Code:


    HTML Code:
    <h4>Send us a message!</h4>
    	<div id="contact_form"  class="col_w280 float_l">
    		<form method="post" name="contact" action="#">
    		
    			<label for="author">Name:</label> <input type="text" id="author" name="author" class="required input_field" />
    			<div class="cleaner h10"></div>
    			<label for="email">Email:</label> <input type="text" id="email" name="email" class="validate-email required input_field" />
    			<div class="cleaner h10"></div>
    								<label for="text">Message:</label> <textarea id="text" name="text" rows="0" cols="0" class="required"></textarea>
    			<div class="cleaner h10"></div>
    			
    			<input type="submit" value="Send" id="submit" name="submit" class="submit_btn float_l" />
    			<input type="reset" value="Reset" id="reset" name="reset" class="submit_btn float_r" />
    			
    		</form>
    	</div>
    Please do the needful. Thanks in advance.
    Last edited by Mittineague; Aug 3, 2012 at 21:42. Reason: reformatting bbcode tags

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,940
    Mentioned
    215 Post(s)
    Tagged
    12 Thread(s)
    Hi,
    You want to make a contact form using PHP and have it send (you) a mail when a visitor fills it out.
    Have I understood you correctly?

  3. #3
    SitePoint Member
    Join Date
    Aug 2012
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Pullo,

    You got the point.

  4. #4
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,940
    Mentioned
    215 Post(s)
    Tagged
    12 Thread(s)
    Okay then. I'll show you how to make a very simple contact form and have it send you a mail.

    However, before you do this, take a minute to consider some of the alternatives.
    For example a quick Google search for "contact form generator" points you to a whole host of services (some free, some not), offering to do what you want.

    Anyway, presuming you want to code one by hand, you'll need a form.
    Here's one I made earlier. Name it contact.php and upload it to your server.

    HTML Code:
    <!DOCTYPE HTML>
    <html lang="en">
      <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8">
        <title>Contact form example</title>
      </head>
      
      <body>
        <h1>Send us a message</h1>
        
        <form action="contact.php" method="post">
          <label for="message">Your message:</label><br />
          <textarea name="message" id="message" rows="10" cols="50"></textarea><br />
          <input type="submit" value="Send" />
        </form>
      </body>
    </html>
    As it stands, this doesn't really do much.
    When a visitor enters a message, the form just posts this message back to itself and that's it.
    What we would like to do now, is add some PHP code, which can extract the message into an email and send it.

    We can do this by adding the following PHP code to the top of our file. Note the use of the strip_tags() function to protect against xxs.

    Code PHP:
    <?php 
      $message= strip_tags($_POST['message']);   
      $address = "youremail@yourdomain.com";
      $subject = "Message from contact form";
      $from = "From: Contact Form <noreply@yourdomain.com>";
      mail($address, $subject, $message, $from);
    ?>

    This now poses us with an additional problem, however. When the page loads, the PHP kicks in and sends an empty email to whichever address you specified.
    We can get around this by adding the following HTML to the bottom of the form:
    HTML Code:
    <input type="hidden" name="process" value="1" />
    and then checking the value of "process", before executing any PHP.

    Code PHP:
    if ($_POST['process'] == 1) {	
    ...
    }

    At this point we have a working form, but there are still a couple of issues that need ironing out.

    Firstly, it would be sensible to check that the user has entered a message and not just submitted a blank form.
    To do this, let's add an $error variable to the top of our script. Then, we can check if the $message variable is empty. If it is, we can then add our error message to our $error variable.

    Code PHP:
    $error = "";
    ...
    if (empty($message)){
      $error.= "You didn't enter a message.";
    }

    Then, before sending the mail, we can check if the $error variable contains anything. If it doesn't, then we can proceed as normal, if it does, then we don't send the mail, rather re-render the form, plus error message.

    Code PHP:
    if (empty($error )) {
      send the mail as above
    }
    ...
    if (!empty($error )){
        echo "<p>Your message could not be sent:</p>";  
        echo "<p>"."$error"."</p>";
    }

    Now, this stops a user submitting a blank message, but conversely, it would probably be nice to give the user some feedback if their message is sent correctly.
    I normally do this by creating a "success" page, and on a successful form submission, redirecting the user to that page.
    You can do this using the header() function in PHP, thus:

    Code PHP:
    header("location: http://www.yoursite.com/contact/success.html");

    Finally, although it's not applicable in our case, as we only have one field, it is nicer for the user if when the form is re-rendered in the case of an error, not all of the fields which they filled out correctly are then wiped.
    You can make sure that text fields retain their original values, by using the value attribute thus:
    HTML Code:
    value="<?php echo($message); ?>"
    In the case of a text area however, we don't even need this and can just use
    Code PHP:
    <?php echo($message); ?>
    inbetween the <textarea> tags.

    Here is the complete script. I hope you find this useful.

    Code PHP:
    <?php 
      if ($_POST['process'] == 1) {	
        $message = strip_tags($_POST['message']);   
        $error = "";
     
        if (empty($message)){
          $error.= "You didn't enter a message.";
        } 
     
        if (empty($error )) {   
          $address = "youremail@yourdomain.com";
          $subject = "Message from contact form";
          $from = "From: Contact Form <noreply@yourdomain.com>";
          mail($address, $subject, $message, $from);
          header("location: http://www.yourdomain.com/contact/success.html");
        }
      }
    ?>
     
    <!DOCTYPE HTML>
    <html lang="en">
      <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8">
        <title>Contact form example</title>
      </head>
     
      <body>
        <?php   
          if (!empty($error )){
            echo "<p>Your message could not be sent:</p>";  
            echo "<p>"."$error"."</p>";
          }  
        ?>
     
        <h1>Send us a message</h1>
     
        <form action="contact.php" method="post">
          <label for="message">Your message:</label><br />
          <textarea name="message" id="message" rows="10" cols="50"><?php echo($message); ?></textarea><br />
          <input type="hidden" name="process" value="1" />
          <input type="submit" value="Send" />
        </form>
      </body>
    </html>

  5. #5
    SitePoint Member
    Join Date
    Aug 2012
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you So much... I will follow the guidelines and post the result back... Thanks again


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
  •