Monday, 23 February 2015

Mail Scripting in PHP



Mail Scripting :

It is  common requirement to have a submit form on almost website.

In this we will create a PHP script that will send an email when a web form is submitted.
There are two parts for the web form:
 

  1. The HTML code for the form. The html standard form is as bellow.
  2. The PHP script for handling the form submission. The script receives the form submission     and sends an email.   

     
    HTML code for the email form:

    <form method="post" name="myemailfrm" action="form_to_email.php">

    Enter Name: <input type="text" name="name">

    Enter Email Address:<input type="text" name="email">

    Enter Message: <textarea name="message"></textarea>

    <input type="submit" value="Send Form">
    </form>



    • The form contains the following fields:
               name, email and message.
    • name and email are single  line text input fields where as message is a text area field.
    • On clicking the submit , the form will be submitted to “form_to_email.php”. 
              This form is submitted through the POST Method.
     


    Accessing the form submission data in the PHP script:

    Once your website visitor has submitted the form, the browser sends the form submission data to the script mentioned in the ‘action’ attribute of the form.

    Since we have the form submission method mentioned as POST in the form (method=’post’) we can access the form submission data through the $_POST[] in the PHP script.

    • The following code gets the values submitted for the fields: name, email and message.

    <?php
      $name = $_POST['name'];
      $visitor_email = $_POST['email'];
      $message = $_POST['message'];
    ?>


    Composing the Email:

    Now, we can use the above PHP variables to compose an email Here is the code:

    <?php
        $email_from = 'yourname@yourwebsite.com';

        $email_subject = "Form_Submit";

        $email_body = "You have received a new message from the user $name.\n".
                                "Here is the message:\n $message".
    ?>
     

    • The ‘From’ address, the subject and the body of the email is composed in the code above. Note the way the body of the message is composed using the variables.


    Sending the email:



    • The PHP function to send email is mail().
    • mail(to,subject,message,headers)


    • The headers parameter is to provide additional mail parameters ( like the from address, CC, BCC etc)
     

    Here is the code to send the email:

    <?php

      $to = "yourname@yourwebsite.com";

      $headers = "From: $email_from \r\n";

      $headers .= "Reply-To: $visitor_email \r\n";

      mail($to,$email_subject,$email_body,$headers);

     ?>
     

     
    Sending the email to more than one recipients:


    If you want to send the email to more than one recipients, then you just need to add these in the “$to” variable.

    <?php
      $to = "name1@website-name.com, name2@website-name.com,name3@website-name.com";

      mail($to,$email_subject,$email_body,$headers);
    ?>


    Example:

    <?php
    $to = "name1@website-name.com, name2@website-name.com,name3@website-name.com";

    $headers = "From: $email_from \r\n";

    $headers .= "Reply-To: $visitor_email \r\n";

    $headers .= "Cc: someone@domain.com \r\n";

    $headers .= "Bcc: someoneelse@domain.com \r\n";

    mail($to,$email_subject,$email_body,$headers);
    ?>














    Reference: Html-form-guid.com
         

    No comments:

    Post a Comment