PHP Mail form to HTML

Discussion in 'PHP' started by cocoonfxmedia, Jun 29, 2011.

  1. #1
    Hi Guys

    I have the following PHP script which works like a dream, but I can't figure how to convert the emails to HTML so that I can style them so they look better. Any ideas I don't want to completely rewrite the script...?


    
    
     if ($_SERVER["REQUEST_METHOD"] <> "POST")
    	die("You can only reach this page by posting from the html form"); 
    session_start();
    
    $capt = $_POST["captcha_input"];
    
    $valid_ref1="http://www.website/test.html";
    $valid_ref2="http://www.website/test.html";
    $replyemail="info@website.co.uk";
    function clean_input_4email($value, $check_all_patterns = true)
    {
     $patterns[0] = '/content-type:/';
     $patterns[1] = '/to:/';
     $patterns[2] = '/cc:/';
     $patterns[3] = '/bcc:/';
     if ($check_all_patterns)
     {
      $patterns[4] = '/\r/';
      $patterns[5] = '/\n/';
      $patterns[6] = '/%0a/';
      $patterns[7] = '/%0d/';
     }
    
     //NOTE: can use str_ireplace as this is case insensitive but only available on PHP version 5.0.
     return preg_replace($patterns, "", strtolower($value));
     
    }
    
    $name = clean_input_4email($_POST["name"]);
    $email = clean_input_4email($_POST["email"]);
    $tele = clean_input_4email($_POST["tele"]);
    $mobile = clean_input_4email($_POST["mobile"]);
    $date = clean_input_4email($_POST["date"]);
    $dpick = clean_input_4email($_POST["dpick"]);
    $horse = clean_input_4email($_POST["horse"]);
    $colour = clean_input_4email($_POST["colour"]);
    $event = clean_input_4email($_POST["event"]);
    $carriage = clean_input_4email($_POST["carriage"]);
    $thepickup = clean_input_4email($_POST["thepickup"], false);
    $thedest1 = clean_input_4email($_POST["thedest1"], false);
    $thedest2 = clean_input_4email($_POST["thedest2"], false);
    $postcode = clean_input_4email($_POST["postcode"]);
    $postcode2 = clean_input_4email($_POST["postcode2"]);
    $postcode3 = clean_input_4email($_POST["postcode3"]);
    $themessage = clean_input_4email($_POST["themessage"], false);
    
    
    
    $booking = preg_replace('/\//','',$_POST['date']);
    
    
    if ($_POST["captcha_input"] != $_SESSION["pass"]) {
    
    $error_msg='<p align="center"><strong>&nbsp;</strong></p>
    			<p align="center"><strong>SORRY ERROR - NOT SENT</strong>';
    		  echo "<a href='javascript:history.back(1);'>Back to e mail form</a>";
    
    
    }
    
    elseif($name && $email && $capt == $_SESSION["pass"]){
    
    
    $success_sent_msg='<p align="center"><strong>&nbsp;</strong></p>
                       <p align="center"><strong>Your message has been successfully sent to us<br>
                       </strong> and we will reply as soon as possible.</p>
                       <p align="center">A copy of your query has been sent to you.</p>
                       <p align="center">Thank you for contacting us.</p>
    				   <p align="center">Please <a href="http://www.website.co.uk/contact.html">click here</a> to return</p>';
    
    $replymessage = "
    Hello $name
    
    Thank you for your email booking request.
    
    I will endeavour to reply to you shortly.
    
    
    Please DO NOT reply to this email.
    
    Below is a copy of the contact details you submitted:
    Contact Details: 
    
    Name:- $name
    Email: $email
    Telephone: $tele
    Mobile: $mobile
    Pickup Address:  
    $thepickup
    Postcode: 
    $postcode
    
    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    
    Booking Details
    
    Date:  $date - $dpick
    Event: $event
    Number of Horse:- $horse 
    Colour choosen :-  $colour
    Carriage Type:- $carriage
    Special Requirements:- $themessage
    
    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    I accept and understand the terms of booking; that deposits are non refundable and that management reserve the right as a result of unforeseen circumstances to supply alternative transport.
    
    
    All bookings require a £100 non refundable deposit which must be returned with the completed booking form to the above address. Cheques should be made payable to TTTT LTD. Balance due 4 weeks prior to hire.
    
    NB Strictly no fireworks in close proximity of the horses. Maximum 6 mile round trip for horses’ wellbeing.
    Whilst every effort is made to ensure we arrive on time, we cannot be held responsible for unforeseen circumstances beyond our control (i.e. breakdowns, road works, accidents etc.
    
    --------------------------------------------------
    Please visit again http://www.web.co.uk/contact.html
    Kind Regards 
     Ltd
    
    ";
    
    // email variable not set - load $valid_ref1 page
    if (!isset($_POST['email']))
    {
     echo "<script language=\"JavaScript\"><!--\n ";
     echo "top.location.href = \"$valid_ref1\"; \n// --></script>";
     exit;
    }
    
    $ref_page=$_SERVER["HTTP_REFERER"];
    $valid_referrer=0;
    if($ref_page==$valid_ref1) $valid_referrer=1;
    elseif($ref_page==$valid_ref2) $valid_referrer=1;
    if(!$valid_referrer)
    {
     echo "<script language=\"JavaScript\"><!--\n alert(\"$error_msg\");\n";
     echo "top.location.href = \"$valid_ref1\"; \n// --></script>";
     exit;
    }
    $themessage =
    "
    
    BOOKING FORM
    Booking = S-$booking$dpick
    
    CONTACT DETAILS: 
    
    Name:- $name				
    Email: $email
    Telephone: $tele			
    Mobile: $mobile
    Address:  
    $thepickup
    Postcode: 
    $postcode
    
    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    
    Booking Details
    
    Event: $event
    Date:  $date 				Time: $dpick
    Number of Horse:- $horse 
    Colour choosen :-  $colour
    Carriage Type:- $carriage
    Special Requirements:- $themessage
    
    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    
    Destination 1 Detials
    
    Address: 
    $thedest1
    Postcode: 
    $postcode2
    
    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    
    Destination 2 Detials
    
    Address: 
    $thedest2
    Postcode: 
    $postcode3
    
    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    
    ";
    
    
    mail("$replyemail",
         "Booking Ref= S-$booking$dpick",
         "$themessage",
         "From: $email\nReply-To: $email");
    mail("$email",
         "Booking Ref= S-$booking$dpick",
         "$replymessage",
         "From: $replyemail\nReply-To: $replyemail");
    echo $success_sent_msg;
    
    }else{
    
    echo $error_msg;
    
    }
    
    PHP:
     
    cocoonfxmedia, Jun 29, 2011 IP
  2. prasanthmj

    prasanthmj Member

    Messages:
    62
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    45
    #2
    1.format the message in HTML

    
    //Make the message in HTML
    $replymessage = "
    <html>
    <head>
      <title></title>
    </head>
    <body>
    Hello $name
    ...
    ";
    
    PHP:
    2. Add Mime settings in the 'header' parameter

    
    // To send HTML mail, the Content-type header must be set
    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
    
    
    // Additional headers
    $headers .= "To: $replyemail" . "\r\n";
    $headers .= "From: $email" . "\r\n";
    $headers .= "Reply-To: $email" . "\r\n";
    
    
    
    mail("$replyemail",
         "Booking Ref= S-$booking$dpick",
         "$themessage",
         $headers);
    
    PHP:
    For an example, see php mail under "Example #4 Sending HTML email"
     
    prasanthmj, Jun 29, 2011 IP