Results 1 to 5 of 5

Thread: How can i add HTML functionality to this PHP contact Script?

  1. #1
    Join Date
    Feb 2007
    Posts
    145
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default How can i add HTML functionality to this PHP contact Script?

    Hi there, I already have a PHP script that sends an email to me when someone used the contact form on my website, but the email I receive is just standard text, how can i add hyperlinks and images into this script? My script is below:

    PHP Code:
    <?php

    function domain_exists($email,$record 'MX')
    {
    list(
    $user,$domain) = split('@',$email);
    return 
    checkdnsrr($domain,$record);
    }

    function 
    isValidEmail($address) {
      return 
    preg_match("/^[a-z0-9._\-]+[@]([a-z0-9\-]+[.])+([a-z]{2,4})\$/i",
                        
    $address);
     }


    // set the default timezone to use. Available since PHP 5.1
    date_default_timezone_set('Europe/London');


    // get posted data into local variables
    $EmailFrom "ryan.fitton@googlemail.com";
    $EmailTo "ryan.fitton@googlemail.com";
    $Subject "Email From Personal Website";
    $YourName Trim(stripslashes($_POST['YourName'])); 
    $YourEmail Trim(stripslashes($_POST['YourEmail'])); 
    $YourMessage Trim(stripslashes($_POST['YourMessage']));
    $YourIP Trim(stripslashes($_POST['IP'])); 
    $Date date("d M Y");
    $Time date("h:i A");


    // validation
    $validationOK=true;
    if (!
    $validationOK) {
      print 
    "<meta http-equiv=\"refresh\" content=\"0;URL=/contact_error.php\">";
      exit;
    }


    // Check if required fields are filled in
    if(trim($YourName) == '' || trim($YourEmail) == '' || trim($YourMessage) == '') {
    print 
    "<meta http-equiv=\"refresh\" content=\"0;URL=/contact_error.php\">";
    exit;
    }


    // check for a valid email
    if(!isValidEmail($YourEmail) || !domain_exists($YourEmail))
       die(
    "<meta http-equiv=\"refresh\" content=\"0;URL=/contact_error.php\">");
    else {


    // prepare email body text
    $Body .= "Ryan Fitton, you have recieved an email from the contact form on your website.";
    $Body .= "\n\n";
    $Body .= "This message was sent on the ";
    $Body .= $Date;
    $Body .= " at ";
    $Body .= $Time;
    $Body .= "\n\n";
    $Body .= "--------------------------------------";
    $Body .= "\n";
    $Body .= "IP Adresses: ";
    $Body .= $IP=$_SERVER['REMOTE_ADDR'];
    $Body .= "\n\n";
    $Body .= "Name: ";
    $Body .= $YourName;
    $Body .= "\n\n";
    $Body .= "Email: ";
    $Body .= $YourEmail;
    $Body .= "\n\n";
    $Body .= "Message: ";
    $Body .= $YourMessage;
    $Body .= "\n";
    $Body .= "--------------------------------------";
    $Body .= "\n\n\n";
    $Body .= "This email has arrived from http://www.ryanfitton.co.uk and is private for Ryan Fitton (ryan.fitton@hotmail.co.uk). If you are not Ryan Fitton and you have recieved this email, please contact me using this email address: ryan.fitton@hotmail.co.uk";


    // send email 
    $success mail($EmailTo$Subject$Body"From: <$EmailFrom>");

    // redirect to success page 
    if ($success){
      print 
    "<meta http-equiv=\"refresh\" content=\"0;URL=/contact_sent.php\">";

    }
    else{
      print 
    "<meta http-equiv=\"refresh\" content=\"0;URL=/contact_error.php\">";
    }

    }

    ?>
    I want to add an hyperlink using html for example:
    HTML Code:
    <a href='http://www.google.co.uk'>Clike here to access Google</a>
    I want to have this somewhere in the body tags, such as:
    Code:
    $Body .= "Ryan Fitton, you have recieved an email from the contact form on your website here: <a href='http://www.google.co.uk'>Clike here to access Google</a>";
    Last edited by Ryan Fitton; 08-15-2010 at 03:05 PM.

  2. #2
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    You need to give it a header

    PHP Code:
    $headers  'MIME-Version: 1.0' "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' "\r\n";
    $headers .= "From: <$EmailFrom>\r\n";
    $success mail($EmailTo$Subject$Body$headers); 
    http://php.net/manual/en/function.mail.php
    Corrections to my coding/thoughts welcome.

  3. #3
    Join Date
    Feb 2007
    Posts
    145
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default

    Hi thanks for your gel but i've managed to get it working using this:
    PHP Code:
    <?php

    function domain_exists($email,$record 'MX')
    {
    list(
    $user,$domain) = split('@',$email);
    return 
    checkdnsrr($domain,$record);
    }

    function 
    isValidEmail($address) {
      return 
    preg_match("/^[a-z0-9._\-]+[@]([a-z0-9\-]+[.])+([a-z]{2,4})\$/i",
                        
    $address);
     }


    // set the default timezone to use. Available since PHP 5.1
    date_default_timezone_set('Europe/London');


    // get posted data into local variables
    $EmailFrom "ryan.fitton@googlemail.com";
    $EmailTo "ryan.fitton@googlemail.com";
    $Subject "Email From Personal Website";
    $YourName Trim(stripslashes($_POST['YourName'])); 
    $YourEmail Trim(stripslashes($_POST['YourEmail'])); 
    $YourMessage Trim(stripslashes($_POST['YourMessage']));
    $YourIP Trim(stripslashes($_POST['IP'])); 
    $Date date("d M Y");
    $Time date("h:i A");


    // validation
    $validationOK=true;
    if (!
    $validationOK) {
      print 
    "<meta http-equiv=\"refresh\" content=\"0;URL=/contact_error.php\">";
      exit;
    }


    // Check if required fields are filled in
    if(trim($YourName) == '' || trim($YourEmail) == '' || trim($YourMessage) == '') {
    print 
    "<meta http-equiv=\"refresh\" content=\"0;URL=/contact_error.php\">";
    exit;
    }


    // check for a valid email
    if(!isValidEmail($YourEmail) || !domain_exists($YourEmail))
       die(
    "<meta http-equiv=\"refresh\" content=\"0;URL=/contact_error.php\">");
    else {


    // prepare email body text
    $Body .= "Ryan Fitton, you have recieved an email from the contact form on your website <a href='http://www.ryanfitton.co.uk'>here</a>:";
    $Body .= "\n\n";
    $Body .= "This message was sent on the ";
    $Body .= $Date;
    $Body .= " at ";
    $Body .= $Time;
    $Body .= "\n\n";
    $Body .= "--------------------------------------";
    $Body .= "\n";
    $Body .= "IP Adresses: ";
    $Body .= $IP=$_SERVER['REMOTE_ADDR'];
    $Body .= "\n\n";
    $Body .= "Name: ";
    $Body .= $YourName;
    $Body .= "\n\n";
    $Body .= "Email: ";
    $Body .= $YourEmail;
    $Body .= "\n\n";
    $Body .= "Message: ";
    $Body .= $YourMessage;
    $Body .= "\n";
    $Body .= "--------------------------------------";
    $Body .= "\n\n\n";
    $Body .= "This email has arrived from http://www.ryanfitton.co.uk and is private for Ryan Fitton (ryan.fitton@hotmail.co.uk). If you are not Ryan Fitton and you have recieved this email, please contact me using this email address: ryan.fitton@hotmail.co.uk";

    $headers[]  = 'MIME-Version: 1.0';
    $headers[] = 'Content-type: text/html; charset=iso-8859-1' ;
    $headers[] = 'From: <'.$EmailFrom.'>';

    // the implode is because each line has to be separated by an return space (part of the RFC)
    $success mail($EmailTo$Subject$Bodyimplode("\r\n"$headers));

    // redirect to success page 
    if ($success){
      print 
    "<meta http-equiv=\"refresh\" content=\"0;URL=/contact_sent.php\">";

    }
    else{
      print 
    "<meta http-equiv=\"refresh\" content=\"0;URL=/contact_error.php\">";
    }

    }

    ?>

  4. #4
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    That's the same thing with an extra function, the implode.
    Corrections to my coding/thoughts welcome.

  5. #5
    Join Date
    Feb 2007
    Posts
    145
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default

    Yeahh i thought it was but I wasn't sure :P

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
  •