Email Form Issues

Discussion in 'PHP' started by nickharper, Oct 8, 2007.

  1. #1
    Hi,

    I have an email script which at the minute sends the email with no formatting.

    It gets every variable from the form and sends it through.

    Here is the code:

    <?php
    $my_email = "email";
    
    //  Initialise variables
    
    $errors = array();
    
    if($_SERVER['REQUEST_METHOD'] == "POST"){$form_input = $_POST;}elseif($_SERVER['REQUEST_METHOD'] == "GET"){$form_input = $_GET;}else{exit;}
    
    // Remove leading whitespace from all values.
    
    function recursive_array_check(&$element_value)
    {
    
    if(!is_array($element_value)){$element_value = ltrim($element_value);}
    else
    {
    
    foreach($element_value as $key => $value){$element_value[$key] = recursive_array_check($value);}
    
    }
    
    return $element_value;
    
    }
    
    recursive_array_check($form_input);
    
    // Check referrer is from same site.
    
    if(!(isset($_SERVER['HTTP_REFERER']) && !empty($_SERVER['HTTP_REFERER']) && stristr($_SERVER['HTTP_REFERER'],$_SERVER['HTTP_HOST']))){$errors[] = "You must enable referrer logging to use the form";}
    
    // Check for a blank form.
    
    function recursive_array_check_blank($element_value)
    {
    
    global $set;
    
    if(!is_array($element_value)){if(!empty($element_value)){$set = 1;}}
    else
    {
    
    foreach($element_value as $value){if($set){break;} recursive_array_check_blank($value);}
    
    }
    
    }
    
    recursive_array_check_blank($form_input);
    
    if(!$set){$errors[] = "You cannot send a blank form";}
    
    // Strip HTML tags from all fields.
    
    function recursive_array_check2(&$element_value)
    {
    
    if(!is_array($element_value)){$element_value = strip_tags($element_value);}
    else
    {
    
    foreach($element_value as $key => $value){$element_value[$key] = recursive_array_check2($value);}
    
    }
    
    return $element_value;
    
    }
    
    recursive_array_check2($form_input);
    
    // Validate name field.
    
    if(isset($form_input['name']) && !empty($form_input['name']))
    {
    
    if(preg_match("`[\r\n]`",$form_input['name'])){$errors[] = "You have submitted an invalid new line character";}
    
    if(preg_match("/[^a-z' -]/i",stripslashes($form_input['name']))){$errors[] = "You have submitted an invalid character in the name field";}
    
    }
    
    // Validate email field.
    
    if(isset($form_input['email']) && !empty($form_input['email']))
    {
    
    if(preg_match("`[\r\n]`",$form_input['email'])){$errors[] = "You have submitted an invalid new line character";}
    
    
    }
    
    // Display any errors and exit if errors exist.
    
    if(count($errors)){foreach($errors as $value){print "$value<br>";} exit;}
    
    // Build message.
    
    function build_message($request_input){if(!isset($message_output)){$message_output ="";}if(!is_array($request_input)){$message_output = $request_input;}else{foreach($request_input as $key => $value){if(!empty($value)){if(!is_numeric($key)){$message_output .= "\n".$key.": ".build_message($value);}else{$message_output .= "\n".build_message($value);}}}}return $message_output;}
    
    $message = build_message($form_input);
    
    $message = $message . "\n\n-- \n Email sent from Website";
    
    $message = stripslashes($message);
    
    $subject = "Referral Form";
    
    $headers = "From: " . $form_input['email'] . "\n" . "Return-Path: " . $form_input['email'] . "\n" . "Reply-To: " . $form_input['email'] . "\n";
    
    mail($my_email,$subject,$message,$headers);
    
    ?>
    PHP:
    My question is how do I go about formatting what the email will look like?

    Thanks
     
    nickharper, Oct 8, 2007 IP
  2. nabil_kadimi

    nabil_kadimi Well-Known Member

    Messages:
    1,065
    Likes Received:
    69
    Best Answers:
    0
    Trophy Points:
    195
    #2
    Do you want to send HTML messages?
     
    nabil_kadimi, Oct 8, 2007 IP
  3. nickharper

    nickharper Active Member

    Messages:
    732
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    75
    #3
    Yes, basically I want the emails to look nice and formatted when they go through to the recipient.
     
    nickharper, Oct 9, 2007 IP
  4. nabil_kadimi

    nabil_kadimi Well-Known Member

    Messages:
    1,065
    Likes Received:
    69
    Best Answers:
    0
    Trophy Points:
    195
    #4
    nabil_kadimi, Oct 9, 2007 IP
  5. kendo1979

    kendo1979 Peon

    Messages:
    208
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    if you are not using template, have you tried heredoc ?

    $message = <<<MAIL
    here you make a
    template design with all the variable needed


    MAIL;

    then you send it with simple mail() ;
     
    kendo1979, Oct 10, 2007 IP
  6. jemy

    jemy Peon

    Messages:
    70
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    you can send in html via inbox phpmmailer download here
     
    jemy, Oct 10, 2007 IP